2017-05-20 60 views
0

prof。博士學位。 Boni Garcia爲QA Automation WebDriverManager提供了一個很棒的工具。BrowserManager實現BrowserFactory

正如你可以在github上看到他做不手動下載最新版本的瀏覽器的一個很好的解決方案,並且不設定絕對路徑變量

System.setProperty(「webdriver.chrome.driver」,「/絕對/路徑/到/二進制/ chromedriver「);

我現在將向您展示如何在您的項目中使用特殊化設置並使用它。基本上它是Browser Factory的實現,並且我使用了TestNG,Maven和Selenium。

首先在你的pom.xml添加

<dependency> 
    <groupId>io.github.bonigarcia</groupId> 
    <artifactId>webdrivermanager</artifactId> 
    <version>1.6.2</version> 
</dependency> 

在項目中像這樣創建BrowserFactory類:

import io.github.bonigarcia.wdm.*; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.chrome.ChromeOptions; 
import org.openqa.selenium.edge.EdgeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.opera.OperaDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriverService; 
import org.openqa.selenium.safari.SafariDriver; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.concurrent.TimeUnit; 


public class BrowserFactory { 

    public static WebDriver GetBrowser(String browserName) { 
     browserName = browserName.toLowerCase(); 

     if(browserName.equals("chrome")) 
      return getChromeInstance(); 

     if(browserName.equals("edge")) 
      return getEdgeInstance(); 

     if(browserName.equals("ie")) 
      return getIEInstance(); 
     if(browserName.equals("phantomjs")) 
      return getPhantomJSInstance(); 

     if(browserName.equals("opera")) 
      return getOperaInstance(); 
     else 
      return getFFInstance(); 
    } 

    private static FirefoxDriver getFFInstance() { 

     FirefoxDriverManager.getInstance().setup(); 
     FirefoxDriver driver=new FirefoxDriver(); 
     return driver; 
    } 

    private static ChromeDriver getChromeInstance() { 

     ChromeDriverManager.getInstance().setup(); 


     Map<String, Object> prefs = new HashMap<String, Object>(); 
     prefs.put("profile.default_content_setting_values.notifications", 2); 
     prefs.put("credentials_enable_service", false); 
     prefs.put("password_manager_enabled", false); 

     ChromeOptions options = new ChromeOptions(); 
     options.setExperimentalOption("prefs", prefs); 

     ChromeDriver driver = new ChromeDriver(options); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.manage().window().maximize(); 

     return driver; 
    } 


    private static InternetExplorerDriver getIEInstance() { 

     InternetExplorerDriverManager.getInstance().setup(); 
     InternetExplorerDriver driver=new InternetExplorerDriver(); 
     return driver; 
    } 

    private static PhantomJSDriver getPhantomJSInstance() { 

     PhantomJsDriverManager.getInstance().setup(); 
     PhantomJSDriver driver=new PhantomJSDriver(); 
     return driver; 
    } 


    public static EdgeDriver getEdgeInstance() { 
     EdgeDriverManager.getInstance().setup(); 
     EdgeDriver driver=new EdgeDriver(); 
     return driver; 
    } 

    public static OperaDriver getOperaInstance() { 
     OperaDriverManager.getInstance().setup(); 
     OperaDriver driver=new OperaDriver(); 
     return driver; 
    } 
} 
在TestNG的

現在testing.xml添加瀏覽器參數,如:

<suite name="YTCommentator" > 

    <parameter name="browser" value="chrome"/> 

    <test name="YTComments"> 

     <parameter name="email" value=""/> 

     <parameter name="password" value=""/> 

     <parameter name="searchFor" value=""/> 

     <parameter name="comment" value=""/> 

     <parameter name="repeatTimes" value=""/> 

     <classes> 

      <class name="CommentsTest" /> 

     </classes> 

    </test> 

</suite> 

在您的測試中,使用此參數添加新的@BeforTest

@BeforeTest 
@Parameters({"browser"}) 
public void setup(String browser){ 

    driver =BrowserFactory.GetBrowser(browser); 

} 

,這是所有:)

問題是,只有Chrome的現在與WebDriverManager。像FF其他驅動程序有錯誤:((

1495307903858 geckodriver信息上偵聽127.0.0.1:36758

1495307904514 geckodriver ::木偶INFO啓動瀏覽器\ \ C:\ Program Files文件(x86)的\ Mozilla Firefox的\與ARGS [ 「-marionette」]

1495307904773 addons.manager錯誤啓動失敗firefox.exe:[異常... 「組件返回故障代碼:80070057(NS_ERROR_ILLEGAL_VALUE)nsIFile.create]」 nsresult:「80070057(NS_ERROR_ILLEGAL_VALUE )「location:」JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getDir :: line 70「data:no] Stack trace:FileUtils_getDir()@resource://gre/modules/FileUtils.jsm :70 < FileUtils_getFile()@resource://gre/modules/FileUtils.jsm:42 < AddonManagerInternal.validateBlocklist()@resource://gre/modules/AddonManager.jsm:700 < AddonManagerInternal.startup()@resource:// gre /modules/AddonManager.jsm:870 < this.AddonManagerPrivate.startup()@resource://gre/modules/AddonManager.jsm:3033 < amManager.prototype.observe()@resource://gre/components/addonManager.js :65

JavaScript錯誤:資源://gre/modules/AddonManager.jsm,線1677:NS_ERROR_NOT_INITIALIZED:AddonManager未初始化

1495307905408木偶INFO偵聽端口55052

JavaScript錯誤:資源://gre/modules/AddonManager.jsm,行2585:NS_ERROR_NOT_INITIALIZED:AddonManager未初始化

org.openqa.selenium.SessionNotCreatedException:無法創建新的遠程會話。需要的能力= Capabilities [{moz:firefoxOptions = {binary = Optional.empty,args = [],legacy = null,logLevel = null,prefs = {},profile = null}}],required capabilities = Capabilities [{moz: firefoxOptions = {二進制=可選。empty,args = [],legacy = null,logLevel = null,prefs = {},profile = null}}]

構建信息:version:'3.3.1',revision:'5234b325d5',time:' 2017-03-10 09:10:29 +0000'

系統信息:host:'HP',ip:'192.168.10.101',os.name:'Windows 10',os.arch:'amd64' ,os.version:'10 0.0' ,java.version: '1.8.0_101'

驅動程序信息:driver.version:FirefoxDriver

在org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java :126)

請更新WebDriverManager,我們將有很好的工具來幫助我們自動化和CI!

+1

我會建議編輯你的問題,減少羅嗦,更重要的一點。這個問題很難理解。 – lax1089

回答

0

由於Firefox Gecko驅動程序0.16.1,請嘗試將所有Selenium依賴項更新爲3.4.0。它不在SeleniumHQ上,但它在mvn存儲庫上可用。它適用於我...

更新:對於Opera,它看起來像WebDriverManager使用的是舊版本的驅動程序。剛剛添加淘寶作爲驅動程序回購,它很好地工作: OperaDriverManager.getInstance()。useTaobaoMirror()。setup();

+0

非常好,它解決了FF的問題!但它不適用於其他車手。我認爲webdriver manager需要更新。 –

+0

這裏是爲IE和Selenium 3.4.0 私人靜態InternetExplorerDriver getIEInstance(){ InternetExplorerDriverManager.getInstance()。version(「3.4」)。setup(); DesiredCapabilities功能= DesiredCapabilities.internetExplorer(); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true); capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING,true); InternetExplorerDriver driver = new InternetExplorerDriver(capabilities); 返回驅動程序; } –