任何人都可以帮助我使用AssertEquals吗?
我的测试用例类的代码如下,但在AssertEquals失败后,测试继续进行下一个方法,即createClientTodelete。为什么呢?
public class Client
{
public String baseUrl = "http://test.abc.com";
public WebDriver driver;
@BeforeTest
public void setBaseURL()
{
driver = new FirefoxDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
}
@Test(priority = 0, description = "verify successful login")
public void verifyLogin()
{
String expectedDashTitle = "oms";
String actualDashTitle = driver.getTitle();
Assert.assertTrue(driver.findElements(By.name("username")).size()>0);
Assert.assertTrue(driver.findElements(By.name("password")).size()>0);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("123@123");
driver.findElement(By.name("login")).submit();
Assert.assertEquals(actualDashTitle, expectedDashTitle,"Title Not Found!");
}
@Test(priority = 1, description = "verify client is created successfully")
public void createClientTodelete()
{
driver.findElement(By.xpath(".//*[@id='mainMenu']/ul/li[2]/a/span")).click();
Assert.assertTrue(driver.findElements(By.linkText("Create")).size()>0);
driver.findElement(By.linkText("Create")).click();
driver.findElement(By.id("company_name")).sendKeys("TestCompany");
driver.findElement(By.id("contact_person_firstname")).sendKeys("FirstName");
driver.findElement(By.id("contact_person_lastname")).sendKeys("LastName");
driver.findElement(By.id("contact_person_email")).sendKeys("abc@test.com");
driver.findElement(By.id("save")).click();
}
答案 0 :(得分:2)
通过查看您的代码,可以清楚地知道您正在使用TestNG进行这些测试。 在TestNG中,您可以使用“dependsOnMethods”属性,如下所示。
package sample.testng;
import org.testng.annotations.Test;
import org.testng.Assert;
public class SampleTest {
@Test
public void test(){
System.out.println("Executing test 1");
Assert.assertEquals("ABCD", "abc");
}
@Test(dependsOnMethods={"test"})
public void test1(){
System.out.println("Second test runs only if the first one is successful, otherwise its ignored");
//Asserts or whatever
}
}