首页 > 语言 > JavaScript > 正文

JavaScript在web自动化测试中的作用示例详解

2024-05-06 15:35:31
字体:
来源:转载
供稿:网友

前言

JS的全称JavaScript,是一种运行在浏览器中的解释型脚本语言,通常用来实现web前端页面的基本功能,对于前端开发人员是不得不掌握的一门基本技能,但是对于做web自动化测试的人员来说,如果为了实施自动化测试专门研究JS的脚本语法不仅浪费时间,也偏离了我们的工作重心,所以今天就给大家总结一下,在web自动化测试中常用的一些JS脚本,只要掌握这些脚本的使用,无需再为专门学习js脚本而花费太多时间,优秀程序员的素质是什么?有现成的直接用,绝不浪费时间自己写!^_^ 开玩笑的,俗话说技多不压身,多掌握一门技能,只有好处没坏处。正文开始!

窗口滚动

用途:滑动web页面

def scrollTo(x, y): js = """ window.scrollTo("{x}", "{y}") """.format(x=x, y=y) driver.execute_script(js)

参数说明

x:屏幕向右移动的距离

y:屏幕向下移动的距离

移除属性

用途:以下方法可以删除元素的任何属性,主要用来移除时间控件的readonly属性

def remove_attribute(css, attribute, index=0): js = """ var element = document.querySelectorAll("{css}")[{index}];  element.removeAttribute("{attr}"); """.format(css=css, index=index, attr=attribute) driver.execute_script(js)

参数说明

css::css表达式

index:索引值,默认0,标识第一个元素

attribute:元素的某个属性,比如readonly,value,name等

高亮元素

用途:方便用户查看当前操作的是哪个页面元素,也方便测试人员定位问题

def height_light(css, index=0): js = """ var element = document.querySelectorAll("{css}")[{index}];  element.style.border="2px solid red"; """.format(css=css, index=index) driver.execute_script(js)

参数说明

css:css表达式

index:索引值,默认0,标识第一个元素

点击元素

用途:由于web自动化的最大问题就是稳定性比较差,有些时候使用selenium无法点击元素,因此我们可以使用JS实现元素的点击操作

def click(css, index=0): js = """var element = document.querySelectorAll("{css}")[{index}];    element.click();""".format(css=css, index=index) driver.execute_script(js)

参数说明

css:css表达式

index:索引值,默认0,标识第一个元素

清除输入框内容

用途:用来清除输入框的内容

def clear(css, index=0): js = """var element = document.querySelectorAll("{css}")[{index}];    element.value = "";""".format(css=css, index=index) driver.execute_script(js)

参数说明

css:css表达式

index:索引值,默认0,标识第一个元素

输入内容

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选