极简主义 Selenide(二)

本贴最后更新于 976 天前,其中的信息可能已经时移世易

1、Selenide核心类com.codeborne.selenide.Condition API介绍

断言条件是使用should/ shouldNot/ waitUntil/waitWhile构造。建议静态导入更加简单:

visible / appear // 可见 e.g. $(“input”).shouldBe(visible)
present / exist // 存在 conditions to wait for element existence in DOM 
hidden / disappear // 不可见 hidden = not(visible)
readonly // 只读属性 e.g. $(“input”).shouldBe(readonly)
name // name属性 e.g. $(“input”).shouldHave(name(“fname”))
value // value属性 e.g. $(“input”).shouldHave(value(“John”))
type // type属性 e.g. $(“#input”).shouldHave(type(“checkbox”))
id // id属性 e.g. $(“#input”).shouldHave(id(“myForm”))
empty //value和text为空 e.g. $(“h2”).shouldBe(empty)
attribute(name) //某个属性 // e.g. $(“#input”).shouldHave(attribute(“required”))
attribute(name, value) //某个属性值 // e.g. $(“#list li”).shouldHave(attribute(“class”, “active checked”))
cssClass(String) // css e.g. $(“#list li”).shouldHave(cssClass(“checked”))
focused
enabled 
disabled
selected
matchText(String regex)
text(String substring)
exactText(String wholeText)
textCaseSensitive(String substring)
exactTextCaseSensitive(String wholeText)

2、Selenide核心类com.codeborne.selenide.Selectors API介绍

该类包含一些By通过文本或属性定位元素的选择器

byText - 按精确文本搜索元素
withText - 按包含的文本子字符串搜索元素
by(attributeName, attributeValue) - 按属性的名称和值搜索
byTitle - 按属性title搜索
byValue - 按属性搜索
Xpath  - xpath搜索
等等
  
// Examples:
$(byText("Login")).shouldBe(visible));
$(byXpath("//div[text()='Login']")).shouldBe(visible);  

3、Selenide核心类com.codeborne.selenide.ElementsCollection API介绍

找到页面上元素集合类,通常ElementsCollection类的对象可以通过$$方法获取。

3.1、断言
shouldBe - e.g. $$(".errors").shouldBe(empty)
shouldHave - e.g. $$("#mytable tbody tr").shouldHave(size(2))

断言也扮演显式等待的角色,等待状态下(例如size(2),empty,texts("a", "b", "c"))直到超时达到(Configuration.collectionsTimeout默认值6000毫秒)。

3.2、获取元素集合的状态和属性的方法
size()
isEmpty()
getTexts() // 返回可见元素集合文本的数组,e.g. for elements: <li>a</li><li hidden>b</li><li>c</li> will return the array ["a", "", "c"]
3.3、特定集合元素的方法选择器
filterBy(Condition)  过滤条件例如$$("#multirowTable tr").filterBy(text("Norris"))
excludeWith(Condition)  包含条件例如$$("#multirowTable tr").excludeWith(text("Chuck"))
get(int) - 返回第 n 个元素
findBy(Condition) - 返回第一个满足条件的元素

4、Selenide核心类com.codeborne.selenide.CollectionCondition API介绍

ElementsCollection断言条件类,在shouldBe/shouldHave 等方法中使用。

empty // e.g. $$("#list li").shouldBe(empty)
size(int) // e.g. $$("#list li").shouldHave(size(10))
sizeGreaterThan(int)
sizeGreaterThanOrEqual(int)
sizeLessThan(int)
sizeLessThanOrEqual(int)
sizeNotEqual(int)
texts(String... substrings)
exactTexts(String... wholeTexts)

5、Selenide核心类com.codeborne.selenide.WebDriverRunner API介绍

这个类定义了一些浏览器管理方法:

isChrome()
isFirefox()
isHeadless()
url() - 返回当前 URL
source() - 返回当前页面的源 HTML 代码
getWebDriver() - 返回 WebDriver 实例
setWebDriver(WebDriver) - 告诉 Selenide 使用用户创建的驱动程序从这一刻起用户自己负责关闭驱动程序通过调用getWebDriver().quit())。

6、Selenide核心类com.codeborne.selenide.Configuration API介绍

这个类包含不同的配置选项来配置基于 Selenide 的测试的执行,例如:

timeoutSelenideElement等待超时显式的使用should/ shouldNot/ waitUntil/ waitWhile和隐式等待 默认设置为 4000 毫秒可以修改例如Configuration.timeout = 6000;
collectionsTimeoutElementsCollection等待超时即在显式的使用should/ shouldNot/ waitUntil/ waitWhile和隐式等待; 默认设置为 6000 毫秒可以修改例如Configuration.collectionsTimeout = 8000;
browser:(例如"chrome", "ie", "firefox")
回帖
请输入回帖内容 ...