博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web自动化框架抽取示例【Java+selenium】
阅读量:5064 次
发布时间:2019-06-12

本文共 13418 字,大约阅读时间需要 44 分钟。

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();

       }

}

转载于:https://www.cnblogs.com/Nancy-Lee/p/11043859.html

你可能感兴趣的文章
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
发送请求时params和data的区别
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
如何增强你的SharePoint 团队网站首页
查看>>
FZU 1914 Funny Positive Sequence(线性算法)
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
MySQL服务读取参数文件my.cnf的规律研究探索
查看>>
java string(转)
查看>>
__all__有趣的属性
查看>>
写博客
查看>>
利用循环播放dataurl的视频来防止锁屏:NoSleep.js
查看>>
python3 生成器与迭代器
查看>>
java编写提升性能的代码
查看>>
ios封装静态库技巧两则
查看>>