弹出框是网页自动化测试常见得操作页面元素之一,常见的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学习-040-JavaScript弹出框(alert、confirm、prompt)操作演示实例的更多相关文章

  1. 练习:javascript弹出框及地址选择功能,可拖拽

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. selenium浏览器弹出框alert 操作

    1.简介 在WebDriver中要处理JS生成的alert.confirm以及prompt,需要 switch_to.alert() 来选取(定位)警告弹窗,在对弹窗进行关闭.输入等信息操作. 2.操 ...

  3. JavaScript 弹出框

    JavaScript 有三种类型的弹出框:警告框.确认框和提示框. 警告框 如果要确保信息传递给用户,通常会使用警告框. 当警告框弹出时,用户将需要单击“确定”来继续. 语法 window.alert ...

  4. 自动化测试基础篇--Selenium弹出框alert

    摘自https://www.cnblogs.com/sanzangTst/p/7685304.html   不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认 ...

  5. 弹出框Alert

    selenium提供了三个处理alert的方法 注意:首先需要切换窗口到alert driver.switch_to.alert() (1)点击确定按钮 driver.switch_to.alert. ...

  6. javascript弹出框打印某个数值时,弹出NaN?(not a number)

    一.NaN:表示not a number null 未定义或空字符串 undefined 对象属性不存在 或是声明了变量但从未赋值. 二.出现这种情况有(1)此常数的值是零被零除所得到的结果. (2) ...

  7. bootstrap里面的popover组件如何使鼠标移入可以对弹出框进行一系列的操作

    在bootstrap里面,有一个组件很可爱,它就是popover,它是对标签title属性的优化,奉上连接一枚:http://docs.demo.mschool.cn/components/popov ...

  8. JavaScript弹出框

    confirm(str); 参数说明: str:在消息对话框中要显示的文本 返回值: Boolean值 返回值: 当用户点击"确定"按钮时,返回true 当用户点击"取消 ...

  9. (十)弹出框Alert与ActionSheet

    第一种方式:中间弹窗 从中间弹出的窗口称为AlertView. 可以设置多个按钮,取消按钮会放在对右端或者最下端,按钮超过两个,会竖着排列. UIAlertView *alert = [[[UIAle ...

  10. JavaScript 弹出框:警告(alert)、确认(confirm)的简单写法

    onclick="javascript:return window.confirm('message')"

随机推荐

  1. java学习第一天 回顾以前

    1.1常量: 基本数据类型常量 字符常量 整数常量的表现形式:一进制的形式来表示(二进制,八进制,十进制,十六进制) 生活中:十进制(0-9)  ,星期(七进制(0-6)) ,时间(十二进制(0-11 ...

  2. HTML5本地存储——Web SQL Database

    在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...

  3. 2.Java异常学习

    1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ...

  4. 11g新特性-dba_users安全性的一些增强

    1.dba_user表的password(除了GLOBAL和EXTERNAL的密码)不再保存密码. 查询10g的dba_user表 SQL> select username,password f ...

  5. 【Mybatis框架】查询缓存(二级缓存)

    继上一篇博客,我们讲述了mybatis的一级缓存,接下来,我们来学习一下mybatis的二级缓存 博客链接地址: http://blog.csdn.NET/liweizhong193516/artic ...

  6. js函数封装

    1.随机数 <script> function rnd(n,m){ return parseInt(Math.random()*(m-n)+n); } var a=rnd(45,47); ...

  7. 表单验证——jquery validate使用说明【另一个教程】

    [参考:http://www.tuicool.com/articles/y6fyme] jQuery Validate jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证 ...

  8. 防止 jsp被sql注入的五种方法

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  9. android解析json

    android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...

  10. CSS3绘制404页面

    标题有点噱了...最近在做一个交通有关的项目, 想做一个类似标志牌的404, 所以就有了这个. 只用的CSS3中的旋转, 效果如下 上代码: <!DOCTYPE html> <htm ...