How to handle javascript alerts, confirmation and prompts?
2016-03-03 15:02
495 查看
http://www.seleniumeasy.com/selenium-tutorials/how-to-handle-javascript-alerts-confirmation-prompts
Generally JavaScript popups are generated by web application and hence they can be easily controlled by the browser.
Webdriver offers the ability to cope with javascript alerts using Alerts APIClick here to view Alert API Details
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
Alert is an interface. There below are the methods that are used
//Will Click on OK button.
alert.accept();
// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on th Alert.
alert.getText();
//Will pass the text to the prompt popup
alert.sendkeys();
//Is used to Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)
The below program will demonstrate you working on Alerts popup using above html file.
The below program will demonstrate you working on Confirmation popup using above html file.
The below is the sample code for prompt popup, please copy and make an html file and pass it to the webdriver as below.
The below program will demonstrate you working on prompt popup using above html file.
Generally JavaScript popups are generated by web application and hence they can be easily controlled by the browser.
Webdriver offers the ability to cope with javascript alerts using Alerts APIClick here to view Alert API Details
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
Alert is an interface. There below are the methods that are used
//Will Click on OK button.
alert.accept();
// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on th Alert.
alert.getText();
//Will pass the text to the prompt popup
alert.sendkeys();
//Is used to Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)
Working with Alerts using Selenium Webdriver:
The below is the sample code for alerts, please copy and make an html file and pass it to the webdriver.<html> <head> <title>Selenium Easy Alerts Sample </title> </head> <body> <h2>Alert Box Example</h2> <fieldset> <legend>Alert Box</legend><p>Click the button to display an alert box.</p> <button onclick="alertFunction()">Click on me</button> <script> function alertFunction() { alert("I am an example for alert box!"); } </script> </fieldset> </body> </html>
The below program will demonstrate you working on Alerts popup using above html file.
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class PopupsHandling { WebDriver driver=new FirefoxDriver(); @Test public void ExampleForAlert() throws InterruptedException { driver.manage().window().maximize(); driver.get("file:///C:/path/alerts.html"); Thread.sleep(2000); driver.findElement(By.xpath("//button[@onclick='alertFunction()']")).click(); Alert alert=driver.switchTo().alert(); System.out.println(alert.getText()); alert.accept(); } }
Working with Confirmation Popups
The below is the sample code for confirmation Popup, please copy and make an html file and pass it to the webdriver as below.<html> <head> <title>Selenium Easy Confirm popup Sample </title> </head> <body> <h2>Confirm Box Example</h2> <fieldset> <legend>Confirm Box</legend> <p>Click the button to display a confirm box.</p> <button onclick="confirmFunction()">Click on me</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> </body> </html>
The below program will demonstrate you working on Confirmation popup using above html file.
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class PopupsHandling { WebDriver driver=new FirefoxDriver(); @Test public void ExampleForConfirmBox() throws InterruptedException { driver.manage().window().maximize(); driver.get("file:///C:/path/confirmation.html"); Thread.sleep(2000); driver.findElement(By.xpath("//button[@onclick='confirmFunction()']")).click(); Alert alert=driver.switchTo().alert(); System.out.println(alert.getText()); alert.dismiss(); } }
Working with Prompt Popups.
In prompt, you can enter the text using webdriver sendkeys("text..")The below is the sample code for prompt popup, please copy and make an html file and pass it to the webdriver as below.
<html> <head> <title>Selenium Easy Prompt popup Sample </title> </head> <body> <h2>Prompt Box Example</h2> <fieldset> <legend>Prompt Box</legend> <p>Click the button to demonstrate the prompt box.</p> <button onclick="promptFunction()">Click on me</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>
The below program will demonstrate you working on prompt popup using above html file.
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class PopupsHandling { WebDriver driver=new FirefoxDriver(); @Test public void ExampleForPromptBox() throws InterruptedException { driver.manage().window().maximize(); driver.get("file:///C:/path/prompt.html"); Thread.sleep(2000); driver.findElement(By.xpath("//button[@onclick='promptFunction()']")).click(); Alert alert=driver.switchTo().alert(); driver.switchTo().alert().sendKeys("Helllo"); alert.accept(); System.out.println(alert.getText()); } }
相关文章推荐
- js获取节点
- 字符串和JSON对象互转的方法
- Javascript进阶篇——( JavaScript内置对象---下)--Array数组对象---笔记整理
- angular js 在ie11 下的get请求缓存问题的解决办法
- 关于ActiveX控件开发、js调用
- C# JSON和对象之间互相转换
- javascript中eval的用法
- javascript 高级程序设计 五
- [LeetCode][JavaScript]Palindrome Number
- javascript阻止事件冒泡和浏览器的默认行为
- js 时间操作
- [JavaScript][window&forms]
- javascript 封装ajax(多版本)
- com.fasterxml.jackson.core.JsonParseException: Unexpected character
- javaScript中的两个叹号(!!)
- JavaScript HTML DOM
- javascript数组去重
- 7、JavaScript总结——实现选项卡切换的效果
- html+js实现图片预览
- JS1 js获取dom元素方法