目前对于我的这段代码,我想检查的是,只有当我的优先级为1的第一个测试失败时,我的优先级为3的测试用例才应该运行..我该怎么办?
// login in to BIO
@Test (priority=1)
public void Login() throws Exception{
browser.findElement(By.name("name")).sendKeys("johnmartin11");
browser.findElement(By.name("password")).sendKeys("johnmartin1111");
browser.findElement(By.cssSelector("input.signup-btn")).click();
Thread.sleep(20000);
try{
Thread.sleep(20000);
WebElement element = browser.findElement(By.xpath("/html/body/div[3]/div/div[1]/div/div[2]/div/div[1]/a/div[1]"));
String trb = element.getText();
Assert.assertEquals("Blood", trb);
System.out.println("Login as Buyer successful");
}
catch(Exception e){
System.out.println("Login failed as Buyer");
}
}
// Company Account page here
@Test (priority=2)
public void CompanyAccount() throws Exception{
browser.findElement(Companyac).click();
Thread.sleep(20000);
try{
WebElement element = browser.findElement(Companyac);
String cac = element.getText();
Assert.assertEquals("Company Account", cac);
System.out.println("in Company Account page Successfully");
}
catch(Exception e){
System.out.println("Company Account failed");
}
}
// Create Stock link check
// @Test (priority=3)
// public void membertopartner() throws Exception{
// Thread.sleep(20000);
// browser.findElement(memberlink).click();
// browser.findElement(By.linkText("Partner")).click();
// Thread.sleep(20000);
// try{
// WebElement element = browser.findElement (By.linkText("Manage Product Stocks"));
// String stocklink = element.getText();
// Assert.assertEquals("Manage Product Stocks", stocklink);
// System.out.println("stock link found");
// }
// catch(Exception e){
// System.out.println("Stock link failed");
// }
// }
最佳答案
您可以在第二个方法中使用dependsOnMethods注释,这样,如果第一个方法通过,则仅执行第二个方法。现在根据您的要求,您只想在第一次失败时才执行第二次,然后您必须以这种方式调整您的Assert,因此我将Assert.assertEquals更改为Assert.assertNotEquals
@Test (priority=1)
public void Login() throws Exception{
browser.findElement(By.name("name")).sendKeys("johnmartin11");
browser.findElement(By.name("password")).sendKeys("johnmartin1111");
browser.findElement(By.cssSelector("input.signup-btn")).click();
Thread.sleep(20000);
try{
Thread.sleep(20000);
WebElement element = browser.findElement(By.xpath("/html/body/div[3]/div/div[1]/div/div[2]/div/div[1]/a/div[1]"));
String trb = element.getText();
Assert.assertNotEquals("Blood", trb);
System.out.println("Login as Buyer successful");
}
catch(Exception e){
System.out.println("Login failed as Buyer");
}
}
// Company Account page here -- Now this method will be executed only when Login is successful.
@Test (dependsOnMethods = { "Login" })
public void CompanyAccount() throws Exception{
browser.findElement(Companyac).click();
Thread.sleep(20000);
try{
WebElement element = browser.findElement(Companyac);
String cac = element.getText();
Assert.assertEquals("Company Account", cac);
System.out.println("in Company Account page Successfully");
}
catch(Exception e){
System.out.println("Company Account failed");
}
}
关于java - 我想检查的是,只有当我的优先级为1的第一个测试失败时,我的优先级为3的测试用例才能运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35890439/