Articles → SELENIUM → Handling Javascript Alerts In Selenium

Handling Javascript Alerts In Selenium






Alert Class In Java




  1. accept() – Method used to click ‘Ok’ button in an alert.
  2. dismiss() – Method used to click ‘Cancel’ button in an alert
  3. getText() – Method used to get text from an alert.
  4. sendKeys() – Method used to send text to an alert.



Simple Alert Box




Picture showing the different parts of the alert box

Click to Enlarge





	<!DOCTYPE html>
	<html lang="en"
		xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta charset="utf-8" />
			<title></title>
		</head>
		<body>
			<script>
	alert("This is the test alert message");
	</script>
		</body></html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.accept();
		System.out.println(alertMessage);
	}
}




  1. An alert message appears
  2. ‘Ok’ button is clicked and alert message disappears.
  3. An alert message will be displayed in console.
  4. Picture showing how to handle simple alert box in selenium

    Click to Enlarge



Confirm Alert Box




Picture showing the different parts of the confirm box

Click to Enlarge





		<!DOCTYPE html>
		<html lang="en"
			xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<meta charset="utf-8" />
				<title></title>
			</head>
			<body>
				<script>
        var out = confirm("Are you sure you want to click on Ok");
        document.write(out);
    </script>
			</body></html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.accept();
		System.out.println(alertMessage);
	}
}




  1. An alert message appears
  2. ‘Ok’ button is clicked and alert message disappears.
  3. An alert message text will be displayed in console.
  4. ‘true’ is written on HTML page.
Picture showing how the confirm box is handled using selenium when ok button is clicked

Click to Enlarge



package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.dismiss();
		System.out.println(alertMessage);
	}
}




  1. An alert message appears
  2. ‘Cancel’ button is clicked and alert message disappears.
  3. An alert message text will be displayed in console.
  4. ‘false’ is written on HTML page.
  5. Picture showing how the confirm box is handled using selenium when cancel button is clicked

    Click to Enlarge



Prompt Alert Box




Picture showing the different components of the prompt alert box

Click to Enlarge



			<!DOCTYPE html>
			<html lang="en"
				xmlns="http://www.w3.org/1999/xhtml">
				<head>
					<meta charset="utf-8" />
					<title></title>
				</head>
				<body>
					<script>
       prompt("Please enter your age", "18");
    </script>
				</body></html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.sendKeys("19");
		alert.dismiss();
		System.out.println(alertMessage);
	}
}




  1. A prompt alert message will appear with ‘18’ as its default value
  2. Value will be changed to ‘19’.
  3. Picture showing the prompt alert box for the user to enter the value

    Click to Enlarge

  4. ‘Cancel’ button is clicked and alert message disappears.
  5. An alert message text will be displayed in console.
  6. Picture showing how the prompt alert box is handled in selenium

    Click to Enlarge



Posted By  -  Karan Gupta
 
Posted On  -  Saturday, April 15, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250