我正在使用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;
}
}
}
摇篮 :
/*
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 :(得分:2)
对我而言,它的作用(也来自IntelliJ IDEA)
系统属性ProcessRequest
当然也需要正确设置。
顺便说一句,第二个断言应改为
IHttpHandler
以便使测试通过。
答案 1 :(得分:1)
我在Linux Mint上有类似的东西。
根据建议,您需要更新Gecko Driver。最后一个版本是v0.15.0
在它之后,你必须在创建FF驱动程序之前将它的位置设置为属性:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
此外,您必须将selenium-web-driver
更新为latest 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;
}
现在通过测试:
其他资源:
答案 2 :(得分:0)
根据https://github.com/mozilla/geckodriver/issues/548&amp; https://github.com/mozilla/geckodriver/issues/526此问题仍然存在;)
因此我建议您将Gecko驱动程序降级为v0.14
或v0.13
,如果这些gecko版本不能与selenium 3.3.1
一起使用,那么您也可以尝试降级硒版本到selenium 3.0
。
干杯!!