Selenium ChromeDriver在Linux上无法启动

时间:2013-06-13 14:51:53

标签: selenium-webdriver selenium-chromedriver

我试图在junit中运行我的测试,但是我遇到了以下错误:

>org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
System info: os.name: 'Linux', os.arch: 'i386', os.version: '3.2.0-45-generic-pae', java.version: '1.7.0_21'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
    at br.com.xact.xmanager.xdesk.administracao.LoginPageChrome.setUp(LoginPageChrome.java:23)
[...]

我的代码:

package br.com.xact.xmanager.xdesk.administracao;

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.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

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

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");
        driver = new ChromeDriver();
        driver.get("http://m01fab0054:8080/xmanager/hel.cadGruposSituacaoChamados.x");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testConfiguraOLogin() throws Exception {
        driver.findElement(By.id("dsLogin")).clear();
        driver.findElement(By.id("dsLogin")).sendKeys("suporte");
        driver.findElement(By.id("dsSenha")).clear();
        driver.findElement(By.id("dsSenha")).sendKeys("senha");
        driver.findElement(By.id("loginButton")).click();
    }

    @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;
    }
}
}

1 个答案:

答案 0 :(得分:1)

  1. 从此处下载ChromeDriver:https://code.google.com/p/chromedriver/downloads/list
  2. 您需要在属性中指定文件:

    System.setProperty("webdriver.chrome.driver", "Path to driver you've downloaded in step 1");