Selenium (Java):同时检查页面是否正确加载
Selenium (Java): Do both positive AND negative check whether page is loaded correctly
在 Selenium 中,有标准检查页面是否在技术上正确加载。例如:
@FindBy(id = "loginButton")
private WebElement loginButton;
@Override
public void assertLoadedTechnicallyCorrect() {
WebDriverWait wait = waitWithTimeoutSeconds(10);
wait.until(ExpectedConditions.elementToBeClickable(loginButton));
}
问题:无论页面加载与否,您通常不必等待 10 秒,因为带有错误消息(我们称之为 alertMessage)的不同页面加载速度要快得多。在这种情况下,页面上将没有登录按钮。
所以预期的行为是:
- 等待直到 loginButton 或 alertMessage 可点击(当然,对于 alertMessage 显示就足够了,但它也是可点击的)。
- 如果是 loginButton,继续测试用例。
- 如果是 alertMessage,停止并抛出错误信息。
已在 Selenium Expected Conditions - possible to use 'or'?
下提出并回答了类似的问题
但是,artfulrobot 提出的定义“AnyEc”的解决方案 class 并不以相同的方式工作,因为我需要针对不同情况的相反行为 - 不相同的行为。
有几种方法可以解决这个问题。
如果是这样你可以使用。
driverWait.until(ExpectedConditions.or(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")),
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything"))));
然后分别检查显示的是哪个元素
if(driver.findelements(By.xpath("loginbutton")).size>0)
//do loginbutton code part
else if (driver.findelements(By.xpath("error message")).size>0)
//do error part
在 Selenium 中,有标准检查页面是否在技术上正确加载。例如:
@FindBy(id = "loginButton")
private WebElement loginButton;
@Override
public void assertLoadedTechnicallyCorrect() {
WebDriverWait wait = waitWithTimeoutSeconds(10);
wait.until(ExpectedConditions.elementToBeClickable(loginButton));
}
问题:无论页面加载与否,您通常不必等待 10 秒,因为带有错误消息(我们称之为 alertMessage)的不同页面加载速度要快得多。在这种情况下,页面上将没有登录按钮。
所以预期的行为是:
- 等待直到 loginButton 或 alertMessage 可点击(当然,对于 alertMessage 显示就足够了,但它也是可点击的)。
- 如果是 loginButton,继续测试用例。
- 如果是 alertMessage,停止并抛出错误信息。
已在 Selenium Expected Conditions - possible to use 'or'?
下提出并回答了类似的问题但是,artfulrobot 提出的定义“AnyEc”的解决方案 class 并不以相同的方式工作,因为我需要针对不同情况的相反行为 - 不相同的行为。
有几种方法可以解决这个问题。
如果是这样你可以使用。
driverWait.until(ExpectedConditions.or(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")),
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything"))));
然后分别检查显示的是哪个元素
if(driver.findelements(By.xpath("loginbutton")).size>0)
//do loginbutton code part
else if (driver.findelements(By.xpath("error message")).size>0)
//do error part