测试模拟环境:在www.1905.com网站中执行两个用户的登陆退出操作
需要的文件有:
1、User的实例类:
public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2、DBUtil
public class MysqlUtil { public static Connection getConnection(){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/SY","root","1905"); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close(Statement stmt,Connection conn){ if(conn!=null){ try { if(stmt!=null){ stmt.close(); } conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
3、UserDAO
public class UserDAO { public List<String> usernameList() throws Exception{ String username; List<String> usernameList=new ArrayList<String>(); Connection conn = MysqlUtil.getConnection(); Statement stat = conn.createStatement(); ResultSet rst = stat.executeQuery("select * from user"); System.out.println(rst); while(rst.next()){ username = rst.getString("name"); usernameList.add(username); } System.out.println(usernameList); return usernameList; } public List<String> passwordList() throws Exception{ String password; List<String> passwordList=new ArrayList<String>(); Connection conn = MysqlUtil.getConnection(); Statement stat = conn.createStatement(); ResultSet rst = stat.executeQuery("select * from user"); while(rst.next()){ password = rst.getString("password"); passwordList.add(password); } System.out.println(passwordList); return passwordList; } }
4、Junit测试类
public class LoginJDBCTest { private static WebDriver driver; private static Navigation navigate; private static String url="http://www.1905.com"; private static String filesrc = "UserAndPassword.xls"; @BeforeClass public static void setUpBeforeClass() throws Exception { //加载浏览器 driver = new FirefoxDriver(); navigate = driver.navigate(); navigate.to(url); driver.manage().window().maximize(); } @AfterClass public static void tearDownAfterClass() throws Exception { if(driver!=null){ driver.close(); driver.quit(); } } @Test public void test() throws Exception { UserDAO user = new UserDAO(); List<String> usernameList = user.usernameList(); List<String> passwordList = user.passwordList(); int usersize = usernameList.size(); for(int i=0;i<usersize;i++){ new WebDriverWait(driver,15).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[2]/a"))); try { Thread.sleep(8000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[2]/a")); LogAndReg.click(); WebElement username = driver.findElement(By.xpath(".//*[@id=‘inputUsername‘]")); WebElement password = driver.findElement(By.xpath(".//*[@id=‘inputPassword‘]")); WebElement login = driver.findElement(By.xpath(".//*[@id=‘loginreg‘]/div/div[1]/form/p/button")); username.clear(); String name = usernameList.get(i); username.sendKeys(name); //输入对应的password值 for(int j=0;j<passwordList.size();j++){ password.clear(); String pass = passwordList.get(j); password.sendKeys(pass); } login.click(); WebDriverWait wait = new WebDriverWait(driver,10); WebElement logoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[3]/a[2]"))); logoutButton.click(); } } }
以上代码是给自己的一个记录,其中有些还可以继续完善封装,剩下的就之后在完善好了~~
时间: 2024-10-12 16:53:19