Alert and Popup Handling in Selenium WebDriver
Posted By : Mohd Sheemal | 25-Apr-2018
Alert & Popup Handling in Selenium WebDriver :
What is Alert ?
Alert is a little message box which shows on-screen warning to give the client some sort of data or request consent to play out certain sort of task. It might be additionally utilized for notice reason.
Here are few alert types:
1- Simple Alert :
This basic alert shows some data or cautioning on the screen.
2- Prompt Alert :
This Prompt Alert asks some commitment from the customer and selenium webdriver can enter the substance using sendkeys(" ")
3- Confirmation Alert :
This affirmation caution solicits consent to do some write from activity.
How to handle Alert in Selenium WebDriver :
Ready interface gives the beneath couple of techniques which are generally utilized as a part of Selenium Webdriver.
1) void dismiss() // To click on the 'Cancel' button of the alert.
driver.switchTo().alert().dismiss();
2) void accept() // To tap on the 'alright' catch of the caution.
driver.switchTo().alert().accept();
3) String getText() // To catch the alert message.
driver.switchTo().alert().getText();
4) void sendKeys(String stringToSend) // To send a few information to caution box.
driver.switchTo().alert().sendKeys("Text here");
You can see various Alert techniques are shown as appeared in underneath screen proposed by Eclipse.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.Alert;
public class AlertDemo {
public static void main(String[] args) throws NoAlertPresentException,InterruptedException {
System.setProperty("webdriver.chrome.driver"," chrome driver path here ");
WebDriver driver = new ChromeDriver();
// Alert Message handling
driver.get(" url here ");
driver.findElement(By.name(" ")).sendKeys(" ");
driver.findElement(By.name("submit")).submit();
// Switching to Alert
Alert alert = driver.switchTo().alert();
// Capturing alert message.
String alertMessage= driver.switchTo().alert().getText();
// Displaying alert message
System.out.println(alertMessage);
Thread.sleep(5000);
// Accepting alert
alert.accept();
}
}
In automation, when we have different windows in any web application, the action may need to switch control among a few windows from one to other with a specific end goal to finish the task. After consummation of the activity, it needs to come back to the principle window i.e. parent window.
In selenium web driver there are strategies through which we can deal with numerous windows.
Driver.getWindowHandles();
To manage each and every opened window by web driver, we can use "D
Driver.getWindowHandle();
At the point when the site opens, we have to deal with the principle window by driver.getWindowHandle(). This will deal with the ebb and flow window that exceptionally distinguishes it inside this driver case. Its arrival write is String.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Mohd Sheemal
Mohd Sheemal is a Bright QA Engineer. He has a good knowledge over Selenium.