目录

执行脚本命令系列简介

相关方法

官方API

验证过程

命令详解


执行脚本命令系列简介

执行脚本,即在ide中执行js脚本。

 

相关方法

execute script
execute async script
run script

 

官方API


run

Runs a test case from the current project.

arguments


run script

Creates a new "script" tag in the body of the current test window, and adds the specified text into the body of the command. Beware that JS exceptions thrown in these script tags aren't managed by Selenium, so you should probably wrap your script in try/catch blocks if there is any chance that the script will throw an exception.

arguments

  • script: The JavaScript snippet to run.

execute script

Executes a snippet of JavaScript in the context of the currently selected frame or window. The script fragment will be executed as the body of an anonymous function. To store the return value, use the 'return' keyword and provide a variable name in the value input field.

arguments

  • script: The JavaScript snippet to run.

  • variable name: The name of a variable without brackets.


execute async script

Executes an async snippet of JavaScript in the context of the currently selected frame or window. The script fragment will be executed as the body of an anonymous function and must return a Promise. The Promise result will be saved on the variable if you use the 'return' keyword.

arguments

  • script: The JavaScript snippet to run.

  • variable name: The name of a variable without brackets.

 

验证过程

测试界面html

<!DOCTYPE html>
<html>
	<head>
	<title>selenium 学习之assert系列 </title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
</style>
<script type="text/javascript">
	var xx='我是一个全局变量!'
	var globv=0;

	function myAlert(){
		console.log('exe myalert')
		document.getElementById("xxx").innerHTML='xxx';
		alert("你好,我是一个警告框!");
	}

	function alertGlobVar(vv){
		globv++;
		alert('传递的参数是:'+vv+',全局变量目前是第'+globv+'次调用!');
		return globv;
	}


	function updateGlobvToPage(vv){
		globv++;
		document.getElementById("span1").innerHTML='globv='+globv+'   传递的参数是:'+vv;

//throw   "自定义错误"

		return globv;
	}

	function localVar(){
		ss=999;
		return ss;
	}


	function asynFun(){
		let p = new Promise(function(resolve, reject){
			//做一些异步操作
			setTimeout(function(){
				console.log('执行完成Promise');
				resolve('要返回的数据可以任何数据例如接口返回数据');
			}, 2000);
		});
		return p;
	}



</script>
</head>

 <h1>selenium test 之 执行脚本系列 </h1>
<div id="main">
	<input id='jdjjd' type="button" value="点击调用alert" onclick="myAlert()"/>	  
	<span  id="xxx" ></span>
	<br/><br/>	

	show:<span id="span1"></span>
</div>
</body>

</html>

 

 

selenium IDE脚本:(分3个)

第一个测试界面html相关的

第二个,execute script与run script主要区别的:

 

第三个,相关的(run命令相关,见详解)

 

side文件代码

{
  "id": "bc9eec24-28ec-4cd5-bedd-c17277d4d9dc",
  "version": "2.0",
  "name": "exejs",
  "url": "http://localhost:8080/aa/runexe.html",
  "tests": [{
    "id": "ff025ed8-863c-4ebd-85d1-d86a61002fcd",
    "name": "runexe",
    "commands": [{
      "id": "c9879897-ec1a-4944-8de4-82774e26e004",
      "comment": "",
      "command": "open",
      "target": "http://localhost:8080/aa/runexe.html",
      "targets": [],
      "value": ""
    }, {
      "id": "14f99843-e396-41f2-bb2b-8bb377230987",
      "comment": "",
      "command": "runScript",
      "target": "return window.xx=3999",
      "targets": [],
      "value": ""
    }, {
      "id": "88effd3f-0f2e-4701-b95a-e288da8d423c",
      "comment": "",
      "command": "executeScript",
      "target": "return window.xx",
      "targets": [],
      "value": "ss"
    }, {
      "id": "23db1251-e8dc-4c48-b01d-e4f1759c2604",
      "comment": "",
      "command": "//executeScript",
      "target": "return myAlert()",
      "targets": [],
      "value": "ss"
    }, {
      "id": "6b05d3d9-8e86-4f8b-ab18-96d0c6295da7",
      "comment": "",
      "command": "//executeScript",
      "target": "return window.updateGlobvToPage(3)",
      "targets": [],
      "value": "ss"
    }, {
      "id": "08e1c84f-f52c-40a9-baee-87bd5d63debe",
      "comment": "",
      "command": "//executeScript",
      "target": "return localVar()",
      "targets": [],
      "value": "ss"
    }, {
      "id": "e98242f5-0790-4853-9d4f-35979b43e326",
      "comment": "",
      "command": "//executeAsyncScript",
      "target": "return asynFun()",
      "targets": [],
      "value": "ss"
    }, {
      "id": "0e6c22a8-51f6-4296-a3af-7ca5ec44ad3b",
      "comment": "",
      "command": "echo",
      "target": "${ss}",
      "targets": [],
      "value": ""
    }, {
      "id": "7f7dd232-e21a-4762-96ee-5dfec82bd32d",
      "comment": "",
      "command": "//runScript",
      "target": "return window.updateGlobvToPage(3)",
      "targets": [],
      "value": ""
    }, {
      "id": "3ae82989-80a5-4bfb-b5b1-cb2fdc74dbf9",
      "comment": "",
      "command": "echo",
      "target": "${eee}",
      "targets": [],
      "value": ""
    }]
  }, {
    "id": "eb278d1c-9cc1-42b1-a09e-5fa91152bf4f",
    "name": "run2exe",
    "commands": [{
      "id": "7db33495-c452-4de3-beab-c03d2fdb85b2",
      "comment": "",
      "command": "open",
      "target": "http://localhost:8080/ext-6.2.0/examples/classic/aria/",
      "targets": [],
      "value": ""
    }, {
      "id": "4b210595-d2f0-4533-8d26-0ddbdae02155",
      "comment": "",
      "command": "//executeScript",
      "target": "Ext.getCmp('bbbbb').getSelectionModel().select(4,true)",
      "targets": [],
      "value": ""
    }, {
      "id": "4e742b17-01c2-4144-8917-07d60562aa52",
      "comment": "",
      "command": "runScript",
      "target": "Ext.getCmp('bbbbb').getSelectionModel().select(3,true)",
      "targets": [],
      "value": ""
    }]
  }, {
    "id": "4efdadd0-47d4-4646-acb1-299577646cee",
    "name": "runtest",
    "commands": [{
      "id": "e59e1934-bb74-4ead-9182-26016e967d39",
      "comment": "",
      "command": "run",
      "target": "test22",
      "targets": [],
      "value": ""
    }]
  }, {
    "id": "e3418e16-0282-476a-8a17-b24f25de5be1",
    "name": "test22",
    "commands": [{
      "id": "3ec4e733-a9f5-4ffb-97d0-376b50152d85",
      "comment": "",
      "command": "echo",
      "target": "333",
      "targets": [],
      "value": ""
    }]
  }],
  "suites": [{
    "id": "069dfa40-84ae-427b-a4ae-a439987279fe",
    "name": "Default Suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["ff025ed8-863c-4ebd-85d1-d86a61002fcd"]
  }],
  "urls": ["http://jsw.taizhou.gov.cn/index.html", "http://localhost:8080/aa/runexe.html", "http://127.0.0.1:8080/aa/runexe.html", "http://localhost:8080/ext-6.2.0/examples/classic/desktop/index.html", "http://localhost:8080/ext-6.2.0/examples/classic/aria/"],
  "plugins": []
}

 

命令详解

execute script
解析:在当前选定的框架或窗口的上下文中执行JavaScript片段。脚本片段将作为匿名函数的主体执行。要存储返回值,请使用“return”关键字并在值输入字段中提供变量名。(官方api翻译)
注意:
1、参数有两个,可以有返回值,但必须是调用的js部分有返回值,且ide命令部分必须加return(假如调用页面方法test(),要求js的test()方法有return,且ide命令部分必须是return test())
2、可以调用js全局变量,可以调用js方法;
3、调用js变量,变量名对不上会出错,调用方法方法名对不上会出错;
4、调用方法如果方法体和ide部分没有return,则返回undefined;
5、注意,无法执行界面的对话框命令,如alert等命令;(见验证脚本,执行验证结果如此)
6、调用js可以执行变量修改,可以操作界面dom进行操作;

execute async script
解析:在当前选定的框架或窗口的上下文中执行JavaScript的异步片段。脚本片段将作为匿名函数的主体执行,并且必须返回一个Promise。如果使用'return'关键字,Promise结果将保存在变量上。(官方api翻译)
注意:
1、该方法只适用于异步执行的,如ajax、settimeout;
2、返回对象只能是promise,es6的内容,其他对象如普通变量不行;
3、此方法可以调用界面异步处理方法,也就是可以调用后台交互的接口方法,如果后台方法没有权限验证或防御机制,那么此方法具有一定的侵入性;

run script
解析:在当前测试窗口的主体中创建一个新的“script”标记,并将指定的文本添加到命令的主体中。请注意,在这些脚本标记中抛出的JS异常不是由Selenium管理的,因此如果脚本有可能抛出异常,您应该将脚本包装在try/catch块中。
注意:
1、没有返回值;
2、一样可以执行界面的变量修改,界面dom操作;不能调用界面alert对话框等;
3、除了返回参数部分与execute script有明显区别,其他常规使用暂未发现有区别;

run(非执行部分,相关命令,顺便说明)
解析:执行一个用例
注意:
1、参数是用例名,可以是当前用例,也可以是其他用例名,只要在ide中的用例都能执行;
2、用例未找到不报错;


--------
run script与execute script区别
百度到的技术贴内容:
1、getEval(execute script) 有返回值,runScirpt没有返回值
2、getEval 默认环境上下文是Selenium, runScirpt 在当前冲口的正文创建script标签。
如果要是使用window上下文,e.g. : window.document.getElementById(‘foo’)
如果你需要使用一个locater来引用页面上的单个Element。
可以使用 this.browserbot.findElement(“id=foo”)
3、runScript 执行的js 异常不会被 Selenium 处理,所以需要自己添加try/catch。

验证如下
1、execute script有返回值,run script无返回值,此点无问题;
2、上下文的区别没有验证出来,找了一些专门的技术贴验证无区别,见run2exe用例,部署extjs官方用例验证均无发现区别,地址:https://www.cnblogs.com/bugua/archive/2011/12/15/2288399.html,验证结果如下图:

3、未找到相关资料,自己写的throw exception和数字除以0都无发现区别;(此点未深入探索)

补充:
1、对界面生成的html脚本进行检查,未发现ide对页面代码进行侵入,所以run script命令中在当前测试窗口的主体中创建一个新的“script”标记的描述,也不是很明白。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐