Selenium 2移轉到3的重大改變 – Firefox啟動方式

Selenium 2移轉到3的重大改變 – Firefox啟動方式

Image result for selenium 3

Selenium 3架構上的改變導致原本Selenium 2自動化測試無法正常啟動FireFox

這篇文主要講解如何解決這個問題?

Selenium3啟動FireFox的錯誤訊息

原本在 Selenium 2的環境下 , 不需要其他的設定或是下載其他Browser Webdriver 就可以啟動firefox

範例程式如下:

[pastacode lang=”java” manual=”import%20org.openqa.selenium.WebDriver%3B%0Aimport%20org.openqa.selenium.firefox.FirefoxDriver%3B%0A%20%0Apublic%20class%20Gecko_Driver%20%7B%0A%20%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09WebDriver%20driver%20%3D%20new%20FirefoxDriver()%3B%0A%09%09driver.get(%22http%3A%2F%2Fwww.google.com%22)%3B%0A%09%09Thread.sleep(3000)%3B%0A%09%09driver.close()%3B%0A%09%7D%0A%20%0A%7D” message=”” highlight=”” provider=”manual”/]

但是相同的程式如果使用 Selenium 3執行的時候 , 會出現類似下列錯誤訊息

java.lang.IllegalStateException…

The path to the driver executable must be set by webdriver.gecko.driver system propeerty: ‘.

Geckodriver下載與設計

Selenium 3之後需要Geckodriver Webdriver 才可以正常啟動,

因此到這裡下載最新的 Geckodriver 版本

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

下載之後必須設定環境變數 Path, 有三個方式:

  1. 我的電腦 > 環境變數 > Path=d:\webdriver\GeckoDriver.exe
  2. System.setProperty(“webdriver.gecko.driver”,”d:\\\\webdriver\\GeckoDriver.exe”);
  3. 設定 Desired Capabilities

範例程式1: 使用  setProperty

 

[pastacode lang=”java” manual=”%0Aimport%20org.openqa.selenium.WebDriver%3B%0Aimport%20org.openqa.selenium.firefox.FirefoxDriver%3B%0Apublic%20class%20Gecko_Driver%20%7B%0A%20%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20InterruptedException%20%7B%0A%09%09%2F%2F%20Configure%20the%20Geckodriver.exe%20for%20selenium3%0A%09%09System.setProperty(%22webdriver.gecko.driver%22%2C%20%22D%3A%5C%5C%5C%5Cwebdrivers%5C%5Cgeckodriver.exe%22)%3B%0A%09%09%0A%09%09WebDriver%20driver%20%3D%20new%20FirefoxDriver()%3B%0A%09%09driver.get(%22http%3A%2F%2Fwww.google.com%22)%3B%0A%20%0A%09%09Thread.sleep(3000)%3B%0A%09%09driver.close()%3B%0A%09%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

範例程式2: 使用 Desired Capabilities

[pastacode lang=”java” manual=”%09import%20org.openqa.selenium.WebDriver%3B%0A%09import%20org.openqa.selenium.firefox.FirefoxDriver%3B%0A%09import%20org.openqa.selenium.remote.DesiredCapabilities%3B%0Apublic%20class%20Gecko_Driver%20%7B%0A%20%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20InterruptedException%20%7B%0A%09%09DesiredCapabilities%20capabilities%20%3D%20DesiredCapabilities.firefox()%3B%0A%09%09%0A%09%09%2F%2F%20set%20web%20driver%20to%20Marionette%20for%20Selenium3%0A%09%09capabilities.setCapability(%22marionette%22%2C%20true)%3B%20%0A%09%09WebDriver%20driver%20%3D%20new%20FirefoxDriver(capabilities)%3B%0A%09%09%0A%09%09driver.get(%22http%3A%2F%2Fwww.google.com%22)%3B%0A%20%09%09Thread.sleep(3000)%3B%0A%09%09driver.close()%3B%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

 

總結

  • 如果您目前使用Selenium 2 執行自動化測試,  沒有特別需求的情況, 繼續使用可以暫時不升級至Selenium3
  • 如果你因為Browser版本不相容被迫一定要在Selenium 3 才可以執行, 那麼原本的FireFox則需要額外下載geckodriver與設定才可以執行
  • 如何設定GeckoDriver? 有三種方式:
    • 設定環境變數Path
    • 設定GeckoDriver
    • 設定Capability Set

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *