Selenium学习(五) selenium IDE命令之 prompt相关详解

目录

prompt相关方法

官方API

方法简介

验证过程

扩展


prompt相关方法

1、assert prompt
2、answer on next prompt
3、choose cancel on next prompt
4、webdriver answer on visible prompt
5、webdriver choose cancel on visible prompt

 

官方API

answer on next prompt

Affects the next alert prompt. This command will send the specified answer string to it. If the alert is already present, then use "webdriver answer on visible prompt" instead.

arguments

  • answer: The answer to give in response to the prompt pop-up.

 

assert prompt

Confirm that a JavaScript prompt has been rendered. The test will stop if the assert fails.

arguments

  • text: The text to use.

 

choose cancel on next prompt

Affects the next alert prompt. This command will cancel it. If the alert is already present, then use "webdriver choose cancel on visible prompt" instead.

 

webdriver answer on visible prompt

Affects a currently showing alert prompt. This command instructs Selenium to provide the specified answer to it. If the alert has not appeared yet then use "answer on next prompt" instead.

arguments

  • answer: The answer to give in response to the prompt pop-up.

 

webdriver choose cancel on visible prompt

Affects a currently showing alert prompt. This command instructs Selenium to cancel it. If the alert has not appeared yet then use "choose cancel on next prompt" instead.

 

方法简介

1、assert prompt
断言对话框
2、answer on next prompt
回复最下一个对话框(下一个对话框选择确定)
3、choose cancel on next prompt
选择取消最下一个对话框(下一个对话框选择取消)
4、webdriver answer on visible prompt
当前可见的对话框选择确定
5、webdriver choose cancel on visible prompt
当前可见的对话框选择取消

 

验证过程

测试页面代码index.html

<!DOCTYPE html>
<html>
	<head>
	<title>selenium 学习</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
</style>
<script type="text/javascript">
	function myFunction(){
		var x;
		var person=prompt("请输入你的名字","Harry Potter");
		if (person!=null && person!=""){
			x="你好 " + person + "! 今天感觉如何?";
			document.getElementById("demo").innerHTML=x;
		}
		else{
			x="收到了cancle,呜呜呜~~";
			document.getElementById("demo").innerHTML=x;
		}
	}
</script>
</head>

 <h1>selenium test </h1>
<div id="main">
	   1、点击触发prompt!    
	   <input type="button" onclick="myFunction()" value="显示prompt" />
	   <p id="demo"></p>
		   
</div>
</body>
</html>

selenium脚本:

结论:

1、assert prompt
解析:判断当前界面有无渲染过prompt,参数是该prompt的标题,非对话框内容,如果无渲染过或者参数(标题)对不上,则失败。
2、answer on next prompt
解析:界面有prompt对话框的时候,可以提前设置,设置后当有prompt对话框的时候,立即会选择确定,并传递prompt对话框参数。
3、choose cancel on next prompt
解析:与answer on next prompt相反,可以提前设置,设置后当有prompt对话框的时候,立即会选择取消,并传递prompt对话框参数。

 

以下两个方法试验中失败:

4、webdriver answer on visible prompt
释义:理论上应该是当前对话框选择确定
5、webdriver choose cancel on visible prompt
释义:理论上应该是当前对话框选择取消

 

失败的两个方法进一步验证

原有测试用例中,测试4 5两个方法无任何效果,总是返回ok,觉得有问题,故新建测试用例,打开百度首页后直接运行该方法,结果还是返回ok...

4 5两个方法都是,和官网API,以及中文翻译的有差异,执行过程中,执行到界面prompt处会停止,无法自动执行4 5两个方法,最终试验以失败告终。
另, 4 5两个命令均是已webdriver开头的,与其他方法稍有不同,不太确定是否设计者在设计的时候,还是别有用意,也许是只有webdriver的方式下才可以使用。
如果有人知道这两个方法的用法,以及本文章中错漏之处,望指教。如果后续有时间折腾出来,会完善本文章。

 

扩展

1、一个prompt有多个预设的时候,默认获得最后预设的answer或cannel;
2、一个界面,多个prompt如果要预设,则需要在上一个prompt预设结果执行完成以后再次预设,一个界面无法预设多次,有效的预设只有一个;
3、给的index.html代码,名字无所谓,但需要放在本地tomcat下,静态界面无法执行用例的,具体放的方法,比较简单,可以直接百度的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值