Selenium3.0 自動化測試


  早在2013年的時候,Selenium官方宣布,Selenium新的版本會在聖誕節的時候發布。但是,他們並沒有說哪一個聖誕節發布。

  轉眼的三年過去了,目前已經發布到Selenium3.0 beta4版本,這將會是Selenium3.0正式版本前的最后一個測試版本。

  盡管我對Selenium3.0比較失望(本以為它會集成移動端的自動化測試)。但是,它還是做了一些變動。

 

Selenium3.0的變化                 

  最大的變化應該是去掉了Selenium RC 了,這是必然的結果。Selenium RC Selenium1.0的產物,Selenium2.0WebDriver為主,經過這么多年有發展,Selenium RC已經很少有人在用了。Selenium3.0版本去掉是個必然的結果。

  • You’ll need to be running Java 8 to use the Java pieces of Selenium. This is the oldest version of Java officially supported by Oracle, so hopefully you’re using it already!

  Selenium3.0只支持Java8版本以上,所以,如果你是用Java+Selenium開發自動化測試,那么Java JDK需要升級到Java8了,對於其它編程來說可以忽略這點,除非你要使用Selenium Grid

  Selenium3.0中的Firefox驅動獨立了,在Selenium3.0之前,只要在不同編程語言下安裝好Selenium就可以驅動Firefox瀏覽器運行自動化測試腳本。這是因為不同語言下的Selenium庫中移動包含了Firefox瀏覽驅動。

  然而,現在Firefox瀏覽器驅動與Selenium庫分離,單獨提供下載。

  下載地址:https://github.com/mozilla/geckodriver/releases

  不過,geckodriver驅動要求Friefox瀏覽器必須48版本以上。

  • Support for Safari is provided on macOS (Sierra or later) via Apple’s own safaridriver.

  Safari是蘋果公司的瀏覽器,然后,它也早就實現了多平台的支持,同樣可以在Windows下運行,然而,它的驅動比較有意思,是集成到Selenium Server中的。也就是說你想讓自動化測試腳本在Safari瀏覽器上運行,必須使用Selenium Server

  • Support for Edge is provided by MS through their webdriver server.
  • Only versions 9 or above of IE are supported. Earlier versions may work, but are no longer supported as MS no longer supports them.

 

如何使用瀏覽器驅動                                       

   讀者可以單獨創建一個目錄,如:D:/drivers/ ,把不同瀏覽器的驅動都放到該目錄。geckodriver.exeFirefox)、chromedriver.exeChrome)、MicrosoftWebDriver.exeEdge)、IEDriverServer.exeIE)、operadriver.exeOpera)等。

  然后,將D:/drivers/添加到系統環境變最path下面即可。

 

Python安裝Selenium3.0                                                   

 通過pip安裝,3.0.0b3為當前最新版本。

>pip install selenium==3.0.0b3

Selenium3.0API沒有任何改變,跑個簡單的例子驗證一下。 

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("Selenium2")
driver.find_element_by_id("su").click()

driver.quit()

 

 

Java安裝Selenium3.0                                                        

  下載Selenium Server 3.0.0-beta4為當前最新版本:http://www.seleniumhq.org/download/

 

   打開Eclipse,導入:如下圖:

 

  同樣通過一個簡單的例子來驗證Selenium3.0工作正常。 

package base.test.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;

public class BaiduTest {
    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.baidu.com/");
        driver.findElement(By.id("kw")).sendKeys("selenium java");
        driver.findElement(By.id("su")).click();
        driver.quit();
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM