In this tutorial, we will see ‘How to Handle Alerts in Selenium’. There are different types of alerts we can see while testing web based applications. While filling the forms we get this alert after clicking on submit button. We will also see ‘How to Handle Window popup in Selenium’.
Let’s start.
Table of Contents
What is Alert?
Alert is simple message box which appears after clicking on any button to provide some information to the user or ask to perform some actions or provide some warning message.
Types of Alerts:
- Windows based alert
- Web based alert
As we know Selenium only support web based application that is why selenium does not support window based alert. For this we can use third party tool like ‘AutoIt’ or ‘Robot’ to handle it.
Web based alert can easily handle by Selenium using Alert Interface.
There are different types of Web based alert.
- Simple Alert:
This alert is used to notify the warning message to the user with an ‘Ok’ button.
2. Prompt Alert:
This alert will ask user to fill some information to complete the task.
In below screen we can see without filling details we cannot click on Search button.
3. Confirmation Alert:
This alert is used to confirm some task to the user and contains Yes and No button. For Example:
‘Are you sure, you want to delete this photo?’
How to Handle Alerts in Selenium
In Selenium, Alert interface provides different methods which are used to handle the web based popup or alerts.
void dismiss(): To click ‘cancel’ button of the alert.
driver.switchTo().alert().dismiss();
void accept(): To click ‘Ok’ button of the alert.
driver.switchTo().alert.accept();
String getText(): To get the text from alert.
driver.switchTo().alert.getText();
void sendKeys(): To enter some text in alert’s text field.
driver.switchTo().alert.sendkeys(“Text”);
Apart from this there are so many methods in Alert interface. You can see in below screenshot.
Now we will see one scenario and automate it.
Step1: Open this URL – http://seleniumpractise.blogspot.com/2017/01/how-to-handle-alert-confirmation.html
Step2: Click on ‘Try it’ button.
Step3: Enter your name in textbox.
Step4: Click on Ok button.
Handling Alert in Selenium Web driver using above scenario
package datapkg; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class AlertHandle { @Test public void A() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://seleniumpractise.blogspot.com/2017/01/how-to-handle-alert-confirmation.html"); driver.manage().window().maximize(); //click on Try it button driver.findElement(By.xpath("//p[@id='demo']//preceding::button")).click(); Thread.sleep(1000); driver.switchTo().alert().sendKeys("Ajeet Maurya"); driver.switchTo().alert().accept(); } }
Now we will see ‘How to handle windows popup in selenium’.
As we know Selenium only supports to Web based popups, So we cannot handle window popups using selenium because in window popups don’t have any locators but selenium supports third party tool like ‘AutoIt’ and Robot class, using this we can handle window based popups.
Refer below link to handle windows popups using AutoIt.
How to handle window popup in Selenium
Handle window popup in Selenium using Robot Class
Robot class is a Java utility which emulates the keyboards and mouse actions.
Below scenario we will automate using Robot class-
- Open browser and open ‘gmail.com’.
- Enter valid username and password.
- Click on Sign In button.
- Click on Compose button
- Click on attach file icon.
- Select the files to be uploaded with the window based pop up.
Code:
package datapkg; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class AlertHandle { @Test public void A() throws InterruptedException, AWTException { System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://gmail.com"); driver.manage().window().maximize(); Thread.sleep(1000); // enter a valid email address driver.findElement(By.xpath("//input[@type='email']")).sendKeys("automationtestings20@gmail.com"); driver.findElement(By.xpath("//span[contains(text(), 'Next')]")).click(); // enter a valid password driver.findElement(By.xpath("//input[@type='password']")).sendKeys("Neetu@1234"); // click on sign in button driver.findElement(By.id("signIn")).click(); Thread.sleep(30000); // click on compose button driver.findElement(By.xpath("//div[@class='z0']//div[contains(text(),'COMPOSE')]")).click(); // click on attach files icon driver.findElement(By.xpath("//div[contains(@command,'Files')]//div[contains(@class,'aaA')]")).click(); // creating instance of Robot class (A java based utility) Robot rb =new Robot(); // pressing keys with the help of keyPress and keyRelease events rb.keyPress(KeyEvent.VK_D); rb.keyRelease(KeyEvent.VK_D); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_SHIFT); rb.keyPress(KeyEvent.VK_SEMICOLON); rb.keyRelease(KeyEvent.VK_SEMICOLON); rb.keyRelease(KeyEvent.VK_SHIFT); rb.keyPress(KeyEvent.VK_BACK_SLASH); rb.keyRelease(KeyEvent.VK_BACK_SLASH); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_P); rb.keyRelease(KeyEvent.VK_P); rb.keyPress(KeyEvent.VK_I); rb.keyRelease(KeyEvent.VK_I); rb.keyPress(KeyEvent.VK_C); rb.keyRelease(KeyEvent.VK_C); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_ENTER); rb.keyRelease(KeyEvent.VK_ENTER); Thread.sleep(2000); } }
How to Handle Window in Selenium
Refer below link for handling window in selenium.
How to Handle Window in Selenium
I hope you have enjoyed this tutorial. Happy Learning 🙂