SELENIUM WEB AUTOMATION
Browser Setup
• Using System Property method
[Link]("[Link]", "path/to/chromedriver"); WebDriver
driver = new ChromeDriver();
• Using WebDriverManager method
[Link]().setup(); WebDriver driver = new ChromeDriver();
Basic Browser Actions
[Link](url); // Open URL
[Link]().to("[Link] // Navigate to another URL
[Link]().back(); // Go back
[Link]().forward(); // Go forward
[Link]().refresh(); // Refresh the page
[Link](); // Get the title
[Link](); // Get the current URL
[Link](); // Get the page source
Locators
WebElement element = [Link]([Link]("id"));
WebElement element = [Link]([Link]("name"));
WebElement element = [Link]([Link]("class"));
WebElement element = [Link]([Link]("tag"));
WebElement element = [Link]([Link]("Exact Link Text"));
WebElement element = [Link]([Link]("Partial Link"));
WebElement element = [Link]([Link]("css-selector"));
WebElement element = [Link]([Link]("xpath"));
// Find multiple elements
List elements = [Link]([Link]("tag"));
Web Element Actions
// Click an element
WebElement button = [Link]([Link]("buttonId")); [Link]();
// Enter text
WebElement input = [Link]([Link]("inputId")); [Link]("text");
// Clear input field
[Link]();
// Get text
String text = [Link]();
// Get attribute
String attribute = [Link]("attributeName");
//Handling dropdown
WebElement dropdown = [Link]([Link]("dropdownId"));
Select select = new Select(dropdown);
// Select by visible text
[Link]("Option Text");
// Select by value
[Link]("value");
// Select by index
[Link](0);
// Get all options
List<WebElement> options = [Link]();
Handling Alerts
// Switch to alert
Alert alert = [Link]().alert();
[Link](); // Accept alert
[Link](); // Dismiss alert
String alertText = [Link](); // Get alert text
[Link]("text"); // Send input to alert
Waits
• Implicit wait
[Link]().timeouts().implicitlyWait([Link](10));
• Explicit wait
WebDriverWait wait = new WebDriverWait(driver, [Link](10));
WebElement e = [Link]([Link]([Link]("id")));
Handling Frames
// Switch to frame by index
[Link]().frame(0);
// Switch to frame by name or ID
[Link]().frame("frameName");
// Switch to frame by WebElement
WebElement frameElement = [Link]([Link]("iframe"));
[Link]().frame(frameElement);
// Switch back to default content
[Link]().defaultContent();
Taking Screenshots
File screenshot = ((TakesScreenshot) driver).getScreenshotAs([Link]);
[Link](screenshot, new File("/[Link]"));
Assertions
// Assert conditions
[Link](actual, expected);
[Link](condition);
[Link](condition);
[Link](object);
Handling windows
JavascriptExecutor js = (JavascriptExecutor) driver; // Execute JavaScript
[Link]("[Link](0,500)"); // Scroll down
[Link]("arguments[0].click();", element); // Click an element