Selenium脚本获取Cookie中的数值
程序员文章站
2022-05-13 18:06:06
...
使用Selenium录制功能测试脚本非常方便,能够减轻测试人员的繁重工作量,针对不同版本录制一下Selenium脚本,对应新的发布版本可以优先使用Selenium脚本验证系统的功能,对于没有通过测试用例再手工检查测试,如果页面元素发生变化必须更新Selenium脚本。
不管是前后端分离项目,还是传统的后台项目,有些数据是存放在Http Cookie中的,而且是动态数据,编写通用Selenium脚本时,需要解析Cookie中的数据作为变量用在后续的Http请求中,比如用户登录页面通常需要输入验证码,验证码是符图片,为了支持自动化测试,需要寻求开发人员帮助,在测试期间将验证码放在Cookie中,Selenium可以从Cookie中解析出验证码,并输入到页面的输入框中,这样测试登录脚本就比较通用,可以作为公共代码使用。
var start=document.cookie.indexOf('vcode=')+'vcode='.length;
var end=document.cookie.indexOf(';',start);
if(end == -1)
return document.cookie.substring(start);
else
return document.cookie.substring(start, end);
{
"id": "fec21223-4086-48c3-8cb0-f3629819b047",
"version": "2.0",
"name": "Test",
"url": "http://localhost:8080",
"tests": [{
"id": "111eb6e9-62dc-4eae-876a-3ecb63340dcf",
"name": "1.Login",
"commands": [{
"id": "369f9098-664c-475a-aa91-a6bcadd588b1",
"comment": "",
"command": "open",
"target": "/",
"targets": [],
"value": ""
}, {
"id": "71061e4a-e439-4530-a443-157fc9445dd2",
"comment": "",
"command": "setWindowSize",
"target": "1768x1200",
"targets": [],
"value": ""
}, {
"id": "94e9b9f0-ecf1-446c-b2db-bd72d40e89e2",
"comment": "",
"command": "type",
"target": "css=.el-form-item:nth-child(1) .el-input__inner",
"targets": [
["css=.el-form-item:nth-child(1) .el-input__inner", "css:finder"],
["xpath=//input[@type='text']", "xpath:attributes"],
["xpath=//div[@id='app']/div/div/div[2]/form/div/div/div/input", "xpath:idRelative"],
["xpath=//input", "xpath:position"]
],
"value": "gf:gfadmin"
}, {
"id": "da9e1c19-92f2-4f0c-8802-e8412239d691",
"comment": "",
"command": "type",
"target": "css=.el-form-item:nth-child(2) .el-input__inner",
"targets": [
["css=.el-form-item:nth-child(2) .el-input__inner", "css:finder"],
["xpath=//input[@type='password']", "xpath:attributes"],
["xpath=//div[@id='app']/div/div/div[2]/form/div[2]/div/div/input", "xpath:idRelative"],
["xpath=//div[2]/div/div/input", "xpath:position"]
],
"value": "1"
}, {
"id": "eb588d76-9e99-41cc-83c2-3f1a19d4d32c",
"comment": "",
"command": "click",
"target": "css=.sys-login",
"targets": [
["css=.sys-login", "css:finder"],
["xpath=//div[@id='app']/div", "xpath:idRelative"],
["xpath=//div/div", "xpath:position"]
],
"value": ""
}, {
"id": "1a2d204e-c3ba-48f7-9d59-68d4bcef28b1",
"comment": "",
"command": "storeValue",
"target": "css=.el-form-item:nth-child(1) .el-input__inner",
"targets": [],
"value": "vcode"
}, {
"id": "c168c39b-38fd-4ee7-a0bd-da03cdbc9db5",
"comment": "",
"command": "executeScript",
"target": "var start=document.cookie.indexOf('vcode=')+'vcode='.length; var end=document.cookie.indexOf(';',start); if(end == -1) \treturn document.cookie.substring(start); else \treturn document.cookie.substring(start, end);",
"targets": [],
"value": "vcode"
}, {
"id": "a3dff630-63bd-43d8-9d4b-b95fea6d2e87",
"comment": "",
"command": "type",
"target": "css=.el-form-item:nth-child(3) .el-input__inner",
"targets": [
["css=.el-form-item:nth-child(3) .el-input__inner", "css:finder"],
["xpath=(//input[@type='text'])[2]", "xpath:attributes"],
["xpath=//div[@id='app']/div/div/div[2]/form/div[3]/div/div/input", "xpath:idRelative"],
["xpath=//div[3]/div/div/input", "xpath:position"]
],
"value": " ${vcode}"
}, {
"id": "3ca4bcd8-ac55-4634-9f23-9ea9dbe6bdc7",
"comment": "",
"command": "click",
"target": "linkText=登录",
"targets": [
["linkText=登录", "linkText"],
["css=.btn-login", "css:finder"],
["xpath=//a[contains(text(),'登录')]", "xpath:link"],
["xpath=//a[@type='primary']", "xpath:attributes"],
["xpath=//div[@id='app']/div/div/div[2]/form/a", "xpath:idRelative"],
["xpath=//a", "xpath:position"],
["xpath=//a[contains(.,'登录')]", "xpath:innerText"]
],
"value": ""
}]
}],
"suites": [{
"id": "2e63b072-297e-41ab-95e9-ac1520b71287",
"name": "Default Suite",
"persistSession": false,
"parallel": false,
"timeout": 300,
"tests": ["111eb6e9-62dc-4eae-876a-3ecb63340dcf"]
}],
"urls": ["http://localhost:8080/"],
"plugins": []
}
上一篇: git commit 更新
下一篇: 用线程实现经典生产者消费者问题