1. 程式人生 > >自動化測試工具Selenium入門

自動化測試工具Selenium入門

文章目錄

簡介

寫過爬蟲的朋友一定遇到過這樣一個問題:網頁的部分內容是由js(ajax)技術生成的,而這部分內容恰恰是我們想要的,並且這個ajax請求的url或者生成的cookie資訊我們很難拿到。
普通的爬蟲工具例如HttpClient只能模擬http傳送請求,我們無法拿到url或者cookie,所以當然也無法拿到動態網頁內容;htmlunit具有js渲染的功能,某些場景下使用htmlunit是一個比較合適的選擇,但是 htmlunit對於某些複雜js卻顯得無能為力,關於htmlunit的使用,我們會在另外一篇部落格中詳細敘述。
Selenium,作為一個Web應用程式測試的工具,功能顯然更加強大。Selenium的核心Selenium Core基於JsUnit,完全由 JavaScript編寫,因此可運行於任何支援JavaScript的瀏覽器上。顯然,Selenium非常適合解決上述我們提到的動態網頁載入問題。
這裡我們主要講一下Selenium如何在java平臺上使用、以及一些使用過程中出現的坑。

入門

環境搭建

  1. 首先我們要準備一些jar包,裡面有一些api可供我們使用,具體如下:

    commons-codec-1.9.jar
    commons-collections-3.2.1.jar
    commons-exec-1.1.jar
    commons-io-2.4.jar
    commons-lang3-3.3.2.jar
    commons-logging-1.1.3.jar
    cssparser-0.9.14.jar
    guava-15.0.jar
    httpclient-4.3.4.jar
    httpcore-4.3.2.jar
    httpmime-4.3.3.jar
    jna-3.4.0.jar
    phantomjsdriver-1.2.0.jar
    selenium-api-2.41.0.jar
    selenium-chrome-driver-2.44.0.jar
    selenium-firefox-driver-2.44.0.jar
    selenium-htmlunit-driver-2.44.0.jar
    selenium-ie-driver-2.44.0.jar
    selenium-java-2.44.0.jar
    selenium-remote-driver-2.41.0.jar
    selenium-safari-driver-2.44.0.jar
    selenium-support-2.44.0.jar
    

其中版本是我自己選取,若要使用其他版本,大家可以自己嘗試哦。
2. 由於Selenium可以操作各種瀏覽器進行測試,所以在使用之前我們需要在電腦上安裝對應的瀏覽器。注意,Selenium版本與瀏覽器版本不一定相容,以下是FireFox和Chrome具體對應版本:
FireFox:

	2.25.0   ->  18
	2.30.0   ->  19
	2.31.0   ->  20
	2.42.2   ->  29
	2.44.0   ->  33 (不支援31)
	2.53.0   ->  43,46(不支援47)
	2.41.0   ->  26(綠色版本)
	2.44     ->  32.0-35.0
	2.53.0-2.53.6 ->  40.0.3

Chrome:

	2.29  ->  56-58
	2.28  ->  55-57
	2.27  ->  54-56
	2.26  ->  53-55
	2.25  ->  53-55
	2.24  ->  52-54
	2.23  ->  51-53
	2.22  ->  49-52
	2.21  ->  46-50
	2.20  ->  43-48
	2.19  ->  43-47
	2.18  ->  43-46
	2.17  ->  42-43
	2.13  ->  42-45
	2.15  ->  40-43
	2.14  ->  39-42
	2.13  ->  38-41
	2.12  ->  36-40
	2.11  ->  36-40
	2.10  ->  33-36
	2.9  ->  31-34
	2.8  ->  30-33
	2.7  ->  30-33
	2.6  ->  29-32
	2.5  ->  29-32
	2.4  ->  29-32
  1. java環境搭建部分此處略過。開啟Eclipse,新建Java Project,取名selenium_demo,然後將下載的jar包匯入工程。至此,環境搭建完畢。

程式碼實現

  1. 使用Selenium新建一個瀏覽器(這裡以火狐瀏覽器為例,我這裡是mac作業系統):

    String browserPath = "/Applications/Firefox.app/Contents/MacOS/firefox";
    System.setProperty("webdriver.firefox.bin", browserPath);
    WebDriver driver = new FirefoxDriver();
    

其中browserPath是火狐瀏覽器的驅動路徑。如果要新建一個PhantomJs瀏覽器,則程式碼如下:

	String browserPath = "/opt/app/PhantomJS/phantomjs-2.1.1-linux-x86_64/bin/phantomjs";
	System.setProperty("phantomjs.binary.path", browserPath);
	WebDriver driver = new PhantomJSDriver();
  1. 為Selenium設定代理:
    FireFox:

    String proxyIp = "10.10.10.1";
    int proxyPort = 80;
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.http", proxyIp);
    profile.setPreference("network.proxy.http_port", proxyPort);
    profile.setPreference("network.proxy.ssl", proxyIp);
    profile.setPreference("network.proxy.ssl_port", proxyPort);
    WebDriver driver = new FirefoxDriver(profile);
    

PhantomJs:

	DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
	Proxy proxy = new Proxy();
	proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
	proxy.setAutodetect(false);
	String proxyIp = "10.10.10.1";
	int proxyPort = 80;
	proxy.setHttpProxy(proxyIp + ":" + proxyPort);
	desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
	WebDriver driver = new PhantomJSDriver(desiredCapabilities);
  1. 開啟一個url並獲取對應的網頁原始碼:

    String url = "http://www.baidu.com";
    driver.get(url);
    String pageSource = driver.getPageSource();
    System.out.println(pageSource);
    
  2. 瀏覽器視窗最大化:

    driver.manage().window().maximize();
    
  3. 定位某個元素:

    根據ID定位元素:
    WebElement ele = driver.findElement(By.id("id"));
    根據Class定位元素:
    WebElement ele = driver.findElement(By.className("className"));
    根據xpath定位元素:
    WebElement ele = driver.findElement(By.xpath("xpath"));
    
  4. 向表單填資料:

    WebElement ele = driver.findElement(By.id("id"));
    ele.clear();
    ele.sendKeys("str");
    
  5. 觸發元素的點選事件:

    WebElement ele = driver.findElement(By.id("id"));
    ele.click();
    

一些坑

  1. 使用find_element_by_xxxx()方法查詢元素時,如果元素找不到,不會返回null,而是丟擲異常,所以需要自己捕獲異常。
  2. 使用firefox後,很多選項雖然在瀏覽器中進行了設定,但是通過selenium啟動firefox後,設定並沒有生效,所以這些設定你需要在程式碼中新增。
  3. 使用WebDriver點選Button元素時,如果Button元素其他元素遮住了,或沒出現在介面中(比如Button在頁面底部,但是螢幕只能顯示頁面上半部分),使用Click()方法可能無法觸發Click事件。