【java+selenium3】隐式等待+显式等待(七)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

【java+selenium3】隐式等待+显式等待(七)
⼀、隐式等待 -- implicitlyWait
调⽤⽅式:driver.manage().timeouts().implicitlyWait(long time, TimeUnit unit);
//隐式等待调⽤⽅式,5秒+时间单位(枚举类型)
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
注意:
1.隐式等待只能作⽤于元素的等待。

2.智能等待,如果元素在指定的时间内找到,则不会继续等待,否则在指定时间内未找到元素则抛出NoSuchElementException。

3.作⽤域是全局的,跟driver的⽣命周期⼀样,⼀般定义在⽗类中,只要设置隐式等待后,页⾯所有的元素都会被绑定这种等待机制,只需设置⼀次,全局有效(只作⽤于元素),直到driver实例被关闭!
⼆、显式等待 -- WebDriverWait(显式等待)
//调⽤⽅式
WebDriverWait wait = new WebDriverWait();
WebElement element = wait.until(ExpectedConditions.xxx)
注意:
1.除了作⽤于元素等待还可以实现各种场景的等待,例如页⾯加载等!
2.智能的等待⽅式,元素在指定的时间内找到,则不会继续等待,否则抛出TimeOutException.
3.⾮全局设置,可以针对不同的元素绑定不同的等待机制,推荐优先使⽤这⼀种⽅式.
4.除了可以等待元素的查找,还可以⽀持多种其他的等待场景(例如:当加载页⾯的url出现预期值,具体参见ExceptedConditions类的⽅法)
@Test
public void test2() {
driver.get("");
long startTime = System.currentTimeMillis();
try {
//程序⼀共等待5s,默认0.5秒检查⼀次元素是否加载完成
WebDriverWait wait = new WebDriverWait(driver, 5);
//until⽅法,返回⼀个Boolean类型,判断元素现在是否存在在页⾯上。

locator的元素如果可见就停⽌等待,如果不可见就继续等待直到超过规定的时间后,报超时异常; WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("kw")));
element.sendKeys("⾃动化测试!");
} catch (Exception e) {
e.printStackTrace();
}
long overTime = System.currentTimeMillis();
System.out.println("等待时间:"+(overTime-startTime)/1000+"秒");
}
如果在设置时间5s内找到元素则执⾏通过,如果未找到元素则抛异常TimeoutException
三、显式等待中ExpectedConditions ⽤法说明
//程序⼀共等待5s,默认0.5秒检查⼀次元素是否加载完成
WebDriverWait wait = new WebDriverWait(driver, 5);
//until⽅法,返回⼀个Boolean类型,判断元素现在是否存在在页⾯上。

locator的元素如果可见就停⽌等待,如果不可见就继续等待直到超过规定的时间后,报超时异常WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("w")));
1.判断当前页⾯的title是否精确等于预期
titleIs( String title)
2.判断当前页⾯的title是否包含预期字符串
titleContains( String title)
3.判断当前页⾯的url是否精确等于预期
urlToBe( String url)
4.判断当前页⾯的url是否包含预期字符串
urlContains( String fraction)
5.字符串正则表达式
urlMatches( String regex)
6.判断元素是否出现,只要有⼀个元素出现,就通过。

(代表在不代表可见)
判断是否⾄少有 1 个元素存在于 dom 树中。

举个例⼦,如果页⾯上有 n 个元素的 class 都是’column-md-3’,那么只要有 1 个元素存在,这个⽅法就返回 True。

presenceOfElementLocated( By locator)
7.判断元素是否出现,必须所有符合条件的元素都加载出来,才通过。

presenceOfElementsLocated( By locator)
8.如果给定元素是可见的且具有⾮零⼤⼩,否则为null
elementIfVisible(WebElement element)
9.判断元素是否出现。

presenceOfAllElementsLocatedBy( By locator)
10.传⼊类型为:locator , 判断某个元素是否可见. 可见代表元素⾮隐藏,并且元素宽和⾼都不等于 0
visibilityOfElementLocated( By locator)
11.判断某组元素是否可见
visibilityOfAllElementsLocatedBy( By locator)
12.传⼊类型为:element
 判断某个元素是否可见. 可见代表元素⾮隐藏,并且元素宽和⾼都不等于 0
visibilityOfAllElements(final List<WebElement> elements)
13.判断某个元素中的text是否包含了预期的字符串;
textToBePresentInElement( WebElement element, String text)
14.判断某个元素中的 text 是否包含了预期的字符串
textToBePresentInElement(By locator, String text)
15.判断某个元素中的 text 是否包含了预期的字符串
textToBePresentInElementLocated(final By locator, final String text)
16.判断某个元素中的 value 属性是否包含了预期的字符串
textToBePresentInElementValue( WebElement element, String text)
17.判断某个元素中的 value 属性是否包含了预期的字符串
textToBePresentInElementValue(final By locator, final String text)
18.判断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False
frameToBeAvailableAndSwitchToIt(final String frameLocator)
19.断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False
frameToBeAvailableAndSwitchToIt(final By locator)
20.某个元素中是否不存在于dom树或不可见;
invisibilityOfElementLocated(final By locator)
21.判断带有⽂本的元素要么不可见,要么⽂本不存在于元素上
invisibilityOfElementWithText(final By locator, final String text)
22.判断某个元素中是否可见并且是enable的,这样的话才叫clickable;elementToBeClickable(final By locator)
23.判断某个元素中是否可见并且是enable的,这样的话才叫clickable;elementToBeClickable(final WebElement element)
24.判断⼀个元素是否仍在DOM中,传⼊WebElement对象,可以判断页⾯是否刷新了。

stalenessOf(final WebElement element)
25.判断⼀个元素是否仍在DOM中,传⼊WebElement对象,可以判断页⾯是否刷新了。

refreshed(final ExpectedCondition<T> condition)
26.页⾯元素处于被选中状态
elementToBeSelected(WebElement element)
27.判断某个元素的选中状态是否符合预期,传⼊element
elementSelectionStateToBe( WebElement element, boolean selected)
28.判断某个元素是否被选中了,⼀般⽤在下拉列表;
elementToBeSelected(By locator)
29.判断某个元素的选中状态是否符合预期,传⼊locator
elementSelectionStateToBe(final By locator, final boolean selected)
30.判断页⾯上是否存在alert。

alertIsPresent()
注意: 从原⽂档⼀个个扒出来翻译的,有不对的地⽅还请批评指正。

参考原地址:。

相关文档
最新文档