一、可自动切换登陆不一样系统php
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 { public static void main(String[] args) throws InterruptedException { System.out.println("请输入须要登陆的系统编号:"); System.out.println("1.后管平台;2.testlink;3.jira-PASTOCK;4.jira-oas;5.短信平台;6.pafa5运管平台。"); System.setProperty("webdriver.chrome.driver", "D:/myselenium/selenium-kuanjia2016/chromedriver_win32/chromedriver.exe"); Scanner scan = new Scanner(System.in);//Scanner类能够直接接收控制台命令行的输入 int chooseNo = scan.nextInt();//将输入信息的下一个标记扫描为一个 int scan.close(); WebDriver driver = new ChromeDriver(); loginStart(chooseNo,"D:/myselenium/ZDHTest/selenium/loginData.properties",driver); } public static void loginStart(int num,String path,WebDriver driver){ //String[] elements = getPropertiesValue(chooseUrl(num),path).replace("\"","").split(",");//\"是表明转义即表明一个双引号 String[] elements = getPropertiesValue(chooseUrl(num),path).split(","); System.out.println(elements[0]); 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"; } } //get properties file content public static HashMap<String,String> readProperties(String filePath){ Properties pp = new Properties(); HashMap<String,String> strMap = new HashMap<String,String>();//HashMap类是用来存放键与值的映射 FileInputStream fin = null; try { fin = new FileInputStream(filePath); pp.load(fin); } catch (IOException e) { e.printStackTrace(); } Enumeration enums = pp.propertyNames(); //实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素 while(enums.hasMoreElements()){//hasMoreElements当且仅当此枚举对象至少还包含一个可提供的元素时,才返回 true;不然返回 false String strKey = (String)enums.nextElement();//nextElement若是此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素 strMap.put(strKey, pp.getProperty(strKey));//把键与值添加到HashMap中--在此映射中关联指定值与指定键。 } return strMap; } //根据指定key获取value public static String getPropertiesValue(String key,String path){ HashMap<String,String> mpValue = readProperties(path); return mpValue.get(key);//返回指定键所映射的值;若是对于该键来讲,此映射不包含任何映射关系,则返回 null } }
2\批处理LoginAdmin.bathtml
@echo off echo "自动化登陆" javac -encoding utf8 "D:\myselenium\ZDHTest\selenium\LoginAdmin.java" cd "D:\myselenium\ZDHTest\selenium" java LoginAdmin echo "3秒后窗口将关闭...ping -n 3 127.0.0.1 >nul " echo "pause goto 1--这个执行完后不关闭命令窗口" ping -n 3 127.0.0.1 >nul
3\配置文件内容以下:java
ant="https://soasadmin-stg.paic.com.cn/admin/admin/login.html,/html/body/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/input,SUBAOYA853,/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.paic.com.cn/testlink/login.php?note=expired&destination=%2Ftestlink%2Findex.php%3Fcaller%3Dlogin,//*[@id='login'],subaoya023,//*[@id='login_div']/form/p[2]/input,feQ44Rs70112,//*[@id='login_div']/form/input[5]" PASTOCK="http://jira-stock.paic.com.cn:8080/login.jsp?permissionViolation=true&os_destination=%2Fbrowse%2FPASTOCK-1928%3Ffilter%3D-2&page_caps=&user_role=,//*[@id='login-form-username'],subaoya023,//*[@id='login-form-password'],feQ44Rs70112,//*[@id='login-form-submit']" oas="http://jira-stock.paic.com.cn:8080/login.jsp?permissionViolation=true&os_destination=%2Fbrowse%2FOAS-2&page_caps=&user_role=,//*[@id='login-form-username'],subaoya023,//*[@id='login-form-password'],feQ44Rs70112,//*[@id='login-form-submit']" sms="http://pscptest-10.paic.com.cn:42154/pscp/login.jsp#,//*[@id='j_username'],XUDAN005,//*[@id='j_password'],hello1234,//*[@id='login_form']/table/tbody/tr[1]/td[3]/input" pafa5="http://sis-omm-mcc-uat.paic.com.cn/admin/login.html,//*[@id='uname'],CHENZHIXU888,//*[@id='password'],,//*[@id='ext-gen1018']/div/div/div/a"
同一效果,经本身改造如下是一种更简单易懂的写法:web
一、代码以下:可直接执行(得修改配置文件路径,及一些参数值,好比URL,用户名密码等等)chrome
import java.io.FileInputStream; import java.io.IOException; 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; public class LoginTest { public static void main(String[] args) throws InterruptedException { System.out.println("请输入须要登陆的系统编号:"); System.out.println("1.后管平台;2.testlink;3.jira-PASTOCK;4.jira-oas;5.短信平台;6.pafa5运管平台。"); System.setProperty("webdriver.chrome.driver", "D:/myselenium/selenium-kuanjia2016/chromedriver_win32/chromedriver.exe"); Scanner scan = new Scanner(System.in);//Scanner类能够直接接收控制台命令行的输入 int chooseNo = scan.nextInt();//将输入信息的下一个标记扫描为一个 int scan.close(); WebDriver driver = new ChromeDriver(); loginStart(chooseNo,"D:/myselenium/ZDHTest/selenium/loginData.properties",driver); } public static Properties getProperties(String filePath){ Properties pp = new Properties(); FileInputStream fin = null; try { fin = new FileInputStream(filePath); pp.load(fin); } catch (IOException e) { e.printStackTrace(); } return pp; } public static void loginStart(int num,String path,WebDriver driver){ Properties pp = getProperties(path); //String[] elements = getPropertiesValue(chooseUrl(num),path).replace("\"","").split(","); //String[] elements = getPropertiesValue(chooseUrl(num),path).split(","); String[] elements = pp.getProperty(chooseUrl(num)).split(","); /*for (String s:elements ){ System.out.println(s);//依次输出数组中的全部元素 }*/ 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"; } } }
二、配置修改文件以下(去掉双引号,由于配置文件中的键与值都是String型,可省略双引号):数组
ant=https://soasadmin-stg.paic.com.cn/admin/admin/login.html,/html/body/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/input,SUBAOYA853,/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.paic.com.cn/testlink/login.php?note=expired&destination=%2Ftestlink%2Findex.php%3Fcaller%3Dlogin,//*[@id='login'],subaoya023,//*[@id='login_div']/form/p[2]/input,feQ44Rs70112,//*[@id='login_div']/form/input[5] PASTOCK=http://jira-stock.paic.com.cn:8080/login.jsp?permissionViolation=true&os_destination=%2Fbrowse%2FPASTOCK-1928%3Ffilter%3D-2&page_caps=&user_role=,//*[@id='login-form-username'],subaoya023,//*[@id='login-form-password'],feQ44Rs70112,//*[@id='login-form-submit'] oas=http://jira-stock.paic.com.cn:8080/login.jsp?permissionViolation=true&os_destination=%2Fbrowse%2FOAS-2&page_caps=&user_role=,//*[@id='login-form-username'],subaoya023,//*[@id='login-form-password'],feQ44Rs70112,//*[@id='login-form-submit'] sms=http://pscptest-10.paic.com.cn:42154/pscp/login.jsp#,//*[@id='j_username'],XUDAN005,//*[@id='j_password'],hello1234,//*[@id='login_form']/table/tbody/tr[1]/td[3]/input pafa5=http://sis-omm-mcc-uat.paic.com.cn/admin/login.html,//*[@id='uname'],CHENZHIXU888,//*[@id='password'],,//*[@id='ext-gen1018']/div/div/div/a
三、执行,输入1或2或其余相应的数值均可执行相应的程序,进入不一样的URL进行验证;也能够本身写批处理文件在控制台执行jsp