Selenium Java Cheat Sheet
Selenium Java Cheat Sheet
Selenium - Java
Cheat Sheet (Part 2)
alphabin.co
02/08
Handling Windows
Get the current window handle:
String mainWindowHandle = driver.getWindowHandles();
Get all window handles:
Set<String> allWindowHandles =
driver.getWindowHandles();
Switch to a window by handle:
String windowHandle = “window_handle”;
driver.switchTo().window(windowHandle);
Switch back to main window:
driver.switchTo().window(windowHandle);
alphabin.co
03/08
Taking Screenshot
Take a screenshot of the whole page
File screenshotFile = ((TakeScreenshot)
driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new
File(“screenshot.png”));
Take a screenshot of a specific element
File elementScreenshotFile =
element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(elementScreenshotFile, new
File(“element_screenshot.png”));
alphabin.co
04/08
Use of JavascriptExecutor
Create a reference
JavascriptExecutor js = (JavascriptExecutor) driver;
To refresh the browser window
js.executeScript(“location.reload()”);
To send text
js.executeScript(“document.getElementByID(‘id ’).value =
‘Alphabin’;”);
Generate Alert Pop Window
Js.executeScript(“alert(‘hello world’);”);
alphabin.co
05/08
js.executeScript("arguments[0].click();", loginButton);
alphabin.co
06/08
Wait Operations
Implicit wait - global wait
driver.manage().timeouts().implicitlyWait(Duration.ofSec
onds(2));
Explicit wait - local wait
WebDriverWait wait = new WebDriverWait (driver,
Duration.ofSeconds(Time in Seconds);
WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocate
d(By.id("id")));
alphabin.co
07/08
alphabin.co
08/08
Was This
Helpful?
alphabin.co