如何在java的webdriver中使用多个断言
我是SeleniumWebDriver的新手,正在尝试学习这个API。我想知道如何在一个测试中有效地使用多个断言。我试图直接使用它,但它增加了我的代码长度,而且调试起来非常困难。有什么建议可以帮你完成吗如何在java的webdriver中使用多个断言,java,selenium,Java,Selenium,我是SeleniumWebDriver的新手,正在尝试学习这个API。我想知道如何在一个测试中有效地使用多个断言。我试图直接使用它,但它增加了我的代码长度,而且调试起来非常困难。有什么建议可以帮你完成吗 package com.eureqa.scripts; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
package com.eureqa.scripts;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Phase1 {
public static WebDriver driver;
public static WebDriver driver1;
public void navigation1(WebDriver driver1)
{
boolean result=verifyElementPresent(driver1);
if(result)
{
System.out.println("Element found");
}
else
{
System.out.println("Element not found");
}
driver1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
boolean result1=assertElementPresent(driver1);
if(result1)
{
System.out.println("Element asserted");
}
else
{
System.out.println("Element not asserted");
driver1.quit();
}
driver1.findElement(By.linkText("Reports")).click();
}
public boolean verifyElementPresent(WebDriver driver1)
{
try{
driver1.findElement(By.id("commonheader:headerForm:projectlist"));
return true;
}catch(Exception ex)
{
return false;
}
}
public boolean assertElementPresent(WebDriver driver1)
{
try{
driver1.findElement(By.linkText("Reports"));
return true;
}catch(Exception ex)
{
return false;
}
}
public static void main(String arr[]) throws InterruptedException
{
WebDriver driver1=LoginObject.driver();
System.out.println("Object Received");
LoginEureqa m=new LoginEureqa();
m.login(driver1);
Phase1 p1=new Phase1();
p1.navigation1(driver1);
System.out.println();
System.out.println("Phase1 executed successf`enter code here`ully");
}
}
我已经将我的selenium测试与jUnit结合起来,所以我可以只使用jUnit的内置断言。它使代码更易于使用。另一种方法是,您需要查看Selenium的wait方法
当你在考虑测试的时候,你也应该考虑是否真的需要测试在发现错误条件之后继续运行。在您的示例中,如果verifyElementPresent和assertElementPresent都失败,测试尝试单击“Reports”链接是否有意义?如果失败后不需要继续运行测试,代码将更简单
看看你的代码,我可能会重写导航1,比如:package com.eureqa.scripts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Phase1 {
public static WebDriver driver;
public static WebDriver driver1;
public void navigation1(WebDriver driver1)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
assertTrue("commonheader:headerForm:projectlist not found",
driver1.findElements(By.id("commonheader:headerForm:projectlist")).size() == 1);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Reports"))).click();
}
public static void main(String arr[]) throws InterruptedException
{
WebDriver driver1=LoginObject.driver();
System.out.println("Object Received");
LoginEureqa m=new LoginEureqa();
m.login(driver1);
Phase1 p1=new Phase1();
p1.navigation1(driver1);
System.out.println();
System.out.println("Phase1 executed successf`enter code here`ully");
}
}
现在,这并不能完全重现你在原始帖子中的内容,因为每一步都会失败。如果希望第一个断言失败,但继续,可以使用try/catch块包围assertTrue,然后显示单独的消息
navigation1中的代码非常简单。它首先尝试查找ID为“commonheader:HeadPerform:projectlist”的元素。如果找不到该元素,它将抛出一个测试错误,消息为“commonheader:HeadPerform:projectlist not found”
第二步是等待一个带有文本“Reports”的链接被点击。如果该链接在10秒内没有出现,将抛出一个错误 有时,您可能需要对添加脚本执行多个断言 像
String alert_Text = driver.switchTo().alert().getText();
String successMessage = "item successfully added.";
String duplicateMessage = "Duplicate item not allowed";
assertTrue(alert_Text.equals(successMessage) || alert_Text.equals(duplicateMessage), "Success or Duplicate both message will be count as successful message");
你能提供更多关于你想测试什么的细节吗?您只是在寻找一个更好看的代码,还是在尝试运行当前的代码时出现了错误?我建议将所有断言方法(您将在整个代码中递归使用)放在不同的java文件中,并在必要时通过导入类和使用必要的方法调用它们。我的代码这次运行得很好,但我担心的是我必须多次使用断言命令。因此,我想在java中创建一个单独的类,当我可以泛化一个方法时,该方法将使用所有类型的定位器并返回一个布尔值。请建议最好的方法,只要几件事-如果正在使用
驱动程序
初始化为静态字段,则无需将其作为方法参数传递。我知道这只是一个建议的修改,但如果采用JUnit(我个人更喜欢)的方式,可能会将整个代码转换为类似于测试用例的代码。