使用Selenium經過瀏覽器對網站進行自動化測試和相關問題

使用Selenium經過瀏覽器對網站進行自動化測試php

 

自動化測試概念:

 

通常是指軟件測試的自動化,軟件測試就是在預設條件下運行系統或應用程序,評估運行結果,預先條件應包括正常條件和異常條件。html

 

廣義上來說,自動化包括一切經過工具或者運行程序的方式來代替或輔助手工測試的行爲均可以看作自動化,包括性能測試工具(loadrunnerjmeter,或本身所寫的一段程序,用於生成測試數據。狹義上來說,通工具記錄或編寫腳本的方式模擬手工測試的過程,經過回放或運行腳原本執行測試用例,從而代替人工對系統的功能進行驗證。java

 

咱們須要規範的來作單元測試一樣須要相應的單元測試框架,如javaJunittestNGC#NUnit python unittestpytest 等,幾乎全部的主流語言,都會有其對應的單元測試框架。python

 

自動化測試原理:

 

自動化測試分爲動態測試和靜態測試。git

 

動態測試:經過特定的程序來模擬軟件的操做過程或操做行爲,而後對軟件所做出的反應或者輸出的結果進行檢查和驗證。github

 

靜態測試:按照代碼規範和軟件開發的最佳實踐創建各類代碼規則,而後依據這些規則對代碼進行自動掃描,發現和規則不匹配的各類問題。web

 

自動化測試工具的原理其實都同樣,都是經過調用IE COM接口和HTML DOM IEchromeFirefox瀏覽器以及WEB測試對象的操做Android客戶端的則是經過獲取元素的ID以及測試對象來進行測試chrome

 

適合於使用自動化測試的項目要求:

 

首先考慮產品是否適合作自動化測試。這方法比較廣泛的共識是從三個方面進行權衡apache

 

一、軟件需求變更不頻繁windows

 

出於測試維護成本的考慮,若是軟件的需求變更過於頻繁,測試人員就要根據變更的需求來更新測試用例以及相關的測試腳本,而腳本的維護自己就是一個代碼開發的過程,須要修改、調試,必要的時候還要修改自動化測試的框架,若是所花費的成本不低於利用其節省的測試成本,那麼自動化測試即是失敗的。

 

項目中針對於某些相對穩定的模塊,能夠編寫自動化測試腳本進行自動化測試,而某些變更較頻繁的模塊建議採用手工測試。

 

二、項目週期較長

 

因爲自動化測試需求的肯定、自動化測試框架的設計、測試腳本的編寫與調試均須要至關長的時間來完成。這樣的過程自己就是一個測試軟件的開發過程,須要較長的時間來完成。若是項目的週期比較短,沒有足夠的時間去支持這樣一個過程,那麼自動化測試便成爲笑談。

 

三、自動化測試腳本可重複使用

 

自動化測試腳本的重複使用要從三個方面來考量,一方面所測試的項目之間是否很大的差別性(如C/S系統和B/S系統的差別);所選擇的測試工具是否適應這種差別;最後,測試人員是否有能力開發出適應這種差別的自動化測試框架。自動化測試腳本重複使用可大大的減小測試成本。

 

 

 

自動化測試使用的工具:

 

首先要先確認你所測試的產品是桌面程序(C/S)仍是web應用(B/S)。

 

  桌面程序的工具備:QTPAutoRunnerAppium

 

  web應用的工具備:QTPAutoRunnerRobot Frameworkwatirselenium

 

selenium 是支持javapythonrubyphpC#JavaScript

 

  從語言易學性來說,首選ruby python

 

  從語言應用廣度來說,首選javaC#php

 

  從語言相關測試技術成度來說:ruby ,python ,java

 

  或者你能夠考慮整個技術團隊主流用什麼語言,而後選擇相應的語言。

 

一、須要的工具

  (1)eclipse、maven

  (2)新建一個maven項目

  (3)下載好FireFox、Chrome、IE,最好是默認安裝

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

  創建好的maven工程以下:

 

二、配置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>

 三、下載相關jar包

  (1)右鍵工程--》選擇【Maven】--》【Download Sources】;

  (2)等待下載完相關jar包後,選擇【Maven】--》【Update Project...】

四、新建測試類進行不一樣瀏覽器的自動化測試

  (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 }

五、運行結果

  【右鍵】--》選擇【Run As】--》【Java Application】運行:

注意事項和相關說明: 

 關於Firefox瀏覽器:

  一、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瀏覽器都是默認安裝的。

  二、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瀏覽器:

 一、IE瀏覽器須要關閉安全保護模式,否則就會打開不了網頁。

  二、針對windows vista和windows 7上的IE7或者更高的版本,必須在IE選項設置的安全頁中,4個區域的啓用保護模式的勾選都去掉(或都勾上),即保持四個區域的保護模式是一致的。以下圖所示:

  三、針對IE10和更高的版本,必須在IE選項設置中的高級頁中,取消加強保護模式。以下圖所示:

  四、瀏覽器的縮放比例必須設置爲100%,這樣元素定位纔不會出現問題,以下圖所示:

  五、針對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的,保存留用): 
  1、加載全部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)


  2、修改瀏覽器的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/')


  3、瀏覽器啓動時安裝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/')

相關文章
相關標籤/搜索