使用Node.js在服务器上运行Selenium测试

时间:2016-09-29 19:47:57

标签: javascript node.js selenium ubuntu firefox

我正在尝试设置一个Ubuntu服务器,以便它可以使用javascript运行Selenium测试。

我在Digital ocean上开了一个新的Droplet,并运行了以下命令:

apt-get update
apt-get install nodejs
ln -s /usr/bin/nodejs /usr/bin/node
apt-get install default-jre
apt-get install npm
curl -O http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar
java -jar selenium-server-standalone-2.53.1.jar

然后,在一个新的终端:

npm init
npm install webdriverio
touch main.js

在main.js中,我从this page添加以下代码:

var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'firefox'
    }
};

webdriverio
    .remote(options)
    .init()
    .url('http://www.google.com')
    .getTitle().then(function(title) {
        console.log('Title was: ' + title);
    })
    .end();

然后我运行命令:

node main

无声地失败了。回顾第一个终端,我看到一条巨大的错误信息。

19:46:13.910 INFO - Executing: [new session: Capabilities [{rotatable=true, locationContextEnabled=true, loggingPrefs=org.openqa.selenium.logging.LoggingPreferences@6aa523f8, browserName=firefox, javascriptEnabled=true, handlesAlerts=true, requestOrigins={name=webdriverio, version=4.2.16, url=http://webdriver.io}}]])
19:46:13.912 INFO - Creating a new session for Capabilities [{rotatable=true, locationContextEnabled=true, loggingPrefs=org.openqa.selenium.logging.LoggingPreferences@6aa523f8, browserName=firefox, javascriptEnabled=true, handlesAlerts=true, requestOrigins={name=webdriverio, version=4.2.16, url=http://webdriver.io}}]
19:46:13.914 WARN - Exception thrown
java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'ubuntu-sel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-38-generic', java.version: '1.8.0_91'
Driver info: driver.version: unknown
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:183)
    at org.openqa.selenium.remote.server.DefaultSession.<init>(DefaultSession.java:119)
    at org.openqa.selenium.remote.server.DefaultSession.createSession(DefaultSession.java:95)
    at org.openqa.selenium.remote.server.DefaultDriverSessions.newSession(DefaultDriverSessions.java:124)
    at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:59)
    at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:1)
    at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:111)
    at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:79)
    at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:204)
    at org.openqa.selenium.remote.server.DriverServlet.doPost(DriverServlet.java:166)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:132)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.openqa.jetty.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
    at org.openqa.jetty.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:680)
    at org.openqa.jetty.jetty.servlet.ServletHandler.handle(ServletHandler.java:571)
    at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1526)
    at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1479)
    at org.openqa.jetty.http.HttpServer.service(HttpServer.java:920)
    at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
    at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
    at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
    at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:243)
    at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:358)
    at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:537)
Caused by: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'ubuntu-sel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-38-generic', java.version: '1.8.0_91'
Driver info: driver.version: unknown
    at org.openqa.selenium.remote.server.FirefoxDriverProvider.callConstructor(FirefoxDriverProvider.java:101)
    at org.openqa.selenium.remote.server.FirefoxDriverProvider.newInstance(FirefoxDriverProvider.java:68)
    at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:60)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:222)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.server.FirefoxDriverProvider.callConstructor(FirefoxDriverProvider.java:91)
    ... 9 more
Caused by: org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'ubuntu-sel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-38-generic', java.version: '1.8.0_91'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:74)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
    at org.openqa.selenium.firefox.FirefoxDriver.getBinary(FirefoxDriver.java:203)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:128)
    ... 14 more
19:46:13.916 WARN - Exception: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'ubuntu-sel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-38-generic', java.version: '1.8.0_91'
Driver info: driver.version: FirefoxDriver

看起来错误可能是我没有安装Firefox,但我不确定这在这种环境中意味着什么。由于它只是服务器上的命令行,我不能只是去firefox网站下载并安装它。

我需要做些什么不同的事情?我真的不在乎我是否使用firefox或chrome,或者可能是其他更简单的浏览器。只是想让这个基本脚本运行!

更新

我运行了这些命令:

sudo add-apt-repository ppa:ubuntu-mozilla-security/ppa
apt-get update

然后停止并启动java selenium命令。现在当我运行node main时,我没有在selenium终端中看到任何错误,但我也没有看到标题被记录。

0 个答案:

没有答案