使用Selenium通过浏览器对网站进行自动化测试和相关问题

使用Selenium通过浏览器对网站进行自动化测试

 

自动化测试概念:

 

一般是指软件测试的自动化,软件测试就是在预设条件下运行系统或应用程序,评估运行结果,预先条件应包括正常条件和异常条件。

 

广义上来讲,自动化包括一切通过工具或者运行程序的方式来代替或辅助手工测试的行为都可以看做自动化,包括性能测试工具(loadrunnerjmeter,或自己所写的一段程序,用于生成测试数据。狭义上来讲,通工具记录或编写脚本的方式模拟手工测试的过程,通过回放或运行脚本来执行测试用例,从而代替人工对系统的功能进行验证。

 

我们需要规范的来做单元测试同样需要相应的单元测试框架,如javaJunittestNGC#NUnit python unittestpytest 等,几乎所有的主流语言,都会有其对应的单元测试框架。

 

自动化测试原理:

 

自动化测试分为动态测试和静态测试。

 

动态测试:通过特定的程序来模拟软件的操作过程或操作行为,然后对软件所作出的反应或者输出的结果进行检查和验证。

 

静态测试:按照代码规范和软件开发的最佳实践建立各种代码规则,然后依据这些规则对代码进行自动扫描,发现和规则不匹配的各种问题。

 

自动化测试工具的原理其实都一样,都是通过调用IE COM接口和HTML DOM IEchromeFirefox浏览器以及WEB测试对象的操作Android客户端的则是通过获取元素的ID以及测试对象来进行测试

 

适合于使用自动化测试的项目要求:

 

首先考虑产品是否适合做自动化测试。这方法比较普遍的共识是从三个方面进行权衡

 

1、软件需求变动不频繁

 

出于测试维护成本的考虑,如果软件的需求变动过于频繁,测试人员就要根据变动的需求来更新测试用例以及相关的测试脚本,而脚本的维护本身就是一个代码开发的过程,需要修改、调试,必要的时候还要修改自动化测试的框架,如果所花费的成本不低于利用其节省的测试成本,那么自动化测试便是失败的。

 

项目中针对于某些相对稳定的模块,可以编写自动化测试脚本进行自动化测试,而某些变动较频繁的模块建议采用手工测试。

 

2、项目周期较长

 

由于自动化测试需求的确定、自动化测试框架的设计、测试脚本的编写与调试均需要相当长的时间来完成。这样的过程本身就是一个测试软件的开发过程,需要较长的时间来完成。如果项目的周期比较短,没有足够的时间去支持这样一个过程,那么自动化测试便成为笑谈。

 

3、自动化测试脚本可重复使用

 

自动化测试脚本的重复使用要从三个方面来考量,一方面所测试的项目之间是否很大的差异性(如C/S系统和B/S系统的差异);所选择的测试工具是否适应这种差异;最后,测试人员是否有能力开发出适应这种差异的自动化测试框架。自动化测试脚本重复使用可大大的减少测试成本。

 

 

 

自动化测试使用的工具:

 

首先要先确认你所测试的产品是桌面程序(C/S)还是web应用(B/S)。

 

  桌面程序的工具有:QTPAutoRunnerAppium

 

  web应用的工具有:QTPAutoRunnerRobot Frameworkwatirselenium

 

selenium 是支持javapythonrubyphpC#JavaScript

 

  从语言易学性来讲,首选ruby python

 

  从语言应用广度来讲,首选javaC#php

 

  从语言相关测试技术成度来讲:ruby ,python ,java

 

  或者你可以考虑整个技术团队主流用什么语言,然后选择相应的语言。

 

1、需要的工具

  (1)eclipse、maven

  (2)新建一个maven项目

  (3)下载好FireFox、Chrome、IE,最好是默认安装

  (4)下载好chromedriver.exe和IEDriverServer.exe

  建立好的maven工程如下:

 

2、配置pom.xml文件

  加入以下代码,自动下载相关jar包:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.49.1</version>
</dependency>

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>Test</groupId>
 6   <artifactId>test</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <packaging>jar</packaging>
 9 
10   <name>test</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <dependencies>
18     <dependency>
19       <groupId>junit</groupId>
20       <artifactId>junit</artifactId>
21       <version>3.8.1</version>
22       <scope>test</scope>
23     </dependency>
24     <dependency>
25       <groupId>org.seleniumhq.selenium</groupId>
26       <artifactId>selenium-java</artifactId>
27       <version>2.49.1</version>
28    </dependency>
29   </dependencies>
30 </project>

 3、下载相关jar包

  (1)右键工程--》选择【Maven】--》【Download Sources】;

  (2)等待下载完相关jar包后,选择【Maven】--》【Update Project...】

4、新建测试类进行不同浏览器的自动化测试

  (1)新建一个ExampleForChrome.java类测试chrome浏览器:

 1 package Test.test;
 2 
 3 import java.io.IOException;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.WebElement;
 8 import org.openqa.selenium.chrome.ChromeDriver;
 9 import org.openqa.selenium.chrome.ChromeOptions;
10 import org.openqa.selenium.support.ui.ExpectedCondition;
11 import org.openqa.selenium.support.ui.WebDriverWait;
12 
13 public class ExampleForChrome {
14     public static void main(String[] args) throws IOException {
15         // 配置chromeDriver
16         System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
17       //  System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
18         // 创建一个chrome的浏览器实例
19         
20         
21         ChromeOptions options = new ChromeOptions();
22         //通过配置参数禁止data;的出现
23          options.addArguments("--user-data-dir=C:/Users/Dell/AppData/Local/Google/Chrome/User Data/Default");
24           //通过配置参数删除“您使用的是不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性会有所下降。”提示
25         options.addArguments("--start-maximized","allow-running-insecure-content", "--test-type");
26         WebDriver driver;
27         driver = new ChromeDriver(options);
28         // 让浏览器访问www.baidu.com
29         driver.get("http://www.baidu.com");
30         // 获取网页的Title
31         System.out.println("Baidu's Title is:" + driver.getTitle());
32         // 通过id找到搜索输入框的DOM
33         WebElement element = driver.findElement(By.id("kw"));
34         // 在搜索框内输入关键字
35         element.sendKeys("上海");
36         // 提交搜索输入框所在的表单
37         element.submit();
38         // 等待10秒让搜索页面加载,判断新页面的Title为“上海”则说明新页面加载完毕
39         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
40             public Boolean apply(WebDriver d) {
41                 return d.getTitle().startsWith("上海");
42             }
43         });
44         // 显示新页面的Title
45         System.out.println("new page's title is:" + driver.getTitle());
46         // 测试完毕,关闭浏览器
47         driver.quit();
48     }
49 }

  (2)新建一个ExampleForFireFox.java类测试Firefox浏览器:

 1 package Test.test;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.firefox.FirefoxDriver;
 7 import org.openqa.selenium.support.ui.ExpectedCondition;
 8 import org.openqa.selenium.support.ui.WebDriverWait;
 9 
10 public class ExampleForFireFox {
11     public static void main(String[] args) {
12         // 如果你的FireFox没有安装在默认的目录,那么必须在程序中设置,告知去哪里寻找FireFox
13    //     System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe");
14     //    System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
15         // 创建一个FireFox的浏览器实例(因为selenium是对Firefox默认支持的,所以不需要浏览器驱动就可直接创建运行)
16         WebDriver driver = new FirefoxDriver();
17         // 让浏览器访问www.baidu.com
18         driver.get("http://www.baidu.com");
19         // 获取网页的Title
20         System.out.println("Baidu's Title is:" + driver.getTitle());
21         // 通过id找到搜索输入框的DOM
22         WebElement element = driver.findElement(By.id("kw"));
23         // 在搜索框内输入关键字
24         element.sendKeys("上海");
25         // 提交搜索输入框所在的表单
26         element.submit();
27         // 等待10秒让搜索页面加载,判断新页面的Title为“上海”则说明新页面加载完毕
28         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
29             public Boolean apply(WebDriver d) {
30                 return d.getTitle().startsWith("上海");
31             }
32         });
33         // 显示新页面的Title
34         System.out.println("new page's title is:" + driver.getTitle());
35         // 测试完毕,关闭浏览器
36         driver.quit();
37     }
38 }

  (2)新建一个ExampleForIE.java类测试IE浏览器:

 1 package Test.test;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.ie.InternetExplorerDriver;
 7 import org.openqa.selenium.remote.DesiredCapabilities;
 8 import org.openqa.selenium.support.ui.ExpectedCondition;
 9 import org.openqa.selenium.support.ui.WebDriverWait;
10 
11 public class ExampleForIE {
12     public static void main(String[] args) {
13         // 配置ieDriver
14         System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer_x64_2.53.1.exe");
15         DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
16         ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
17         // 创建一个IE的浏览器实例
18         WebDriver driver = new InternetExplorerDriver(ieCapabilities);
19         // 让浏览器访问www.baidu.com
20         driver.get("http://www.baidu.com");
21         // 获取网页的Title
22         System.out.println("Baidu's Title is:" + driver.getTitle());
23         // 通过id找到搜索输入框的DOM
24         WebElement element = driver.findElement(By.id("kw"));
25         // 在搜索框内输入关键字
26         element.sendKeys("上海");
27         // 提交搜索输入框所在的表单
28         element.submit();
29         // 等待10秒让搜索页面加载,判断新页面的Title为“上海”则说明新页面加载完毕
30         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
31             public Boolean apply(WebDriver d) {
32                 return d.getTitle().startsWith("上海");
33             }
34         });
35         // 显示新页面的Title
36         System.out.println("new page's title is:" + driver.getTitle());
37         // 测试完毕,关闭浏览器
38         driver.quit();
39     }
40 }

5、运行结果

  【右键】--》选择【Run As】--》【Java Application】运行:

注意事项和相关说明: 

 关于Firefox浏览器:

  1、Selenium天然支持FireFox,所以在使用FireFox的时候不需要去下载驱动,只需要指定FireFox的安装目录即可。甚至,如果你的FireFox是默认安装的,那么就不需要如下语句:System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");如上代码所示,我的FireFox是安装在C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe下面的。 
  但是,如果使用Chrome或者IE就需要下载相应的驱动程序:chromedriver.exeIEDriverServer.exe然后在代码中指定这两个驱动的存放位置就可以了。此处我的Chrome和IE浏览器都是默认安装的。

  2、selenium与Firefox版本不兼容的问题,可以更新selenium最新版本或者降低Firefox版本。

【Selenium】 -> 【FireFox】
   2.25.0     ->      18
   2.30.0     ->      19
   2.31.0     ->      20
   2.42.2     ->      29
   2.44.0     ->      33 (不支持31,2014/12/1)
若selenium的版本和firefox不兼容,需要升级selenium的jar包,或者是升级firefox。
切记,关掉forefox的升级功能,否则连本地Windows上的脚本都跑不起来,且必须降级forefox。

  可以参考博文:http://www.cnblogs.com/tester808/p/6674588.html    

  http://blog.csdn.net/solami/article/details/6728544  

  http://blog.csdn.net/wanglha/article/details/50159397

网上找到的一些解决方法:

Firefox 45版本以上使用selenium-3.0.1无法直接启动需要如下步骤:

(1)    下载geckodriver.exe

https://github.com/mozilla/geckodriver/releases

解压后放置到

1.查看C:\Python27\Lib\site-packages\selenium\webdriver\firefox中的webdriver.py,在def_init_函数中,executable_path="geckodriver",之前搭建的环境上是executable_path="wires";

2.geckodriver是一原生态的第三方浏览器,对于selenium3.x版本都会使用geckodriver来驱动firefox,所以需要下载geckodriver.exe,下载地址:https://github.com/mozilla/geckodriver/releases

3.放在C:\Python27(查看环境变量path中是否添加C:\Python27该路径)、

(2)

from selenium import webdriver
fromselenium.webdriver.common.desired_capabilities import DesiredCapabilities

fromselenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r'C:\ProgramFiles (x86)\Mozilla Firefox\firefox.exe')

driver =webdriver.Firefox(firefox_binary=binary)

driver.get('http://www.google.com')

 

  关于IE浏览器:

 1、IE浏览器需要关闭安全保护模式,不然就会打开不了网页。

  2、针对windows vista和windows 7上的IE7或者更高的版本,必须在IE选项设置的安全页中,4个区域的启用保护模式的勾选都去掉(或都勾上),即保持四个区域的保护模式是一致的。如下图所示:

  3、针对IE10和更高的版本,必须在IE选项设置中的高级页中,取消增强保护模式。如下图所示:

  4、浏览器的缩放比例必须设置为100%,这样元素定位才不会出现问题,如下图所示:

  5、针对IE11,需要修改注册表。如果是32位的windows,key值为

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE

,如果是64位的windows,key值为

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE

如果key值不存在,就添加。之后在key内部创建一个iexplorer.exe,DWORD类型,值为0,我的windows是64位的,修改后的注册表如下图所示: 

之后,重新运行Java文件就可以正常打开浏览器了。

  关于chrome浏览器:

  当运行测试文件的时候,能够打开浏览器,但会出现【data;】的提示:如下图所示:

为了关闭这个提示,可以在Java类中添加以下代码:

ChromeOptions options = new ChromeOptions();
        //通过配置参数禁止data;的出现
         options.addArguments("--user-data-dir=C:/Users/Dell/AppData/Local/Google/Chrome/User Data/Default");
          //通过配置参数删除“您使用的是不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性会有所下降。”提示
        options.addArguments("--start-maximized","allow-running-insecure-content", "--test-type");
        WebDriver driver;
        driver = new ChromeDriver(options);

 

以上是我win10 系统,Dell电脑的设置。如果是win7 的系统,可以参考以下设置:

ChromeOptions options = new ChromeOptions();
    //通过配置参数禁止data;的出现
     options.addArguments("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default");
      //通过配置参数删除“您使用的是不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性会有所下降。”提示
    options.addArguments("--start-maximized",
   "allow-running-insecure-content", "--test-type");
   driver = new ChromeDriver(options);

 

   如果以上设置完成,重新运行时出现以下错误:

可以看出,是chrome浏览器与chromeDriver.exe版本不兼容的问题,可以下载chromeDriver.exe的最新版本。

 

 

 

网上找了遇到相同问题的解决办法 http://blog.csdn.net/u012246342/article/details/52860949

下载v2.24版本的chromedriver.exe替换原有的chromedriver.exe(下载地址: http://chromedriver.storage.googleapis.com/index.html?path=2.24/ );

重新运行成功!

网上找到的相关资料

下面是关于加载Chrome配置的方法(网上copy的,保存留用): 
  一、加载所有Chrome配置
  用Chrome地址栏输入chrome://version/,查看自己的“个人资料路径”,然后在浏览器启动时,调用这个配置文件,代码如下:
  #coding=utf-8
  from selenium import webdriver
  option = webdriver.ChromeOptions()
  option.add_argument('--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data') #设置成用户自己的数据目录
  driver = webdriver.Chrome(chrome_options=option)


  二、修改浏览器的User-Agent来伪装你的浏览器访问手机m站
  #coding=utf-8
  from selenium import webdriver
  option = webdriver.ChromeOptions()
  option.add_argument('--user-agent=iphone')
  driver = webdriver.Chrome(chrome_options=option)
  driver.get('http://www.taobao.com/')


  三、浏览器启动时安装crx扩展
  #coding=utf-8
  from selenium import webdriver
  option = webdriver.ChromeOptions()
  option.add_extension('d:\crx\AdBlock_v2.17.crx') #自己下载的crx路径
  driver = webdriver.Chrome(chrome_options=option)
  driver.get('http://www.taobao.com/')

posted @ 2017-07-21 10:47  名曰大神  阅读(646)  评论(0编辑  收藏  举报