Please copy the below is the sample code for alerts, Confirmation Popups, Prompt Popups and make an html file.
<html>
<head>
<title>JavaScript Pupups </title>
</head>
<body>
<h2>Alert Box</h2>
<fieldset>
<legend>Alert Box</legend><p>Click the button to display an alert box.</p>
<button onclick="alertFunction()">Alerts</button>
<script>
function alertFunction()
{
alert("I am an example for alert box!");
}
</script>
</fieldset>
<h2>Confirm Box</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick="confirmFunction()">Confirm</button>
<p id="confirmdemo"></p>
<script>
function confirmFunction()
{
var cb;
var c=confirm("I am an Example for Confirm Box.\n Press any button!");
if (c==true)
{
cb="You Clicked on OK!";
}
else
{
cb="You Clicked on Cancel!";
}
document.getElementById("confirmdemo").innerHTML=cb;
}
</script>
</fieldset>
<h2>Prompt Box</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="promptFunction()">Prompt</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter your name","Your name");
if (person!=null)
{
x="Hello " + person + "! Welcome to Selenium Easy..";
document.getElementById("promptdemo").innerHTML=x;
}
}
</script>
</fieldset>
</body>
</html>
<html>
<head>
<title>JavaScript Pupups </title>
</head>
<body>
<h2>Alert Box</h2>
<fieldset>
<legend>Alert Box</legend><p>Click the button to display an alert box.</p>
<button onclick="alertFunction()">Alerts</button>
<script>
function alertFunction()
{
alert("I am an example for alert box!");
}
</script>
</fieldset>
<h2>Confirm Box</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick="confirmFunction()">Confirm</button>
<p id="confirmdemo"></p>
<script>
function confirmFunction()
{
var cb;
var c=confirm("I am an Example for Confirm Box.\n Press any button!");
if (c==true)
{
cb="You Clicked on OK!";
}
else
{
cb="You Clicked on Cancel!";
}
document.getElementById("confirmdemo").innerHTML=cb;
}
</script>
</fieldset>
<h2>Prompt Box</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="promptFunction()">Prompt</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter your name","Your name");
if (person!=null)
{
x="Hello " + person + "! Welcome to Selenium Easy..";
document.getElementById("promptdemo").innerHTML=x;
}
}
</script>
</fieldset>
</body>
</html>
Handle JavaScript Popups in selenium webdriver using java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavaScriptPopup {
public static void main(String[] a) throws InterruptedException {
// Initialize Web driver
WebDriver driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to Page
driver.get("file:///E:/Javascript%20Popup.html");
//===========Alerts ======
//click on button
driver.findElement(By.xpath("html/body/fieldset[1]/button")).click();
//handle to the open alert.
Alert alert = driver.switchTo().alert();
//get the text which is present on th Alert.
System.out.println(alert.getText());
Thread.sleep(3000);
//Click on OK button.
alert.accept();
//===========Confirmation Popups======
//click on button
driver.findElement(By.xpath("html/body/fieldset[2]/button")).click();
//handle to the open confirmation popup.
Alert Confirm = driver.switchTo().alert();
Thread.sleep(3000);
//click on Cancel button.
Confirm.dismiss();
//===========Prompt Popups======
//click on button
driver.findElement(By.xpath("html/body/fieldset[3]/button")).click();
//handle to the open prompt popup.
Alert Prompt = driver.switchTo().alert();
//pass the text to the prompt popup
Prompt.sendKeys("Prompt Box");
Thread.sleep(3000);
//Click on OK button.
Prompt.accept();
//close browser
driver.quit();
}
}
Woow, this works fine for me
ReplyDeleteHelpful stuff to learn about Selenium Webdriver. Thanks for the share mate.
ReplyDelete
ReplyDeleteAmazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
When I started learning then I understood it has got really cool stuff.
I can vouch webdriver has proved the best feature in Selenium framework.
thanks a lot for taking a time to share a wonderful
free selenium article.
Thank you very much......:D it is working for me.....
ReplyDelete
ReplyDeleteThanks, this is generally helpful.
Still, I followed step-by-step your method in this selenium online training
selenium certification
selenium online courses
learn selenium online