4

我对 Selenium 很陌生。下面给出的是我的第一个测试脚本。

问题:是否需要 selenium 服务器来测试本地网站(与正在运行的测试脚本托管在同一台机器上)。

还尝试从 Internet Explorer 执行相同的脚本,仍然得到相同的结果。它只是打开浏览器并关闭(因为 finally 阻止)它。

import org.openqa.selenium.chrome.ChromeDriver;
public class TestScript {
    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) {
        ChromeDriver driver = null;
        try 
        { 
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\user1\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
            driver = new ChromeDriver();
            System.out.println("Opening the Browser");
            driver.get("http://localhsot:5080/myWebSite/8450191#");
            System.out.println("Open the Browser");
            System.out.println("");
            System.out.println("Title" +driver.getTitle());

        }
        catch (Exception ie)
        {
            ie.printStackTrace();
        }
        finally
        {
            System.out.println("Quitting the Browser");
            driver.close();
            driver.quit();
        }
    }

}

异常:以下是我从 Eclipse 执行时遇到的异常:

[2820:6204:36503609:ERROR:gpu_info_collector_win.cc(93)] Can't retrieve a valid WinSAT assessment.
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.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
Driver info: driver.version: ChromeDriver
    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:112)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:116)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:162)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:108)
    at com.seic.scripts.TestScript.main(TestScript.java:16)
Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
    ... 6 more
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:37571/status] to be available after 20009 ms
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104)
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163)
    ... 8 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)
    ... 9 more
Caused by: java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228)
    at java.util.concurrent.FutureTask.get(FutureTask.java:91)
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
    ... 10 more
Quitting the Browser
Exception in thread "Main Thread" java.lang.NullPointerException
    at com.scripts.TestScript.main(TestScript.java:31)

铬版本:21.0.1171.0

操作系统:Windows 7 64 位。

Selenium Web 驱动程序版本:2.39.0

4

2 回答 2

3

您对需要将 Selenium 指向的内容感到有些困惑。

这里:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\user1\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");

此设置webdriver.chrome.driver是 Selenium 将读取以找出ChromeDriver所在位置的设置,而不是实际的Chrome 安装。

https://code.google.com/p/selenium/wiki/ChromeDriver

您必须下载 ChromeDriver,并且它所在的位置应该是您为该webdriver.chrome.driver属性传递的内容。

我想这与您尝试使用 IE 时的原因相同,它还需要一个我猜您没有的驱动程序:

https://code.google.com/p/selenium/wiki/InternetExplorerDriver

至于您是否需要“服务器”-> 进行测试,不,您不是在远程运行它们。一旦您决定远程运行它们(即使用RemoteWebDriver代替ChromeDriver),那么您将需要 Selenium 服务器。

于 2014-02-10T14:51:46.173 回答
0

我建议不要使用 System.setProperty() 将驱动程序放在同一个文件夹中并指向系统变量路径中的该文件夹。然后你根本不需要在代码中指向它,更新你的驱动程序不需要重写代码。

于 2014-02-10T17:06:06.363 回答