selenium TestNG 依赖和忽略测试
依赖:通过使用Test 注释的dependsOnMethods={"verifyLogin"}子句,verifyAccountInfo 测试指定了它依赖verifyLogin()方法。(如果verifyLogin() 方法执行失败,则Test注释的方法会跳过)
忽略:有时候测试用例还没准备好, 可以给测试用例加上@Test(enable = false), 来禁用此测试用例。(该用了不会执行)
1 package com.TestNG.web; 2 3 import java.util.concurrent.TimeUnit; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.chrome.ChromeOptions; 9 import org.testng.annotations.AfterClass; 10 import org.testng.annotations.BeforeClass; 11 import org.testng.annotations.Test; 12 13 public class testng { 14 private WebDriver driver; 15 private String baseUrl; 16 17 @BeforeClass 18 public void test() { 19 System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe"); 20 ChromeOptions Options = new ChromeOptions(); 21 Options.addArguments("user-data-dir=C:\\Users\\happy\\AppData\\Local\\Google\\Chrome\\User Data"); 22 driver = new ChromeDriver(Options); 23 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); 24 25 } 26 27 @Test 28 public void test1() { 29 30 baseUrl = "https://www.baidu.com/"; 31 driver.get(baseUrl); 32 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 33 driver.findElement(By.id("kw")).sendKeys("testNG"); 34 driver.findElement(By.id("su")).click(); 35 36 } 37 38 @Test(dependsOnMethods = {"test1"})//test2()依赖于test1 39 public void test2(){ 40 driver.findElement(By.linkText("英文结果")).click(); 41 42 43 44 } 45 @AfterClass 46 public void test3(){ 47 driver.close(); 48 driver.quit(); 49 50 } 51 //忽略这条用例 52 @Test(enabled = false) 53 public void test4(){ 54 System.out.println("我就是一个测试"); 55 } 56 @Test 57 public void test5(){ 58 System.out.println("我就是一个测试"); 59 } 60 61 }