0% found this document useful (0 votes)
8 views

Selenium Java Cheat Sheet

This document is a cheat sheet for Selenium with Java, covering essential commands for handling windows, taking screenshots, using JavascriptExecutor, and performing wait operations. It includes code snippets for various functionalities such as switching windows, capturing screenshots, executing JavaScript, and implementing different types of waits. The document serves as a quick reference for developers working with Selenium in Java.

Uploaded by

manojnraikar123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Selenium Java Cheat Sheet

This document is a cheat sheet for Selenium with Java, covering essential commands for handling windows, taking screenshots, using JavascriptExecutor, and performing wait operations. It includes code snippets for various functionalities such as switching windows, capturing screenshots, executing JavaScript, and implementing different types of waits. The document serves as a quick reference for developers working with Selenium in Java.

Uploaded by

manojnraikar123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

01/08

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

Get InnerText of a Webpage


string text = js.executeScript(“return
document.documentElement.innerText;”).toString();
Get Title of a WebPage
string text = js.executeScript(“return
document.title;”).toString();
Click an Element
WebElement loginButton = driver.findElement(
By.id("elementId"));

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

Fluent wait - local wait. Is like Explicit with more


options
Wait<WebDriver> fluentWait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(Time in Seconds)
.pollingEvery(Duration.ofMillis(Time in Milliseconds))
.ignoring(ElementNotInteractableException.class);

WebElement element = wait.until(ExpectedConditions


.visibilityOfElementLocated(By.id("id")));
Java hard wait
Thread.sleep(Time in MilliSeconds);

alphabin.co
08/08

Was This
Helpful?

alphabin.co

You might also like