java - selenium firefox 52.0.2 和 intellij idea 崩溃

标签 java selenium firefox intellij-idea selenium-webdriver

我正在使用 IntelliJ 和 selenium 来自动化一个非常简单的测试用例,但它一开始就崩溃了。

此外,我尝试将 Firefox 浏览器更改为 46,但它仍然打开 52.0.2 并崩溃。

问题:
- 如何让它发挥作用?
- 如何将 Mac 上的默认浏览器 Firefox 浏览器更改为 46 而不是 52.0.2?

代码片段:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

public class tt {
    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {

        baseUrl = "https://github.com";
        loadFirefoxDriver();
        //loadChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    private void loadFirefoxDriver() {
        driver = new FirefoxDriver();
    }

    private void loadChromeDriver() throws MalformedURLException {
        // To remove message "You are using an unsupported command-line flag: --ignore-certificate-errors.
        // Stability and security will suffer."
        // Add an argument 'test-type'
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("test-type");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        driver = new RemoteWebDriver(new URL("http://localhost:9515"), capabilities);
    }

    @Test
    public void testFirstSelenium() throws Exception {
        driver.get(baseUrl + "/codesolid");
        driver.findElement(By.linkText("tutorials")).click();
        assertEquals("Welcome to the CodeSolid Tutorials", driver.findElement(By.cssSelector("html body.logged_out.env-production.windows.vis-public div.wrapper div.site div.container div.repository-with-sidebar.repo-container.new-discussion-timeline.js-new-discussion-timeline.with-full-navigation div#js-repo-pjax-container.repository-content.context-loader-container div#readme.clearfix.announce.instapaper_body.md article.markdown-body.entry-content h1")).getText());
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

Gradle:

/*
    build.gradle file for building and running Selenium tests
    Example uses:
    gradle clean test       - Rebuild and run tests
    gradle test             - Run tests only
    gradle cleanTest test   - Force tests to be run even if up to date
    gradle viewResults      - Displays the report of test results (Windows only)
*/

// Support for building and testing
apply plugin: 'java'

// (Optional) configure name and version for jar
jar {
    version  '1.0'
    baseName 'SeleniumStarter'
    extension '.jar'
}

// Cf. Maven properties
ext.junitVersion = '4.11'
ext.seleniumVersion = '2.41.0'


// Cf. Maven <dependencies>
dependencies {
    compile group: 'junit', name: 'junit', version: junitVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-firefox-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-api', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:seleniumVersion
}

// A custom task to show report on tests that have run
task viewResults(dependsOn: ['test'] , type:Exec) {
    workingDir './build/reports/tests'
    commandLine 'cmd', '/c', 'start index.html'
}

// Resolve Maven dependencies as Maven does
repositories {
    mavenCentral()
    mavenLocal()
}

错误堆栈跟踪:

1490969272079   geckodriver INFO    Listening on 127.0.0.1:14563
1490969272652   mozprofile::profile INFO    Using profile path /var/folders/hd/4pn57v5x75b81hdv1nrg7tlw0000gq/T/rust_mozprofile.YiFu7gXew5Bk
1490969272655   geckodriver::marionette INFO    Starting browser /Applications/Firefox.app/Contents/MacOS/firefox-bin with args []
1490969272668   geckodriver::marionette INFO    Connecting to Marionette on localhost:59730
1490969273915   Marionette  INFO    Listening on port 59730
Mar 31, 2017 7:37:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2017-03-31 19:37:56.008 plugin-container[3347:248694] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x963b, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
2017-03-31 19:37:56.013 plugin-container[3347:248694] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x9a03, name = 'com.apple.CFPasteboardClient'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1490969277987   Marionette  INFO    New connections will no longer be accepted
[Child 3347] ###!!! ABORT: Aborting on channel error.: file /builds/slave/m-rel-m64-00000000000000000000/build/src/ipc/glue/MessageChannel.cpp, line 2143
[Child 3347] ###!!! ABORT: Aborting on channel error.: file /builds/slave/m-rel-m64-00000000000000000000/build/src/ipc/glue/MessageChannel.cpp, line 2143

org.junit.ComparisonFailure: 
Expected :Selenium WebDriver — Selenium Documentation
Actual   :Selenium - Web Browser Automation
 <Click to see difference>


    at org.junit.Assert.assertEquals(Assert.java:115)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at test_p.test.test(test.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


Process finished with exit code 255

最佳答案

对我来说它可以工作(也来自 IntelliJ IDEA)

  • Selenium 3.0.1,
  • Windows 10 上的 Firefox 52.0.2 64 位,
  • Gecko 驱动程序 0.14.0 64 位。

当然,系统属性webdriver.gecko.driver也需要正确设置。

顺便说一下,第二个断言应该改为

assertEquals(
  "Source Code for the CodeSolid Tutorials",
  driver.findElement(By.cssSelector("article h1")).getText()
);

从而使测试通过。

关于java - selenium firefox 52.0.2 和 intellij idea 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101712/

相关文章:

javascript - JavaScript 不应该忽略空格吗? Firefox 中的节点怪癖

html - 带有水平子菜单的水平菜单栏在 IE 中不起作用

java - 在 Windows 7 上信任 Java 7 的自签名证书

java - 在不安装 Java Web 应用程序的情况下使用 xuggle

Java DecimalFormatter - Locale DE 和 Locale US 之间的不同行为

java - 如何使用 selenium 导航到同级 <p> 元素

java - Java/Selenium/Maven 中的相对路径

javascript - jQuery .hover() 仅适用于 FireFox

java - toString()表示输出

python - 与不可见元素交互并访问第二个匹配元素