Day 15: Selenium in-depth

Day 15: Selenium in-depth

Selenium provides several features that help in automating web applications effectively. Two important features are handling windows and alerts, and using waits to synchronize test execution with the application's behavior. Below, you'll find explanations of these features along with sample programs demonstrating the usage of different types of waits.

Handling Windows and Alerts:

Selenium allows you to handle multiple windows and alerts that may appear during the execution of your tests. You can switch between windows and interact with alerts using WebDriver's methods.

Here's an example program that demonstrates handling windows and alerts:

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowAndAlertHandlingExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage with a link that opens a new window
????driver.get("https://www.example.com");

????// Store the parent window handle
????String parentWindowHandle = driver.getWindowHandle();

????// Click on a link that opens a new window
????driver.findElement(By.linkText("Open New Window")).click();

????// Switch to the new window
????for (String windowHandle : driver.getWindowHandles()) {
??????if (!windowHandle.equals(parentWindowHandle)) {
????????driver.switchTo().window(windowHandle);
????????break;
??????}
????}

????// Perform actions on the new window
????System.out.println("New Window Title: " + driver.getTitle());

????// Close the new window
????driver.close();

????// Switch back to the parent window
????driver.switchTo().window(parentWindowHandle);

????// Handle an alert dialog
????Alert alert = driver.switchTo().alert();
????System.out.println("Alert Text: " + alert.getText());
????alert.accept();

????// Close the browser
????driver.quit();
??}
}

In this example, we navigate to a webpage and click on a link that opens a new window. We store the parent window handle, switch to the new window, perform actions on it, and then close it. After that, we switch back to the parent window and handle an alert dialog before closing the browser.

Waits:

Waits are essential in Selenium to synchronize test execution with the application's behavior. They ensure that the WebDriver waits for certain conditions to be met before performing actions on elements.

There are three types of waits in Selenium:

Implicit Waits:

Implicit waits set a default waiting time for the WebDriver to wait for an element to be available before throwing an exception. Here's a sample program that demonstrates implicit waits:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class ImplicitWaitExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Set implicit wait time
????driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Find an element using implicit wait
????WebElement element = driver.findElement(By.id("myElement"));

????// Perform actions on the element
????element.click();

????// Close the browser
????driver.quit();
??}
}

In this example, we set the implicit wait time to 10 seconds using driver.manage().timeouts().implicitlyWait(). This means the WebDriver will wait for a maximum of 10 seconds for the element to be available before throwing an exception.

Explicit Waits:

Explicit waits allow you to wait for a specific condition to be met before performing actions on elements. Here's an example that demonstrates explicit waits:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWaitExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Create an explicit wait with a timeout of 10 seconds
????WebDriverWait wait = new WebDriverWait(driver, 10);

????// Wait until an element is clickable
????WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("myElement")));

????// Perform actions on the element
????element.click();

????// Close the browser
????driver.quit();
??}
}

In this example, we create an explicit wait using WebDriverWait and specify a timeout of 10 seconds. We then use the until() method along with an expected condition (ExpectedConditions.elementToBeClickable()) to wait until the element is clickable before performing actions on it.

Fluent Waits:

Fluent waits provide more flexibility in defining waiting conditions and polling intervals. Here's an example that demonstrates fluent waits:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class FluentWaitExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();

????// Navigate to a webpage
????driver.get("https://www.example.com");

????// Create a fluent wait with a timeout of 10 seconds and polling interval of 500 milliseconds
????FluentWait<WebDriver> wait = new FluentWait<>(driver)
????????.withTimeout(Duration.ofSeconds(10))
????????.pollingEvery(Duration.ofMillis(500));

????// Wait until an element is visible
????WebElement element = wait.until(driver -> driver.findElement(By.id("myElement")));

????// Perform actions on the element
????element.click();

????// Close the browser
????driver.quit();
??}
}

In this example, we create a fluent wait using FluentWait and specify a timeout of 10 seconds and a polling interval of 500 milliseconds. We use the until() method with a lambda expression to define the waiting condition, which waits until the element is visible before returning it.

These sample programs demonstrate the usage of different types of waits in Selenium: implicit waits, explicit waits, and fluent waits. By incorporating waits in your Selenium tests, you can ensure that the tests are synchronized with the behavior of the web application, allowing for more reliable and stable test automation.

要查看或添加评论,请登录

Shree Krishna Priya J的更多文章

社区洞察

其他会员也浏览了