Selenium WebDriver- Handling Alerts

In this section, you will learn how to handle alerts in Selenium WebDriver.

Selenium WebDriver provides three methods to accept and reject the Alert depending on the Alert types.

1. void dismiss()

This method is used to click on the 'Cancel' button of the alert.

Syntax:

snippet
driver.switchTo().alert().dismiss();

2. void accept()

This method is used to click on the 'Ok' button of the alert.

Syntax:

snippet
driver.switchTo().alert().accept();

3. String getText()

This method is used to capture the alert message.

Syntax:

snippet
driver.switchTo().alert().getText();

4. void sendKeys(String stringToSend)

This method is used to send some data to the alert box.

Syntax:

snippet
driver.switchTo().alert().sendKeys("Text");

Let us consider a test case in which we will automate the following scenarios:

We will create our test case step by step in order to give you a complete understanding of how to handle alerts in WebDriver.

Step1. Launch Eclipse IDE and open the existing test suite "Demo_Test" which we have created in earlier sessions of this tutorial.

Step2. Right click on the "src" folder and create a new Class File from New >Class.

Selenium WebDriver- Handling Alerts

Give your Class name as "alert_test" and click on "Finish" button.

Selenium WebDriver- Handling Alerts

Step3. Let's get to the coding ground.

  • To invoke Firefox browser, we need to download Gecko driver and set the system property for Gecko driver. We have already discussed this in earlier sessions of this tutorial. You can refer "Running test on Firefox Browser" to learn how to download and set System property for Firefox driver.

Here is the sample code to set system property for Gecko driver:

snippet
// System Property for Gecko Driver 
	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
  • After that we have to initialize Gecko Driver using Desired Capabilities Class.

Here is the sample code to initialize gecko driver using DesiredCapabilities class.

snippet
// Initialize Gecko Driver using Desired Capabilities Class
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability("marionette",true);
	WebDriver driver= new FirefoxDriver(capabilities);

Combining both of the above code blocks, we will get the code snippet to launch Firefox browser.

snippet
// System Property for Gecko Driver 
	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
		
	 // Initialize Gecko Driver using Desired Capabilities Class
	DesiredCapabilities capabilities = DesiredCapabilities.firefox();
	capabilities.setCapability("marionette",true);
	WebDriver driver= new FirefoxDriver(capabilities);
  • After that we need to write the code which will automate our second test scenario (navigate to the desired URL)

Here is the sample code to navigate to the desired URL:

snippet
// Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");

The complete code till now will look something like this:

snippet
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class alert_test {

	public static void main(String[] args) {
		
	// System Property for Gecko Driver 
		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
			
	// Initialize Gecko Driver using Desired Capabilities Class
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
			capabilities.setCapability("marionette",true);
			WebDriver driver= new FirefoxDriver(capabilities);
		

      // Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
	
	}

}

Step4. Now, we will try to locate the "Generate Alert Box" and "Generate Confirm Box" in order to perform alert handling operation. As we know that locating an element involves inspection of its HTML codes.

Follow the steps given below to locate the drop-down menu on the sample web page.

Selenium WebDriver- Handling Alerts
  • It will launch a window containing all the specific codes involved in the development of the "Generate Alert Box" button.
Selenium WebDriver- Handling Alerts
  • Take a note of its link text i.e. "Generate Alert Box"
Selenium WebDriver- Handling Alerts

Similarly, we will inspect the "Generate Confirm box" button.

Selenium WebDriver- Handling Alerts
  • Take a note of its link text i.e. "Generate Confirm Box"
Selenium WebDriver- Handling Alerts

Step5.

To automate our third and fourth test scenario, we need to write the code which click and accept the Generate Alert box as well as click and accept the Generate Confirm box.

Given below is the code snippet to automate our third and fourth test scenario.

snippet
//Handling alert boxes
	//Click on generate alert button
driver.findElement(By.linkText("Generate Alert Box")).click();
		
	//Using Alert class to first switch to or focus to the alert box
Alert alert = driver.switchTo().alert();
		
	//Using accept() method to accep the alert box
	alert.accept();
		
	//Handling confirm box
	//Click on Generate Confirm Box
driver.findElement(By.linkText("Generate Confirm Box")).click();
		
		
	Alert confirmBox = driver.switchTo().alert();
		
	//Using dismiss() command to dismiss the confirm box
	//Similarly accept can be used to accept the confirm box
	confirmBox.dismiss();

Thus, our final test script will look something like this:

snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Alert;
public class alert_test {

	public static void main(String[] args) {
		
		// System Property for Gecko Driver 
		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
			
		// Initialize Gecko Driver using Desired Capabilities Class
			DesiredCapabilities capabilities = DesiredCapabilities.firefox();
			capabilities.setCapability("marionette",true);
			WebDriver driver= new FirefoxDriver(capabilities);
		

		// Launch Website
		driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 

		//Handling alert boxes
		//Click on generate alert button
		driver.findElement(By.linkText("Generate Alert Box")).click();
		
		//Using Alert class to first switch to or focus to the alert box
		Alert alert = (Alert) driver.switchTo().alert();
		
		//Using accept() method to accept the alert box
		alert.accept();
		
		//Handling confirm box
		//Click on Generate Confirm Box
		driver.findElement(By.linkText("Generate Confirm Box")).click();
		
		
		Alert confirmBox = (Alert) driver.switchTo().alert();
		
		//Using dismiss() command to dismiss the confirm box
		//Similarly accept can be used to accept the confirm box
		((Alert) confirmBox).dismiss();


	
	}

}

Upon execution, the above test script will launch the Firefox browser and automate all the test scenarios.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +