BrowserFactory的

时间:2017-05-20 19:45:11

标签: automation qa browser-automation

教授。博士学位。 Boni Garcia为QA Automation WebDriverManager提供了一个很棒的工具。

正如你在github上看到的那样,他为不手动下载最新版本的浏览器以及不设置绝对路径变量提供了一个很好的解决方案

System.setProperty(“webdriver.chrome.driver”,“/ absolute / path / to / binary / 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 INFO收听127.0.0.1:36758

1495307904514 geckodriver :: marionette INFO启动浏览器\?\ C:\ Program Files(x86)\ Mozilla Firefox \ firefox.exe with args [“-marionette”]

1495307904773 addons.manager错误启动失败:[Exception ...“组件返回失败代码:0x80070057(NS_ERROR_ILLEGAL_VALUE)[nsIFile.create]”nsresult:“0x80070057(NS_ERROR_ILLEGAL_VALUE)”location:“JS frame :: resource:/ /gre/modules/FileUtils.jsm :: FileUtils_getDir :: line 70“data:no]堆栈跟踪:FileUtils_getDir()@ resources://gre/modules/FileUtils.jsm:70&lt; FileUtils_getFile()@ resources://gre/modules/FileUtils.jsm:42&lt; AddonManagerInternal.validateBlocklist()@ resources://gre/modules/AddonManager.jsm:700&lt; AddonManagerInternal.startup()@ resources://gre/modules/AddonManager.jsm:870&lt; this.AddonManagerPrivate.startup()@ resource://gre/modules/AddonManager.jsm:3033&lt; amManager.prototype.observe()@resource://gre/components/addonManager.js:65

JavaScript错误:资源://gre/modules/AddonManager.jsm,第1677行:NS_ERROR_NOT_INITIALIZED:AddonManager未初始化

1495307905408 Marionette INFO侦听端口55052

JavaScript错误:资源://gre/modules/AddonManager.jsm,第2585行:NS_ERROR_NOT_INITIALIZED:AddonManager未初始化

org.openqa.selenium.SessionNotCreatedException:无法创建新的远程会话。 desired capabilities = Capabilities [{moz:firefoxOptions = {binary = Optional.empty,args = [],legacy = null,logLevel = null,prefs = {},profile = null}}],required capabilities = Capabilities [{moz: firefoxOptions = {binary = Optional.empty,args = [],legacy = null,logLevel = null,prefs = {},profile = null}}]

构建信息:版本:'3.3.1',修订版:'5234b325d5',时间:'2017-03-10 09:10:29 +0000'

系统信息:主机:'HP',ip:'192.168.10.101',os.name:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:' 1.8.0_101'

驱动程序信息:driver.version:FirefoxDriver

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

请更新WebDriverManager,我们将拥有可以帮助我们完成自动化和CI的强大工具!

1 个答案:

答案 0 :(得分:0)

尝试将所有Selenium依赖项更新为3.4.0,因为Firefox Gecko驱动程序为0.16.1。它不在SeleniumHQ上,但它可以在mvn存储库上使用。它对我有用......

更新:对于Opera,看起来WebDriverManager使用旧版本的驱动程序。刚刚添加淘宝作为驱动程序回购,它很好地工作: 。OperaDriverManager.getInstance()useTaobaoMirror()设置();