————————————第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文件格式————————————
————————————第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————————————
补充一点:在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
————————————第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 + ":其他用户");
}
}}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 史上最全文件类型读写库大盘点!什么?还包括音频、视频?
欢迎来到我们的系列博客《Python全景系列》!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题,帮助你掌握这门强大而灵活的编程语言! 本文系列第四篇,介绍史上最全PYTHON文件类型读写库大盘点!包含常用和不常用的大量文件格式!文…