一:dependsOnMethonds依赖关系
@Test(dependsOnMethonds={“上个@Test方法”})
public class testDepend {
private WebDriver driver;
@Test
public void openSougou() {
driver.get("https://www.sogou.com/");
}
@Test(dependsOnMethods= {"openSougou"})
public void switchFrame() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"loginBtn\"]")).click();
driver.switchTo().frame(driver.findElement(By.xpath(("/html/body/div[1]/iframe"))));
driver.switchTo().frame("ptlogin_iframe");
Thread.sleep(1000);
}
@Test(dependsOnMethods= {"switchFrame"})
public void run() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"switcher_plogin\"]")).click();
driver.findElement(By.xpath("//*[@id=\"u\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"u\"]")).sendKeys("123");
driver.findElement(By.xpath("//*[@id=\"p\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"p\"]")).sendKeys("123");
Thread.sleep(1000);
}
@BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver", ".\\tools\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
}
@AfterClass
public void afterClass() {
driver.quit();
}
}
TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题。
二:@Test方法首字母大小升序
如果不用dependsOnMethods,testNG会自动根据@Test方法名称的首字母升序来执行测试方法。
三:priority优先级设置
或者可以设置测试方法的优先级:
@Test(priority=执行顺序序号)
public void 方法1(){
xxxxxx;
}
@Test(priority=1)
public void 方法2(){
yyyyyyy;
}
&~&~&~& 当想要某段方法暂时不执行,可修改test代码块: @Test(enabled=false) 也可注释掉//