web自动化测试框架抽取示例
例子:测试登录模块,对登录的账号和密码进行不同的case校验。
1.1.1 无优化代码login_1
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver;
public class login_1 { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("测试url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001"); driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close();
System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver1 = new ChromeDriver(); driver1.get("测试url"); driver1.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001"); driver1.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111"); driver1.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver1.close();
System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver2 = new ChromeDriver(); driver2.get("测试url"); driver2.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001"); driver2.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver2.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver2.close(); } } |
1.1.2 初步抽取login_2
以上代码实现账号、密码的三种验证,下面首先对这三种验证进行方法的抽取,代码如下:
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class login_2 { public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); } private static void case_1() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("测试url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_2() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("测试url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_3() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("测试url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } } |
方法抽取方式如下:
在main函数中选取需要抽取方法的代码,右键选择refactor-extract method即可进行抽取。
1.1.3 工具类封装login_3
对生成的chrome驱动进行工具类的封装。
package com.lee.auto.testFrome; import org.openqa.selenium.chrome.ChromeDriver; public class exploreTool { //方法的抽取 public static ChromeDriver getChromeDriver() { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); return driver; } } |
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class login_3 { public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); } private static void case_1() throws InterruptedException { ChromeDriver driver = exploreTool.getChromeDriver(); driver.get("测试url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_2() throws InterruptedException { ChromeDriver driver1 = exploreTool.getChromeDriver(); driver1.get("测试url"); driver1.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001"); driver1.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111"); driver1.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver1.close(); } private static void case_3() throws InterruptedException { ChromeDriver driver2 = exploreTool.getChromeDriver(); driver2.get("测试url"); driver2.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001"); driver2.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver2.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver2.close(); } } |
下面代码为三个浏览器进行登录实现。
package com.lee.auto.testFrome;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerDriverService; import org.openqa.selenium.remote.DesiredCapabilities;
public class exploreTool { // 方法的抽取 public static ChromeDriver getChromeDriver() { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); return driver; }
public static FirefoxDriver getFirefoxDriver() { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); FirefoxDriver driver = new FirefoxDriver(); return driver; }
public static InternetExplorerDriver getIEDriver() { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); InternetExplorerDriver driver = new InternetExplorerDriver(); return driver; } } |
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class login_4 {
public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException { ChromeDriver driver = exploreTool.getChromeDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException { FirefoxDriver driver = exploreTool.getFirefoxDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException { InternetExplorerDriver driver = exploreTool.getIEDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click点击无效,采用keys.enter方式进行登录 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |
IE报错1:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information) |
解决方法:需要把IE安全设置的四个区域的保护模式设置一致。
IE问题2:
页面可以找到登录元素,但是click无效。
解决方法:采用keys.enter方式进行登录。
IE问题3:
找到账号、密码文本框后输入值特别慢。
解决方法:将IEDriverServer由64位的换为32位的。
1.1.4 多态的使用login_5
对于多个浏览器的驱动生成,可以借助webdriver接口进行多态实现。
通过传入的参数值来判断所需的浏览器类型。
package com.lee.auto.login;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class exploreTool_1 {
public static WebDriver getexploreDriver(String exploreType) {
WebDriver driver = null; //判断为chrome if (exploreType.equals("chrome")) { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); driver = new ChromeDriver(); } //判断为Firefox else if (exploreType.equals("firefox")) { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); driver = new FirefoxDriver(); } //判断为IE else if (exploreType.equals("ie")) { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); driver = new InternetExplorerDriver(); } return driver; } } |
package com.lee.auto.login;
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver;
public class login_5 {
public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("chrome"); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("firefox"); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("ie"); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click点击无效,采用keys.enter方式进行登录 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |
1.1.5 配置文件的使用login_6
若多条用例都需要通过exploreTool_1.getexploreDriver方法来获取浏览器驱动,一旦需要切换浏览器,就需要大量修改代码来完成。为了避免此问题,将变动数据采用配置文件方式传入,实现数据和代码的分离。
1.1.5.1新建配置文件
新建配置文件,自定义参数值。
参数值定义格式:
key = value
1.1.5.2读取参数
步骤如下:
- 创建属性对象
- 加载配置文件
- 获取文件中属性值
auto.browserType = ie |
package com.lee.auto.login; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class exploreTool_2 {
public static WebDriver getexploreDriver() { //读取配置文件 //创建属性对象 Properties properties = new Properties(); //对加载的文件抛出找不到文件时的异常 try { //通过属性对象加载文件 properties.load(new FileInputStream("src/config.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //获取配置文件中的属性值 String exploreType = properties.getProperty("auto.browserType");
WebDriver driver = null; //判断为chrome if (exploreType.equals("chrome")) { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); driver = new ChromeDriver(); } //判断为Firefox else if (exploreType.equals("firefox")) { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); driver = new FirefoxDriver(); } //判断为IE else if (exploreType.equals("ie")) { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); driver = new InternetExplorerDriver(); } return driver; } } |
package com.lee.auto.login; import java.io.FileNotFoundException; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver;
public class login_6 {
public static void main(String[] args) throws InterruptedException{ case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("1111"); driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("测试url"); driver.findElement(By.id("username")).sendKeys(""); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click点击无效,采用keys.enter方式进行登录 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |