2

I recently began working with Selenium and to make life easier to start I was using node to run my scripts so that I could visually monitor the tests. My challenge now is to convert it so that it can be run as a headless test. Unfortunately, most of the resources that I have come across only address using phantomjs and ghostdriver with Java or Python. My boss wants me to run the test through phantomjs without Java or Python. Eventually these tests will be run remotely through a Linux VM on a server without a GUI. Currently I am testing using Mac OS X 10.8 and still have many bridges to cross in order to get to my goal.

My most important question firstly, is it possible to run a script from phantomjs through a port without the use of Java or Python? I have spent hours poring through as many resources as I could come across and I've come up with no solution.

If so, how can I properly initialize the test to run headless? Here is how I scripted the start of my functioning test. I want to properly switch the capabilities from firefox to phantomjs and be able to run it headless using the appropriate port. The rest of the test navigates to a specific site, logs in through a widget, then does further navigation to the area which I will build further tests on which to manipulate after I get this working.

var webdriver = require('selenium-webdriver'),
SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;

var server = new SeleniumServer("Path/selenium-server-standalone-2.39.0.jar", {
port: 8910
});

server.start();

var driver = new webdriver.Builder().
usingServer(server.address()).
withCapabilities(webdriver.Capabilities.firefox()).
build();

The test works perfectly, but I am new to this so there might be something foolish that I am overlooking. Please let me know what adjustments to make so that it will run headless through phantom. When I attempt to use node to run the script after switching capabilities to phantomjs it produces

"/Selenium/node_modules/selenium-webdriver/phantomjs.js:22
LogLevel = webdriver.logging.LevelName,
                            ^
TypeError: Cannot read property 'LevelName' of undefined
 at Object.<anonymous> (/Selenium/node_modules/selenium-webdriver/phantomjs.js:22:33)

That's a read only file that I can't adjust, any attempts that I made to define "LogLevel" or "LevelName" to the appropriate corresponding value (DEBUG, etc.) were fruitless.

And if I run it through phantomjs itself I get -

"Error: Cannot find module 'path'

 phantomjs://bootstrap.js:289
 phantomjs://bootstrap.js:254 in require"

(It also lists module 'http') -- (and various undefined function errors)

I feel that with that instance I didn't properly organize where the files for Selenium, phantomjs, and ghostdriver should go in order to play nice. I also removed the server setup portion and instead ran this first, then the script separately.

phantomjs --webdriver=8910 

But it yielded the same result. All of my research to fix these issues turned up instructions for Java and Python but not Javascript by itself. Rather than chase through many rabbit holes I figured it wise to consult better minds.

If you know better than I do and that it is fruitless to attempt this without Java or Python, please let me know. If you know where the issue lies within my script and could propose a fix please let me know. I hope that I have properly described the nature of my issue and if you need more information I will do my best to provide it to you.

This is my second week working with Javascript so if you believe I am making a noob error you very well may be correct. Please, keep in mind that the script works through node with selenium webdriver.

Many thanks for your time!!! ~Isaac

1 Answers1

2

This was a bit tricky but here is the solution I've pieced together:

var webdriver = require('selenium-webdriver'),
    SeleniumServer = require('selenium-webdriver/remote').SeleniumServer,
    server = new SeleniumServer('/path/to/selenium/selenium-server-standalone-2.41.0.jar', {
        port: 4444

    }),
    capabilities = webdriver.Capabilities.phantomjs();
    capabilities.set('phantomjs.binary.path', 'path/to/phantom/bin/phantomjs');

var promise = server.start().then(function() {
    var client = new webdriver.Builder().
    usingServer(server.address()).withCapabilities(
       capabilities
    ).build();
    return {
        'client': client,
        'server': server
    };
}, function(err) {
    console.log('error starting server', err);
});

You can then use the promise with selenium's mocha-compatible test framework to hold the test till the server has started.

I found the documentation really helpful once i figured out the navigation is on the far right of the page. Here's the URL: http://selenium.googlecode.com/git/docs/api/javascript/module_selenium-webdriver.html Then you'll be stuck where I am. Getting selenium-webdriver to quiet down.

pyrzak
  • 63
  • 5