Firefox浏览器在打开后关闭一秒钟,忽略方案

时间:2017-12-28 16:08:52

标签: java selenium firefox cucumber geckodriver

我使用Firefox版本54.0.1(64位),Geckodriver v0.17.0-win64和Selenium 3.4.0但是当我运行我的场景时,Firefox打开并在一秒钟后关闭。所以,我的方案被忽略了。我使用了不同版本的Firefox,Geckodriver和Selenium。但没有帮助 这是我的代码:

private static WebDriver createConfiguredDriver() {

        final String driverName = TestData.getBrowser();   //BrowserType.FIREFOX
        final boolean remote = TestData.isRemoteMode();    //"webdriver.isremote";
        DesiredCapabilities desiredCapabilites = new DesiredCapabilities(driverName, "", Platform.ANY);
        desiredCapabilites.setCapability("marionette", false);
        if (remote) {
            final String host = TestData.getBrowserHost();
            final int port = TestData.getBrowserPort();
            logger.info("Remote browser host: " + host);
            logger.info("Remote browser port: " + port);
            try {
                URL remoteAddress = new URL("http", host, port, "/wd/hub");
                return new RemoteWebDriver(remoteAddress, desiredCapabilites);
            } catch (MalformedURLException mue) {
                throw new IllegalArgumentException("Failed to create remote URL using host: " + host, mue);
            }
        }
        else if (BrowserType.FIREFOX.equalsIgnoreCase(driverName)) {
            System.setProperty("webdriver.gecko.driver",
                    "src\\main\\resources\\geckodriver\\geckodriver.exe");
            logger.info("Using local FireFox WebDriver.");
//            desiredCapabilites.setCapability("firefox_binary", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
            return new FirefoxDriver(desiredCapabilites);

这是我的pom.xml:

<dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium.fluent</groupId>
            <artifactId>fluent-selenium</artifactId>
            <version>1.14.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm</artifactId>
            <version>1.0.11</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.1.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>gherkin</artifactId>
            <version>2.12.2</version>
            <scope>provided</scope>
        </dependency>
         </dependencies>
         <profile>
            <id>firefox</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <webdriver.driver>firefox</webdriver.driver>
            </properties>
        </profile>
         <profile>
            <id>remote</id>
            <properties>
                <webdriver.isremote>true</webdriver.isremote>
                <webdriver.host>vwbnode01</webdriver.host>
                <webdriver.port>4444</webdriver.port>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <skip>false</skip>
                    <forkCount>1</forkCount>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.15</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/Test*.java</include>
                        <include>**/*Steps.java</include>
                        <include>**/*TestRunner.java</include>
                    </includes>
                    <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.2</version>
            </plugin>
        </plugins>
    </build>

我的控制台:

  

rg.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器的无效地址或浏览器启动失败。   构建信息:版本:&#39; 3.4.0&#39;,修订版:&#39;未知&#39;,时间:&#39;未知&#39;   系统信息:主机:&#39; WS00MU016&#39;,ip:&#39;&#39;,os.name:&#39; Windows 10&#39;,os.arch:&#39; amd64&#39 ;,os.version:&#39; 10.0&#39;,java.version:&#39; 9.0.1&#39;   驱动程序信息:driver.version:FirefoxDriver

     

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)       在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)       在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)       在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:137)       在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:191)       在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:108)       在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:137)       在com.dods.ui.mpp.tests.runner.CucumberTestRunner.createConfiguredDriver(CucumberTestRunner.java:60)       在com.dods.ui.mpp.tests.runner.CucumberTestRunner.init(CucumberTestRunner.java:84)       at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.base / java.lang.reflect.Method.invoke(Method.java:564)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)       在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在cucumber.api.junit.Cucumber.run(Cucumber.java:100)       在org.junit.runner.JUnitCore.run(JUnitCore.java:137)       在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)       在com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)       在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)   引起:java.lang.NullPointerException       在org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:192)       在org.openqa.selenium.firefox.XpiDriverService.start(XpiDriverService.java:94)       在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)       ......还有24个

2 个答案:

答案 0 :(得分:0)

您可以在下面找到推荐的版本。我看到你的Selenium和Gecko驱动程序已经升级,但你应该检查属性“marionette”是否设置为true。

GeckoDriver v0.19.0:Firefox 55.0(及更高版本)&amp;硒3.5(及更高)

GeckoDriver v0.18.0:Firefox 53.0(及更高版本)

GeckoDriver v0.16.0:Selenium 3.4(及更高版本)

答案 1 :(得分:0)

通过匹配解决了这个问题: GeckoDriver v0.19.0:Firefox 57.0.4(64位)&amp;硒3.4.0 属性“牵线木偶”应设为真