Selenium WebDriver 忽略 JAVA 中编写的条件逻辑
Selenium WebDriver ignoring the conditional logic written in JAVA
这是一个简单的 sign-in 脚本,我正在 运行 Firefox 中使用。
我想做的是在 sign-in 成功或失败时打印一条消息。
我给错了 user/pass 得到错误(根据网站验证它是正确的)。
page-title 元素仅在用户 logged-in 成功时显示。同样,error-msg 元素仅在用户不是 logged-in.
时显示
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.example.com";
String actualTitle;
// Launch Firefox and direct it to the Base URL
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
// Maximize the browser window
driver.manage().window().maximize();
// Get the actual value of the title
actualTitle = driver.getTitle();
driver.findElement(By.linkText("Signin")).click();
WebElement email = driver.findElement(By.id("email"));
email.click();
email.sendKeys("abc@example.com");
WebElement pass = driver.findElement(By.id("pass"));
pass.click();
pass.sendKeys("abc123456");
driver.findElement(By.id("send2")).click();
// Variables for Dashboard & Error Title
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
WebElement loginFailed = driver.findElement(By.className("error-msg"));
if(loginFailed.getText().contains("Invalid login or password."))
System.out.println("Test passed but login failed.");
else if(dashboardTitle.getText().contains("My Dashboard"))
System.out.println("Logged in successfully.");
else if(dashboardTitle.getText().contains("Account Information"))
System.out.println("Logged in successfully.");
else
System.out.println("You're not logged-in.");
System.out.println("The title of current page is: "+actualTitle);
driver.close();
driver.quit();
User/Pass 不正确,所以 'IF' 条件应该 运行 但实际上它给我错误,找不到其他元素 (page-title)。
如果我删除其他条件(从 IF 语句),那么脚本 运行ning 没有任何问题。
我希望脚本 运行 并搜索这两个元素,如果它只找到一个元素(它应该是),那么它应该显示它的结果而不是给我错误说其他元素不是找到了。
使用 try-catch 块。
....
....
driver.findElement(By.id("send2")).click();
try {
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
System.out.println("Logged in successfully.");
/* Continue code for correct login credentials
*
*/
} catch(NoSuchElementException e) {
}
try {
WebElement loginFailed = driver.findElement(By.className("error-msg"));
System.out.println("Test passed but login failed.");
/* Continue code for incorrect login credentials
*
*/
} catch(NoSuchElementException e) {
}
System.out.println("The title of current page is: "+actualTitle);
driver.close();
driver.quit();
....
....
使用下面的方法来查找一个web元素,这应该可以帮助你等待并找到元素,如果没有找到元素它会捕获异常并且return null 或者一个WebElement。
public WebElement waitAndGetElement(WebDriver driver, By by, int waitTimeInSeconds) {
try {
return new WebDriverWait(driver, waitTimeInSeconds).until(ExpectedConditions.visibilityOfElementLocated(by));
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
示例:
WebElement loginFailed = waitAndGetElement(driver, By.className("error-msg"), 5);
if(loginFailed != null && loginFailed.getText().contains("Invalid login or password."))
System.out.println("Test passed but login failed.");
使用此代码解决了我的问题:
Boolean exist;
if(exist = driver.findElements(By.className("page-title")).size() == 0){
WebElement errorMessage = driver.findElement(By.className("error-msg"));
if(errorMessage.getText().contains("Invalid login or passwords.")){
System.out.println("Test passed but login failed.");
}
else
System.out.println("error-msg class found but different message in it.");
}
else{
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
if(dashboardTitle.getText().contains("Account Information"))
System.out.println("Logged in successfully.");
else
System.out.println("LOGGED IN");
}
这是一个简单的 sign-in 脚本,我正在 运行 Firefox 中使用。 我想做的是在 sign-in 成功或失败时打印一条消息。 我给错了 user/pass 得到错误(根据网站验证它是正确的)。
page-title 元素仅在用户 logged-in 成功时显示。同样,error-msg 元素仅在用户不是 logged-in.
时显示WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.example.com";
String actualTitle;
// Launch Firefox and direct it to the Base URL
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
// Maximize the browser window
driver.manage().window().maximize();
// Get the actual value of the title
actualTitle = driver.getTitle();
driver.findElement(By.linkText("Signin")).click();
WebElement email = driver.findElement(By.id("email"));
email.click();
email.sendKeys("abc@example.com");
WebElement pass = driver.findElement(By.id("pass"));
pass.click();
pass.sendKeys("abc123456");
driver.findElement(By.id("send2")).click();
// Variables for Dashboard & Error Title
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
WebElement loginFailed = driver.findElement(By.className("error-msg"));
if(loginFailed.getText().contains("Invalid login or password."))
System.out.println("Test passed but login failed.");
else if(dashboardTitle.getText().contains("My Dashboard"))
System.out.println("Logged in successfully.");
else if(dashboardTitle.getText().contains("Account Information"))
System.out.println("Logged in successfully.");
else
System.out.println("You're not logged-in.");
System.out.println("The title of current page is: "+actualTitle);
driver.close();
driver.quit();
User/Pass 不正确,所以 'IF' 条件应该 运行 但实际上它给我错误,找不到其他元素 (page-title)。
如果我删除其他条件(从 IF 语句),那么脚本 运行ning 没有任何问题。
我希望脚本 运行 并搜索这两个元素,如果它只找到一个元素(它应该是),那么它应该显示它的结果而不是给我错误说其他元素不是找到了。
使用 try-catch 块。
....
....
driver.findElement(By.id("send2")).click();
try {
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
System.out.println("Logged in successfully.");
/* Continue code for correct login credentials
*
*/
} catch(NoSuchElementException e) {
}
try {
WebElement loginFailed = driver.findElement(By.className("error-msg"));
System.out.println("Test passed but login failed.");
/* Continue code for incorrect login credentials
*
*/
} catch(NoSuchElementException e) {
}
System.out.println("The title of current page is: "+actualTitle);
driver.close();
driver.quit();
....
....
使用下面的方法来查找一个web元素,这应该可以帮助你等待并找到元素,如果没有找到元素它会捕获异常并且return null 或者一个WebElement。
public WebElement waitAndGetElement(WebDriver driver, By by, int waitTimeInSeconds) {
try {
return new WebDriverWait(driver, waitTimeInSeconds).until(ExpectedConditions.visibilityOfElementLocated(by));
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
示例:
WebElement loginFailed = waitAndGetElement(driver, By.className("error-msg"), 5);
if(loginFailed != null && loginFailed.getText().contains("Invalid login or password."))
System.out.println("Test passed but login failed.");
使用此代码解决了我的问题:
Boolean exist;
if(exist = driver.findElements(By.className("page-title")).size() == 0){
WebElement errorMessage = driver.findElement(By.className("error-msg"));
if(errorMessage.getText().contains("Invalid login or passwords.")){
System.out.println("Test passed but login failed.");
}
else
System.out.println("error-msg class found but different message in it.");
}
else{
WebElement dashboardTitle = driver.findElement(By.className("page-title"));
if(dashboardTitle.getText().contains("Account Information"))
System.out.println("Logged in successfully.");
else
System.out.println("LOGGED IN");
}