java+selenium实现web多系统登录
2017-11-02 00:00
706 查看
最近总是频繁登录各个系统,密码又是系统自动生成乱七八糟,毫无章法的字符串,难以记忆。每登一次必须查找一番,然后copy之,实在不堪其扰。所以就干脆写一个脚本自动化登录一下。
1.通过配置文件设置url,用户名、密码、提交按钮的网页元素,用户名,密码。
2.多线程方式,3秒不输入,进行默认登录。
项目需要用到selenium-server-standalone-2.53.0.jar包。下载Selenium-server-standalone,Selenium-java(这个我没有使用也可以运行)
通过批处理(因为公司有代理,设置网络环境是通过批处理命令设置的)执行,运行如下图
主要代码如下:
配置文件格式如下:
1.通过配置文件设置url,用户名、密码、提交按钮的网页元素,用户名,密码。
2.多线程方式,3秒不输入,进行默认登录。
项目需要用到selenium-server-standalone-2.53.0.jar包。下载Selenium-server-standalone,Selenium-java(这个我没有使用也可以运行)
通过批处理(因为公司有代理,设置网络环境是通过批处理命令设置的)执行,运行如下图
主要代码如下:
import java.io.FileInputStream; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Properties; import java.util.Scanner; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * @author 苏宝伢 E-mail:by.su@qq.com * @version 创建时间: 2017年11月01日 上午11:03:18 */ public class LoginAdmin extends Thread{ static boolean flag = true; static int chooseNo = 0; public static void main(String[] args){ System.out.println("请输入需要登录的系统编号,3秒内不输入默认1:"); System.out.println("1.后管平台;2.testlink;3.jira-PASTOCK;4.jira-oas;5.短信平台;6.pafa5运管平台。"); System.setProperty("webdriver.chrome.driver", "D:/bysu/interfaceAuto/chromedriver.exe"); LoginAdmin t1 = new LoginAdmin(); t1.start(); for(int i=3;i>0;i--){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("倒计时" + i + "秒"); if(i==3){ flag = false; } } WebDriver driver = new ChromeDriver(); loginStart(chooseNo,"D:/selenium/loginData.properties",driver); System.exit(0);//如果没有这句,默认登录的时候,scanner还在阻塞的线程,java程序不会自动结束。 } public void run(){ Scanner scan = new Scanner(System.in); while(flag){ chooseNo = scan.nextInt(); } scan.close(); } public static void loginStart(int num,String path,WebDriver driver){ String[] elements = getPropertiesValue(chooseUrl(num),path).replace("\"","").split(","); String url = elements[0]; String userName = elements[1]; String userNameContent = elements[2]; String userPasswd = elements[3]; String userPasswdContent = elements[4]; String submitElement = elements[5]; driver.get(url); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } driver.findElement(By.xpath(userName)).sendKeys(userNameContent); driver.findElement(By.xpath(userPasswd)).sendKeys(userPasswdContent); driver.findElement(By.xpath(submitElement)).click(); } //返回需要登录的系统名称 public static String chooseUrl(int chooseNo){ switch(chooseNo){ case 1: //ant return "ant"; case 2: //testlink return "testlink"; case 3: //jira-PASTOCK return "PASTOCK"; case 4: //jira-oas return "oas"; case 5: //sms return "sms"; case 6: //pafa5 return "pafa5"; default: return "ant"; } } //获取配置文件内容 public static HashMap<String,String> readProperties(String filePath){ Properties pp = new Properties(); HashMap<String,String> strMap = new HashMap<>(); FileInputStream fin = null; try { fin = new FileInputStream(filePath); pp.load(fin); } catch (IOException e) { e.printStackTrace(); } Enumeration enums = pp.propertyNames(); while(enums.hasMoreElements()){ String strKey = (String)enums.nextElement(); strMap.put(strKey, pp.getProperty(strKey)); } return strMap; } //根据指定key获取value public static String getPropertiesValue(String key,String path){ HashMap<String,String> mpValue = readProperties(path); return mpValue.get(key); } }
配置文件格式如下:
ant="https://soasad..,/html/body/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/input,SU...,/html/body/div/div[3]/div[1]/table/tbody/tr[2]/td[2]/input,W2yehW49,/html/body/div/div[3]/div[3]/a" testlink="http://tm....,//*[@id='login'],su...,//*[@id='login_div']/form/p[2]/input,feQ44Rs70.2,//*[@id='login_div']/form/input[5]" PASTOCK="http://jira-stock...,//*[@id='login-form-username'],su...,//*[@id='login-form-password'],feQ44Rs7..2,//*[@id='login-form-submit']" oas="http://jira-stock....,//*[@id='login-form-username'],su...,//*[@id='login-form-password'],feQ44Rs7..2,//*[@id='login-form-submit']" sms="http://pscptest...,//*[@id='j_username'],XUD..5,//*[@id='j_password'],h..4,//*[@id='login_form']/table/tbody/tr[1]/td[3]/input" pafa5="http://sis-omm-mc...,//*[@id='uname'],CH..8,//*[@id='password'],...,//*[@id='ext-gen1018']/div/div/div/a"
相关文章推荐
- Java Web 系统单点登录的实现
- Java Web 系统单点登录的实现
- java web中实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)
- JAVA实现web登录
- java网络编程五:暴力法破解登录系统的完全实现(木子)
- 简单java实现单用户登录系统(不是单点登录)
- Java单点登录在校园身份管理系统中的实现
- 【Java】运用JDBC实现一个注册、登录系统的编写
- java实现web长时间自动登录
- php如何实现web系统单点登录
- java web 项目 图书管理系统的设计与实现
- Java模拟登录WEB系统的简单示例
- java_web用户的自动登录模块的实现
- 基于java-web的登录系统 密码及用户名固定
- php如何实现web系统单点登录
- java网络编程五:暴力法破解登录系统的完全实现
- java_web用户的自动登录模块的实现
- 轻量级单点登录系统最佳实践(四)——第3章 Web-SSO通常实现方式
- java web中实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)
- java网络编程五:暴力法破解登录系统的完全实现(木子)