Selenium2学习-040-JavaScript弹出框(alert、confirm、prompt)操作演示实例

弹出框是网页自动化测试常见得操作页面元素之一,常见的JavaScript弹出框有如下三种:

  • 1、alert(message):方法用于显示带有一条指定消息和一个 OK 按钮的警告框。DemoAlert.html 示例代码如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/theme.css" charset="utf-8"/> <title>元素操作实例-弹出框(警示、提示)</title> <script type="text/javascript">
function op_alert() {
alert("这是一个:弹出框(警示、提示)")
}
</script>
</head> <body>
<div>
<input class="alert" type="button" style="width:200px,height:20px" onclick="op_alert()" value="显示警告框" />
</div>
</body>
</html>
  • 2、confirm(message):方法用于显示一个带有指定消息和 OK 及取消按钮的对话框。DemoConfirm.html示例代码如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/theme.css" charset="utf-8"/> <title>元素操作实例-确认框(确认、取消)</title> <script type="text/javascript">
function op_confirm() {
var op = confirm("这是一个确认消息框示例,是否执行删除操作?") if(op == true) {
document.write("您按下了确认操作键!")
} else {
document.write("你按下了取消操作键!")
}
}
</script>
</head> <body>
<div>
<input class="alert" type="button" onclick="op_confirm()" value="显示确认框" />
</div>
</body>
</html>
  • 3、prompt(text,defaultText):方法用于显示可提示用户进行输入的对话框。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/theme.css" charset="utf-8"/> <title>元素操作实例-输入提示框</title> <script type="text/javascript">
function op_prompt() {
var name = prompt("Please enter your name :", "http://www.cnblogs.com/fengpingfan/") if((name == "范丰平" || name == "http://www.cnblogs.com/fengpingfan/") && name != "") {
document.getElementById('content').innerHTML = "Welcome to my cnblogs : <a href=\"http://www.cnblogs.com/fengpingfan/\">范丰平-博客园</a>"
} else if (name == "") {
document.getElementById('content').innerHTML = "Input nothing !"
} else if (name == null) {
document.getElementById('content').innerHTML = "Clicked cancel button !"
} else {
document.getElementById('content').innerHTML = "<a>Input content is : " + name + "</a>"
}
}
</script>
</head> <body>
<div id="content">
</div>
<div>
<input class="alert" type="button" onclick="op_prompt()" value="显示输入提示框" />
</div>
</body>
</html>

三者需要的 theme.css 文件如下所示:

.alert {
width: 300px;
height: 40px;
}

以上三种对话框均可通过 WebDriver 中的 Alert 去操作,操作源代码如下所示:

DemoAlert.java 源码如下所示:

 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2016 All Rights Reserved.
*/
package ffp.demo.webdriver; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test; /**
* <strong>元素操作实例-弹出框(警示框、提示框)<br></strong>
* alert(message) 方法用于显示带有一条指定消息和一个 OK 按钮的警告框。<br>
* @author Aaron.ffp
* @version V1.0.0: ffp-demo ffp.demo.webdriver DemoAlert.java, 2016-09-16 10:41:02.118 Exp $
*/
public class DemoAlert {
String url = "http://localhost:8080/ffp-demo/res/main/webdriver/DemoAlert.html"; private WebDriver driver = new ChromeDriver(); @Test
public void test_alert() {
this.driver.manage().window().maximize();
this.driver.get(this.url); this.driver.findElement(By.xpath("//input[@class='alert']")).click();
Alert alert = this.driver.switchTo().alert(); System.out.println(alert.getText()); alert.accept(); this.driver.quit(); }
}

DemoConfirm.java 源码如下所示:

 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2016 All Rights Reserved.
*/
package ffp.demo.webdriver; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.os.WindowsUtils;
import org.testng.annotations.Test; import com.google.gson.annotations.Until; import ffp.demo.core.TestCaseBase; /**
* <strong>元素操作实例-确认框(确认、取消)</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: ffp-demo ffp.demo.webdriver DemoConfirm.java, 2016-09-16 10:41:45.227 Exp $
*/
public class DemoConfirm extends TestCaseBase {
String url = "http://localhost:8080/ffp-demo/res/main/webdriver/DemoConfirm.html"; private WebDriver driver = new ChromeDriver(); @Test
public void test_alert() {
this.driver.manage().window().maximize();
this.driver.get(this.url); this.driver.findElement(By.xpath("//input[@class='alert']")).click();
Alert alert = this.driver.switchTo().alert(); System.out.println(alert.getText()); alert.accept();
// alert.dismiss(); System.out.println(this.driver.getPageSource().toString()); this.driver.quit(); }
}

DemoPrompt.java 源码如下所示:

 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2016 All Rights Reserved.
*/
package ffp.demo.webdriver; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.os.WindowsUtils;
import org.testng.annotations.Test; /**
* <strong>元素操作实例-输入提示框</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: ffp-demo ffp.demo.webdriver DemoPrompt.java, 2016-09-16 10:44:16.638 Exp $
*/
public class DemoPrompt {
String url = "http://localhost:8080/ffp-demo/res/main/webdriver/DemoPrompt.html"; private WebDriver driver = new ChromeDriver(); @Test
public void test_prompt() throws InterruptedException {
this.driver.manage().window().maximize();
this.driver.get(this.url); this.driver.findElement(By.xpath("//input[@class='alert']")).click();
Alert prompt = this.driver.switchTo().alert(); System.out.println(prompt.getText());
prompt.sendKeys("Selenium3启动Firefox需要geckodriver.exe"); Thread.sleep(5000); prompt.accept(); // alert.dismiss(); Thread.sleep(5000); System.out.println(this.driver.getPageSource().toString()); WindowsUtils.killByName("chromedriver_x86_2.24.exe");
}
}

相应的脚本源码文件分享链接:https://yunpan.cn/ckua8gdW5fKYQ  访问密码 e5b2

至此,Selenium2学习-040-JavaScript弹出框(alert、confirm、prompt)操作演示实例 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

上一篇:Selenium2学习-014-WebUI自动化实战实例-012-Selenium 操作下拉列表实例-div+{js|jquery}


下一篇:Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力