我需要测试一个案例,其中我有一个随机的额外登录屏幕出现在应用程序中。
WebDriverWait webDriverWait = new WebDriverWait(driver, 4);
WebElement inputBox = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username.input")));
String textInsideInputBox = inputBox.getAttribute("value");
if(!textInsideInputBox.isEmpty())
{
// Insert Search text for Shipping License Plate
insertInputField(driver, "username.input", "warehouse");
// Insert Search text for Serial Number
insertInputField(driver, "password.input", "password");
// Click Authorize button
clickButton(driver, "authorization.authorize.button", "Authorize");
}
我用这个代码来检测弹出的登录屏幕。但当登录窗口不是时,我当然应该得到waiting for visibility of element located by By.id: username.input
。是否可以静默地跳过此代码执行?
您可以简单地使用这样的函数:
public boolean waitForElementToBeVisible(By element) {
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
return true;
}catch (Throwable t){
return false;
}
}
有了它,你可以实现你想要的:
if(waitForElementToBeVisible(By.id("username.input"))){
WebElement inputBox = driver.findElement(By.id("username.input"));
String textInsideInputBox = inputBox.getAttribute("value");
if(!textInsideInputBox.isEmpty())
{
// Insert Search text for Shipping License Plate
insertInputField(driver, "username.input", "datex.local\fp_warehouse");
// Insert Search text for Serial Number
insertInputField(driver, "password.input", "PortalPortal@5655!");
// Click Authorize button
clickButton(driver, "authorization.authorize.button", "Authorize");
}
}