SeleniumLibrary4.5.0 关键字详解(一)

SeleniumLibrary4.5.0 关键字详解(一)
库版本:4.5.0
库范围:全局
命名参数:受支持

简介

SeleniumLibrary是Robot Framework的Web测试库。

本文档说明了如何使用SeleniumLibrary提供的关键字。 有关安装,支持等信息,请参见 python3.9.0 + robotframework + selenium3 实例体验。

有关robotframework框架的更多信息,请参见 https://blog.csdn.net/mask5726/category_10537277.html。

SeleniumLibrary在内部使用Selenium WebDriver模块来控制Web浏览器。 有关常规Selenium的更多信息,请参见Selenium。

内容列表

1、元素定位
2、浏览器和窗口
3、超时,等待和延迟
4、运行故障功能
5、布尔参数
6、webDriver事件
7、线程支持
8、插件
9、引入
11、关键字

定位元素

SeleniumLibrary中所有需要与网页上的元素进行交互的关键字都带有一个通常称为locator的参数,该参数指定如何查找该元素。通常,定位器是使用下面描述的定位器语法以字符串形式给出的,但是也可以使用WebElements。

定位器语法

SeleniumLibrary支持基于不同策略(例如元素ID,XPath表达式或CSS选择器)查找元素。可以使用前缀明确指定策略,也可以隐式指定策略。

默认定位器策略

默认情况下,定位器被认为使用关键字特定的默认定位器策略。所有关键字都支持基于id和name属性的查找元素,但是某些关键字支持在上下文中有意义的其他属性或其他值。例如,单击链接支持href属性,链接文本以及常规ID和名称的添加。

例子:

Click Element example #根据ID或名称进行匹配.
Click Link example # 根据链接文本和href进行匹配.
Click Button example # 根据ID,名称或值进行匹配.
如果定位器意外地以被识别为显式定位器策略或隐式XPath策略的前缀开头,则可以使用显式默认前缀来启用默认策略。

例子:

Click Element name:foo # 查找名称为foo的元素.
Click Element default:name:foo # 使用默认策略,其值为name:foo.
Click Element //foo # 使用XPath // foo查找元素.
Click Element default: //foo # 使用默认策略使用值// foo.
使用语法strategy:value或strategy = value使用前缀指定显式定位器策略。首选前一种语法,因为后者与Robot Framework的命名参数语法相同,并且可能导致问题。分隔符周围的空格将被忽略,因此id:foo,id:foo和id:foo都是等效的。

下表列出了默认支持的定位器策略。除它们之外,还可以注册自定义定位器。

基于示例的策略匹配

Strategy Match based on Example
id Element id. id:example
name name attribute. name:example
identifier Either id or name. identifier:example
class Element class. class:example
tag Tag name. tag:div
xpath XPath expression. xpath://div[@id=“example”]
css CSS selector. css:div#example
dom DOM expression. dom:document.images[5]
link Exact text a link has. link:The example
partial link Partial link text. partial link:he ex
sizzle Sizzle selector deprecated. sizzle:div.example
jquery jQuery expression. jquery:div.example
default Keyword specific default behavior. default:example

有关默认策略如何工作的更多信息,请参见下面的默认定位器策略部分。仅当定位器值本身偶然与某些显式策略匹配时,才需要使用显式默认前缀。

不同的定位器策略有不同的优缺点。建议尽可能使用id(显式地像id:foo一样)或使用默认的定位器策略(像foo一样),因为它的语法很简单,并且对于浏览器来说,通过id定位元素很快。如果元素没有ID或ID不稳定,则需要使用其他解决方案。如果元素具有唯一的标签名称或类,则使用标签,类或css策略(例如tag:h1,class:example或css:h1.example)通常是一个简单的解决方案。在更复杂的情况下,使用XPath表达式通常是最好的方法。它们非常强大,但缺点是它们也可能变得复杂。

例子:

Click Element id:foo # Element with id ‘foo’.
Click Element css:div#foo h1 # h1 element under div with id ‘foo’.
Click Element xpath: //div[@id=“foo”]//h1 # Same as the above using XPath, not CSS.
Click Element xpath: //*[contains(text(), “example”)] # Element containing text ‘example’.
SeleniumLibrary 3.0和更高版本仅支持strategy:value语法。
使用sizzle策略或其别名jquery要求被测系统包含jQuery库。
在SeleniumLibrary 3.0之前,与表相关的关键字仅支持xpath,css和sizzle / jquery策略。
隐式XPath策略
如果定位符以//或(//开头,则该定位符被视为XPath表达式。换句话说,使用// div等效于使用显式xpath:// div。

例子:

Click Element //div[@id=“foo”]//h1
Click Element (//div)[2]

SeleniumLibrary 3.0中新增了对(//前缀的支持。

使用WebElements

除了将定位符指定为字符串之外,还可以使用Selenium的WebElement对象。这要求首先获取一个WebElement,例如,通过使用Get WebElement关键字。

${elem} = Get WebElement id:example
Click Element ${elem}
自定义定位器

如果所需的查找比通过默认定位器提供的查找更为复杂,则可以创建自定义查找策略。使用自定义定位器是一个分为两部分的过程。首先,创建一个关键字,该关键字返回应执行的WebElement:

Custom Locator Strategy [Arguments] ${browser} ${locator} ${tag} ${constraints}
e l e m e n t = E x e c u t e J a v a s c r i p t r e t u r n w i n d o w . d o c u m e n t . g e t E l e m e n t B y I d ( ′ {element}= Execute Javascript return window.document.getElementById(' element=ExecuteJavascriptreturnwindow.document.getElementById({locator}’);
[Return] ${element}

此关键字是对ID定位器基本功能的重新实现,其中$ {browser}是对WebDriver实例的引用,而$ {locator}是定位器策略的名称。要使用此定位器,必须首先使用“添加位置策略”关键字进行注册:

Add Location Strategy    custom    Custom Locator Strategy

“添加位置策略”的第一个参数指定策略的名称,并且该名称必须唯一。注册该策略后,其用法与其他定位符相同:

Click Element    custom:example

有关更多详细信息,请参见添加位置策略关键字。



作者:马克社区何老师