------------------------------------第1个案例BeforeMethod------------------------------------
import org.testng.annotations.*;
public class TestNGDemo {
@BeforeMethod
public void BeforeMethod() {
System.out.println("------BeforeMethod------");
} @AfterMethod
public void AfterMethod() {
System.out.println("------AfterMethod------");
} @Test
public void Test001() {
System.out.println("------Test001------"); }
@Test
public void Test002() {
System.out.println("------Test002------"); }
@Test
public void Test003() {
System.out.println("------Test003------"); }
}
------------------------------------第2个案例BeforeClass------------------------------------
import org.testng.annotations.*;
public class TestNGDemo {
@BeforeClass
public void BeforeClass() {
System.out.println("------BeforeClass------");
} @AfterClass
public void AfterClass() {
System.out.println("------AfterClass------");
} @BeforeMethod
public void BeforeMethod() {
System.out.println("------BeforeMethod------");
} @AfterMethod
public void AfterMethod() {
System.out.println("------AfterMethod------");
} @Test
public void Test001() {
System.out.println("------Test001------"); }
@Test
public void Test002() {
System.out.println("------Test002------"); }
@Test
public void Test003() {
System.out.println("------Test003------"); }
}
------------------------------------第3个案例XML文件格式------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test12">
<classes>
<class name="TankLearn2.Learn.TestNGLearn1" />
</classes>
</test>
</suite>
------------------------------------第4个案例建立一个config文件格式------------------------------------
import org.testng.annotations.*;
public class Config {
@BeforeSuite
public void BeforeSuite() {
System.out.println("------BeforeSuite------");
} @AfterSuite
public void AfterSuite() {
System.out.println("------AfterSuite------");
} @BeforeTest
public void BeforeTest() {
System.out.println("------BeforeTest------");
} @AfterTest
public void AfterTest() {
System.out.println("------AfterTest------");
}}
------------------------------------第5个案例通过xml参数化test2.xml------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<parameter name="test1" value="aaa" />
<parameter name="test2" value="bbb" />
<test name="test12">
<classes>
<class name="B.Demo" />
</classes>
</test>
</suite>
补充一点:在B包中创建的Demo文件:
package B;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;public class Demo {
@Test
@Parameters({"test1","test2"})
public void ParaTest(String test1, String test2) {
System.out.println("This is " + test1);
System.out.println("This is " + test2); }
}
------------------------------------第6个案例通过DataProvider传递参数------------------------------------
package B;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;public class Demo2 {
@DataProvider(name = "user")
public Object[][] Users() {
return new Object[][] { { "aaa", "123456" }, { "bbb", "123456" }, { "ccc", "123456" } };
} @Test(dataProvider = "user")
public void verifyUser(String userName, String password) {
System.out.println("Username: " + userName + " Password: " + password);
}}
------------------------------------第7个案例通过JDBC连接数据库进行数据驱动------------------------------------
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class Test01 {
WebDriver driver;
@BeforeMethod
public void open() {
driver = new FirefoxDriver();
}
@AfterMethod
public void close() {
driver.quit();
}
@DataProvider(name = "dp1")
public Object[][] dp001(){
Object[][] oo=null;//申明一个二维数组,初始值为空值
try {
Connection conn=DriverManager.getConnection("jdbc:mysql://192.168.221.128:3306/bugs","root","123456");
String sql;
sql="select * from profiles";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
List<Object[]> list = new ArrayList<>();//取到所有的用户名再加上12456放到这个list的每个元素里面,list里面的每个元素都是object数组
while(rs.next()) {
list.add(new Object[] {rs.getString("login_name"),"123456"});
}
oo=new Object[list.size()][2];//把list变成为二维数组listsize多少个元素即是数组的大小
for(int i=0;i<oo.length;i++) {
oo[i]=list.get(i);
}
}catch (SQLException e) {
e.printStackTrace();
}
return oo;
}
@Test(dataProvider = "dp1")
public void Test(String uname,String pwd) {
driver.get("http://192.168.221.128/bugzilla/");
driver.findElement(By.linkText("Log In")).click();
driver.findElement(By.id("Bugzilla_login_top")).sendKeys(uname);
driver.findElement(By.id("Bugzilla_password_top")).sendKeys(pwd);
driver.findElement(By.id("log_in_top")).click();
String status = driver.findElement(By.xpath("/html/body/div[1]/table[1]/tbody/tr/td[1]/p")).getText();
System.out.println(status);
switch(status) {
case "Bugzilla – Welcome to Bugzilla":
System.out.println(uname+":管理员");
break;
case "Bugzilla – Main Page":
System.out.println(uname+":普通用户");
break;
case "Bugzilla – Account Disabled":
System.out.println(uname+":禁用用户");
break;
default:
System.out.println(uname+":其他用户");
}
}
}
------------------------------------第8个案例通过案例通过DataProvider传递参数进行bugzilla登录------------------------------------
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;public class Test01 {
WebDriver driver; @BeforeMethod
public void open() {
driver = new FirefoxDriver();
} @AfterMethod
public void close() {
driver.quit();
} @DataProvider(name = "dp1")
public Object[][] Users() {
return new Object[][] { { "scbigboss@163.com", "123456" }, { "bbb@163.com", "123456" },
{ "aaa@163.com", "123456" }, { "ccc@163.com", "12345" } };
} @Test(dataProvider = "dp1")
public void Test(String uname, String pwd) {
driver.get("http://192.168.221.128/bugzilla/");
driver.findElement(By.linkText("Log In")).click();
driver.findElement(By.id("Bugzilla_login_top")).sendKeys(uname);
driver.findElement(By.id("Bugzilla_password_top")).sendKeys(pwd);
driver.findElement(By.id("log_in_top")).click(); String status = driver.findElement(By.xpath("/html/body/div[1]/table[1]/tbody/tr/td[1]/p")).getText();
System.out.println(status); switch (status) {
case "Bugzilla – Welcome to Bugzilla":
System.out.println(uname + ":管理员");
break;
case "Bugzilla – Main Page":
System.out.println(uname + ":普通用户");
break;
case "Bugzilla – Account Disabled":
System.out.println(uname + ":禁用用户");
break;
default:
System.out.println(uname + ":其他用户");
}
}}