Java+Selenium显式等待常用函数之ExpectedConditions

通常配合public WebDriverWait(WebDriver driver, Duration timeout)和public V until(Function<? super T, V> isTrue)使用
ExpectedConditions类均返回ExpectedCondition对象,ExpectedCondition继承Function

检查页面title是否是指定字符串

public static ExpectedCondition<Boolean> titleIs(final String title) 
title:预期的标题,必须完全匹配.
示例:5秒内等待页面title成为“百度一下,你就知道”,否则抛出异常。
ExpectedCondition<Boolean> b = ExpectedConditions.titleIs("百度一下,你就知道");
new WebDriverWait(driver, Duration.ofSeconds(5)).until(b);

检查页面title是否包含指定字符串

public static ExpectedCondition<Boolean> titleContains(final String title)
title:包含的字符串
示例:5秒内等待页面title包含“百度一下”,否则抛出异常。
ExpectedCondition<Boolean> b = ExpectedConditions. titleContains ("百度一下");
new WebDriverWait(driver, Duration.ofSeconds(5)).until(b);

检查当前页面URL是否为指定URL

public static ExpectedCondition<Boolean> urlToBe(final String url)

检查当前页面URL包含指定字符串文本

public static ExpectedCondition<Boolean> urlContains(final String fraction)

检查当前页面URL是否匹配指定正则表达式

public static ExpectedCondition<Boolean> urlMatches(final String regex)

检查页面上是否存在指定元素(不一定可见)(By locator)

public static ExpectedCondition<WebElement> presenceOfElementLocated(final By locator)
示例:5秒内判断页面是否存在指定元素,不存在则抛出异常
By locator = By.name("name") ;
ExpectedCondition<WebElement>  w = ExpectedConditions. presenceOfElementLocated(locator)
new WebDriverWait(driver, Duration.ofSeconds(5)).until(w);

检查页面上是否存在指定元素,且元素是可见的(By locator)

public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator)

检查页面上所有与定位器匹配的元素存在且可见(By locator)

public static ExpectedCondition<List<WebElement>> visibilityOfAllElementsLocatedBy(final By locator)
示例:
By locator = By.name("id") ;
ExpectedCondition<List<WebElement>> ws= ExpectedConditions.visibilityOfAllElementsLocatedBy(locator) ;
new WebDriverWait(driver, Duration.ofSeconds(5)).until(ws);

检查页面上所有指定的元素存在且可见(WebElement… elements)

public static ExpectedCondition<List<WebElement>> visibilityOfAllElements(final WebElement... elements)
public static ExpectedCondition<List<WebElement>> visibilityOfAllElements(final List<WebElement> elements)
示例:
List<WebElement> elements = driver.findElements(By.id("id")) ;
ExpectedCondition<List<WebElement>> lw = ExpectedConditions. visibilityOfAllElements(elements) ;
WebElement element = driver.findElement(By.name("name"));
WebElement element1 = driver.findElement(By.name("name"));
ExpectedCondition<List<WebElement>> lw = ExpectedConditions.visibilityOfAllElements(element1,element) ;
new WebDriverWait(driver, Duration.ofSeconds(5)).until(lw);

检查页面上指定的元素存在且可见(WebElement element)

public static ExpectedCondition<WebElement> visibilityOf(final WebElement element)
示例:
WebElement element = driver.findElement(By.id("id")) ;
ExpectedCondition< WebElement> lw = ExpectedConditions.visibilityOf(element) ;
new WebDriverWait(driver, Duration.ofSeconds(5)).until(lw);

检查页面上是否存在至少一个满足定位的元素(By locator)

public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy(final By locator)
示例:
By locator = By.name("id") ;
ExpectedCondition<List<WebElement>> lw = ExpectedConditions. presenceOfAllElementsLocatedBy (locator) ;
new WebDriverWait(driver, Duration.ofSeconds(5)).until(lw);

检查指定元素的文本值是否与指定字符串一致(常用于等待同一元素文本值变更)

public static ExpectedCondition<Boolean> textToBePresentInElement(final WebElement element,final String text)

# 检查指定定位的文本值是否与指定字符串一致
public static ExpectedCondition<Boolean> textToBePresentInElementLocated(final By locator,final String text)

检查指定元素的‘value’是否与指定字符串一致

public static ExpectedCondition<Boolean> textToBePresentInElementValue(final WebElement element,final String text)

检查指定定位的‘value’是否与指定字符串一致

public static ExpectedCondition<Boolean> textToBePresentInElementValue(final By locator,final String text)

检查是否可切换至指定的iframe,如果可以,则完成切换

public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final String frameLocator)
frameLocator:id或name

public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final By locator)

public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final int frameLocator)
frameLocator:index索引

public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final WebElement frameLocator)

检查元素在页面上不可见或不存在

public static ExpectedCondition<Boolean> invisibilityOfElementLocated(final By locator)

检查页面元素是可见的,并且是可点击的

public static ExpectedCondition<WebElement> elementToBeClickable(final By locator)
public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element)

检查页面元素是否被选中

public static ExpectedCondition<Boolean> elementToBeSelected(final WebElement element)

检查页面指定定位元素的指定属性有特定的值

public static ExpectedCondition<Boolean> attributeToBe(final By locator,final String attribute,final String value)
public static ExpectedCondition<Boolean> attributeToBe(final WebElement element,final String attribute,final String value)
attribute:属性名称
value:指定的属性值

检查页面指定定位元素有指定的文本

public static ExpectedCondition<Boolean> textToBe(final By locator, final String value)

检查页面指定元素的指定属性包含特定的字符串

public static ExpectedCondition<Boolean> attributeContains(final WebElement element,final String attribute,final String value)
public static ExpectedCondition<Boolean> attributeContains(final By locator,final String attribute,final String value)

检查指定元素的指定属性值非空

public static ExpectedCondition<Boolean> attributeToBeNotEmpty(final WebElement element,final String attribute)

检查给定的所有元素存在但不可见(多个元素)

public static ExpectedCondition<Boolean> invisibilityOfAllElements(final WebElement... elements)
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements)

检查指定元素不可见(单个元素)

public static ExpectedCondition<Boolean> invisibilityOf(final WebElement element)
posted @ 2022-07-23 14:39  明月,  阅读(235)  评论(0编辑  收藏  举报