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數組/<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>
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+":其他用戶");
}
}
}
"/<oo.length>閱讀更多 肥西金 的文章