1.在pom.xml中增加testng的依赖,以导入testNG
2.在src-main-resources目录下新建xml文件,比如untitled.xml.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />
</classes> </test>
</suite>
3.BeforeTest AfterTest BeforeClass AfterClass的区别
BeforeTest AfterTest
case1:
package testcase; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class testNG3 {
@BeforeTest
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){System.out.println("case3");}
@AfterTest
public void tearup(){System.out.println("case3:tear down");}
}
case2:
package testcase; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class testNG4 {
@BeforeTest
public void setup(){System.out.println("case4:set up");}
@Test
public void test1(){System.out.println("case4");}
@AfterTest
public void tearup(){System.out.println("case4:tear down");}
}
xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<!--<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />-->
<class name="testcase.testNG3"/>
<class name="testcase.testNG4"/>
</classes> </test>
</suite>
执行结果:
将BeforeTest替换为BeforeClass,AfterTest替换为AfterClass
case1:
package testcase; import org.testng.annotations.*; public class testNG3 {
@BeforeClass
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){System.out.println("case3");}
@AfterClass
public void tearup(){System.out.println("case3:tear down");}
}
case2:
package testcase; import org.testng.annotations.*; public class testNG4 {
@BeforeClass
public void setup(){System.out.println("case4:set up");}
@Test
public void test1(){System.out.println("case4");}
@AfterClass
public void tearup(){System.out.println("case4:tear down");}
}
xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<!--<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />-->
<class name="testcase.testNG3"/>
<class name="testcase.testNG4"/>
</classes> </test>
</suite>
执行结果:
4.使用TestNg增加断言
package testcase;
import org.testng.Assert;
import org.testng.annotations.*;
public class testNG3 {
@BeforeClass
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){
System.out.println("case3");
String actureMessage="abc";
String expectMessage="efg";
Assert.assertTrue(actureMessage.equalsIgnoreCase(expectMessage),"Expect message is "+expectMessage+".But acture message is "+actureMessage);
}
@AfterClass
public void tearup(){System.out.println("case3:tear down");}
}
将expectMessage修改为abc,结果为
5.编写基于TestNg的测试用例
Baidu.java
package testcase; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import java.util.concurrent.TimeUnit;
public class Baidu {
private WebDriver driver;
@BeforeTest
public void setUp(){
String chromepath = System.getProperty("user.dir")+"/src/main/java/drivers/chromedriver";
System.setProperty("webdriver.chrome.driver",chromepath);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void test(){
driver.get("https://www.baidu.com");
driver.findElement(By.xpath("//input[@class='s_ipt' and @id='kw']")).sendKeys("selenium");
driver.findElement(By.xpath("//input[@id='su']")).click();
System.out.println(driver.findElement(By.xpath("//span[@class='nums_text']")).getText()); }
@AfterTest
public void tearDown(){
driver.quit();
}
}
test.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<class name="testcase.Baidu" />
</classes> </test>
</suite>
测试结果: