模拟用户点击链接
Posted 蓝色的天_90
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟用户点击链接相关的知识,希望对你有一定的参考价值。
1 package tool; 2 import org.junit.Assert; 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.chrome.ChromeDriver; 5 import org.openqa.selenium.firefox.FirefoxDriver; 6 import org.openqa.selenium.ie.InternetExplorerDriver; 7 import org.openqa.selenium.remote.DesiredCapabilities; 8 /** 9 * 公共设置 浏览器 10 * @author nxy 11 * 12 */ 13 public class SetBrower { 14 /** 15 * 设置火狐浏览器 16 * @param browerDriverPath 17 * @return 18 */ 19 public WebDriver firefoxBrower(String browerDriverPath){ 20 WebDriver firefoxWD ; 21 //指定驱动路径 22 System.setProperty("webdriver.firefox.bin", browerDriverPath); 23 //新建一个firefox 浏览器driver 24 firefoxWD = new FirefoxDriver(); 25 return firefoxWD; 26 } 27 /** 28 * 设置chrome浏览器 29 * @param browerDriverPath 30 * @return 31 */ 32 public WebDriver chromeBrower(String browerDriverPath){ 33 WebDriver chromeWD ; 34 System.setProperty("webdriver.chrome.driver", browerDriverPath); 35 //这里需要注意的是 放的地址是 chromedriver 的存放地址 36 //新建一个google 浏览器driver 37 chromeWD = new ChromeDriver(); 38 return chromeWD; 39 40 } 41 /** 42 * 设置ie浏览器 43 * @param browerDriverPath 44 * @return 45 */ 46 47 public WebDriver ieBrower(String browerDriverPath){ 48 WebDriver ieWD ; 49 System.setProperty("webdriver.ie.driver", browerDriverPath); 50 //这里需要注意的是 放的地址是 IEDriverServer 的存放地址 51 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); 52 ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 53 //新建一个ie 浏览器driver 54 ieWD = new InternetExplorerDriver(ieCapabilities); 55 return ieWD; 56 57 58 59 } 60 /** 61 * 测试上面的方法 62 * @param args 63 * @throws InterruptedException 64 */ 65 66 public static void main(String[] args) throws InterruptedException { 67 SetBrower test = new SetBrower(); 68 //WebDriver wd= test.chromeBrower("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 69 WebDriver wd= test.firefoxBrower("D:\\Program Files\\firefox\\firefox.exe"); 70 //WebDriver wd= test.ieBrower("C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe"); 71 //打开网站 72 wd.get("http://ip:端口/项目名称/"); 73 74 wd.quit(); 75 76 } 77 78 }
package entity; public class UserTest { private int id; private String userID; private String loginId; private String deptId; private String deptName; private String compName; private int staus; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } public String getLoginId() { return loginId; } public void setLoginId(String loginId) { this.loginId = loginId; } public String getDeptId() { return deptId; } public void setDeptId(String deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getCompName() { return compName; } public void setCompName(String compName) { this.compName = compName; } public int getStaus() { return staus; } public void setStaus(int staus) { this.staus = staus; } public UserTest(int id, String userID, String loginId, String deptId, String deptName, String compName, int staus) { super(); this.id = id; this.userID = userID; this.loginId = loginId; this.deptId = deptId; this.deptName = deptName; this.compName = compName; this.staus = staus; } public UserTest() {
package db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * @author nxy * @创建时间 2014年10月21日-下午1:40:54 * @类名 DBUtil.java */ public class DBUtil { public static Connection getConnection(String username, String password, String url) throws Exception { Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, username, password); return connection; } public static void close(Connection conn) throws SQLException { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw e; } } } public static Connection getConn() throws Exception { Connection connection = null; String username = "root"; String password = "******"; String url = "jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=utf-8"; Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, username, password); return connection; } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import db.DBUtil; import entity.UserTest; public class UserDaoImpl { private static final String FIND_ALL_USER_LOGIN = "SELECT id,user_id,login_id,dept_id,dept_name,compy_name,status FROM user_login where status =0"; private static final String UPDATE_USER_STATUS = "update user_login set status = 1 where login_id =?"; /** * 得到所有将要登录的用户记录 * @return * @throws Exception */ public List<UserTest> findAllUsers() throws Exception{ Connection conn = null; conn = DBUtil.getConn(); PreparedStatement prep = conn.prepareStatement(FIND_ALL_USER_LOGIN); ResultSet rs = prep.executeQuery(); List<UserTest> listAll = new ArrayList<UserTest>(); while(rs.next()){ UserTest bc = new UserTest(); bc.setId(rs.getInt("id")); bc.setCompName(rs.getString("compy_name")); bc.setDeptId(rs.getString("dept_id")); bc.setDeptName(rs.getString("dept_name")); bc.setLoginId(rs.getString("login_id")); bc.setStaus(rs.getInt("status")); bc.setUserID(rs.getString("user_id")); listAll.add(bc); } DBUtil.close(conn); return listAll; } /** * 根据loginid更新 是否已登录的状态 * ststus= 1 说明已经登录完成 * @param bc * @return */ public UserTest UpdateLoginStatus(UserTest bc){ Connection conn = null; try { conn = DBUtil.getConn(); PreparedStatement pst = conn.prepareStatement(UPDATE_USER_STATUS); pst.setString(1, bc.getLoginId()); pst.executeUpdate(); DBUtil.close(conn); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return bc; } public static void main(String[] args) throws Exception { UserDaoImpl usertest = new UserDaoImpl(); List<UserTest> ll = usertest.findAllUsers(); for (UserTest userTest2 : ll) { System.out.println(userTest2.getLoginId()); } } }
package autoTest; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import dao.UserDaoImpl; import entity.UserTest; import tool.SetBrower; public class UserAction { public void UserAction( String baseUrl,String loginId){ SetBrower setBrower = new SetBrower(); WebDriver driver= setBrower.firefoxBrower("D:\\Program Files\\firefox\\firefox.exe"); try{ //baseUrl = "http://ip:端口/项目名称/manage/login/login!toTmplogin?nextUrl=%2Fguohua%2Freporttotal%2Ftotal!togoMoonReportPage"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get(baseUrl + "/homePage/homePage"); driver.findElement(By.name("cmsUser.loginId")).clear(); driver.findElement(By.name("cmsUser.loginId")).sendKeys(loginId); driver.findElement(By.cssSelector("input.loginbtn")).click(); driver.findElement(By.linkText("国际资讯")).click(); driver.findElement(By.linkText("能源企业")).click(); driver.findElement(By.linkText("技术标准")).click(); driver.findElement(By.linkText("技术管理")).click(); driver.findElement(By.linkText("太阳能光热发电培训材料")).click(); }catch(Exception e){ driver.findElement(By.linkText("退出")).click(); driver.quit(); System.out.println(e); } } public static void main(String[] args) throws Exception { UserAction test = new UserAction(); UserDaoImpl dao = new UserDaoImpl(); List<UserTest> userTest = dao.findAllUsers(); for (UserTest userTest2 : userTest) { String loginId = userTest2.getLoginId(); int status = userTest2.getStaus(); test.UserAction("http://ip:端口/项目名称", loginId); dao.UpdateLoginStatus(userTest2); } } }
super(); } }
以上是关于模拟用户点击链接的主要内容,如果未能解决你的问题,请参考以下文章