2017-03-29 26 views
0

我使用IntelliJ和硒自动化一个非常简单的测试用例,但它在开始本身崩溃。硒火狐52.0.2随着intellij想法崩溃

此外,我试图更改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; 
     } 
    } 
} 

摇篮

/* 
    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 
+0

你是什么geckodriver版本? – kushal

+0

我没有壁虎驱动。我不知道如何安装它? – jason

+0

你不必只安装转到https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-win32.zip并保持文件夹 – kushal

回答

1

我在Linux Mint上有类似的东西。

正如所建议的,您需要更新Gecko Driver。最后一个版本是v0.15.0
它之后,你必须设定它的位置为物业,打造FF驱动程序之前:

System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe"); 

此外,您必须更新selenium-web-driverlatest version 3.3.1

ext.seleniumVersion =“3.3.1 '

如果您不想使用FF的最新版本,则需要手动将其降级为浏览器。您可以在这里找到更多信息How to downgrade Firefox version?

创建驱动程序实例,这是好事,最大化浏览器后BTW:。

driverInstance.manage()窗口()最大();

不仅隐含地等待。

而且,当你写断言你必须写:

的assertEquals(actualResult,expectedResult);而不是

的assertEquals( “欢迎来到CodeSolid教程”,driver.findElement(...));

我检查了您的代码,发现您的测试总是失败。您使用了不正确的CSS选择器。

我更新了那么一点点:

public class GitHubTest { 

    private WebDriver driver; 

    @BeforeMethod 
    public void setUp() { 
     driver = DriverManager.getInstance(); 
     driver.get("https://github.com/codesolid"); 
    } 

    @Test 
    public void testFirst() throws Exception { 
     driver.findElement(By.linkText("tutorials")).click(); 

     new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("span.col-11")))); 

     // validate if correct page is open by title & url  
     assertEquals(driver.findElement(By.xpath("//title")).getText(), "GitHub - CodeSolid/tutorials: Tutorials"); 
     assertEquals(driver.getCurrentUrl(), "https://github.com/CodeSolid/tutorials"); 
    } 

    @AfterMethod 
    public void tearDown() throws Exception { 
     DriverManager.closeQuietly(); 
    } 
} 

public class DriverManager { 
    private static volatile WebDriver driverInstance; 

    private DriverManager() { 
    } 

    public static synchronized WebDriver getInstance() { 
     if (driverInstance == null) { 
      switch (readBrowserType()) { 
       case Firefox: { 
        driverInstance = new FirefoxDriver(); 
        driverInstance.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
        driverInstance.manage().window().maximize(); 
        return driverInstance; 
       } 

现在测试通过:

enter image description here

其他资源:

+0

太棒了!这工作:)我不得不改变我的gradle版本和路径到最新版本:)谢谢吨! – jason

2

对我来说,它的工作原理(也来自的IntelliJ IDEA)与

  • 硒3.0.1,
  • 火狐52.0.2在Windows 10的64位,
  • Gecko驱动程序0.14.0 64位。

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

顺便说一句,第二断言应改为

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

,以使测试通过。

+0

太棒了!这工作:)我不得不改变我的gradle版本和路径到最新版本:)谢谢吨!感谢您的CSS选择器代码:)它现在工作:) – jason

+0

然后请接受我的答案,是吗?谢谢。 – kriegaex

+0

我会这样做,但nazar_art给出了一步一步的解释,它帮助我解决了这个问题。抱歉。我已经upvoted你的答案:) – jason