In this tutorial, we will let you know about different types of alert appeared in web applications and how to handle these Alerts in Selenium WebDriver. We will also figure out how do we accept and reject the alert depending upon the alert types. Alert is a small pop up window that comes up on the screen. There are numerous user activities that can bring about an alert on-screen. For e.g. user clicked on a button that displayed a message, The HTML page asked you for some extra information. So in this article, we will learn how to Handle Alerts, JavaScript Alerts, and PopUp Boxes in Selenium WebDriver.
What is Alert?
An alert is a small dialog box that displays on-screen. It gives some kind of information to the user or asks for permission to perform a certain kind of operation. It can be also used for warning purposes. Alerts are different from regular windows. The major difference is that alerts are blocking in nature, they will not allow any action on the underlying webpage if they are present.How to handle Alert in Selenium WebDriver
Selenium WebDriver provides the following methods to interact with the Alerts depending on the Alert types.
1. void dismiss(): It is used to cancel the alert
driver.switchTo().alert().dismiss();
2. void accept(): It is used to click on ok button of the alert
driver.switchTo().alert().accept();
3. String getText(): It captures the alert message text
driver.switchTo().alert().getText();
4. void sendKeys(String stringToSend): It sends some data to alert
driver.switchTo().alert().sendKeys("Text");
1. Simple alert
Simple alerts have just the ok button on them, they give some information to users. Following is the practice example of a simple alert in Selenium Webdriver.import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SimpleAlertBox { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); // Launch the URL driver.get("https://www.testandquiz.com/selenium/testing.html"); driver.manage().window().maximize(); driver.findElement(By.xpath("//button[text()='Generate Alert Box']")).click(); // First Alert class will shift the focus to the alert box Alert alert = driver.switchTo().alert(); // Accept() method will accept the alert box alert.accept(); // Close the browser driver.close(); } }
2. Confirmation Alert
Confirmation Alert comes with two options (Ok/Cancel), you can accept it or dismiss it. It requires user confirmation to perform some task. Users can accept it or cancel it. Here is a code to dismiss the confirmation alert.import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class ConfirmationAlert { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); // Launch the URL driver.get("https://www.testandquiz.com/selenium/testing.html"); driver.manage().window().maximize(); driver.findElement(By.xpath("//button[text()='Generate Confirm Box']")).click(); Alert confirmationAlert = driver.switchTo().alert(); confirmationAlert.dismiss(); // Close the browser driver.close(); } }
3. Prompt Alert
In the prompt alert, you can enter some text by using sendkeys method. These alerts are used when some information is required from the user side. Here is the code example for the prompt alert.import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class PromptAlert { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); // Launch the URL driver.get("https://www.khawarautomationlabs.com/2020/04/alerts-in-selenium-webdriver-in-this.html"); driver.manage().window().maximize(); driver.findElement(By.xpath("//button[text() = 'Click on me']")).click(); Alert promptAlert = driver.switchTo().alert(); String alertText = promptAlert.getText(); System.out.println("Alert text is: " + alertText); promptAlert.sendKeys("Prompt Alert"); promptAlert.accept(); } }
0 comments:
Post a Comment