Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining session ID from WebDriverJS

I'm trying to run WebDriverJS on the browser, but the documentation is somewhat vague on how to get it controlling the host browser. Here, it says:

Launching a browser to run a WebDriver test against another browser is a tad redundant (compared to simply using node). Instead, using WebDriverJS in the browser is intended for automating the browser actually running the script. This can be accomplished as long as the > URL for the server and session ID for the browser are known. While these values may be passed to the builder directly, they may also be defined using the wdurl and wdsid "environment variables", which are parsed from the loading page's URL query data:

    <!-- Assuming HTML URL is /test.html?wdurl=http://localhost:4444/wd/hub&wdsid=foo1234 -->
    <!DOCTYPE html>
    <script src="webdriver.js"></script>
    <input id="input" type="text"/>
    <script>
      // Attaches to the server and session controlling this browser.
      var driver = new webdriver.Builder().build();

      var input = driver.findElement(webdriver.By.tagName('input'));
      input.sendKeys('foo bar baz').then(function() {
        assertEquals('foo bar baz',
            document.getElementById('input').value);
      });
    </script>

I want to open up my test page from Node.js, and then run the commands included in the client-side script. However, I don't know how I would be able to extract the session ID (wdsid query parameter) when I build the session. Does anyone have any idea?

like image 491
Clarence Leung Avatar asked Jun 25 '12 20:06

Clarence Leung


1 Answers

Finally figured it out after a lot of experimentation and reading through the WebDriverJS source code.

var webdriver = require('./assets/webdriver');

var driver = new webdriver.Builder().
    usingServer('http://localhost:4444/wd/hub').
    withCapabilities({
        'browserName': 'chrome',
        'version': '',
        'platform': 'ANY',
        'javascriptEnabled': true
    }).
    build();

var testUrl = 'http://localhost:3000/test',
    hubUrl = 'http://localhost:4444/wd/hub',
    sessionId;

driver.session_.then(function(sessionData) {
    sessionId = sessionData.id;
    driver.get(testUrl + '?wdurl=' + hubUrl + '&wdsid=' + sessionId);
});

driver.session_ returns a Promise object which will contain the session data and other information upon instantiation. Using .then(callback(sessionData)) will allow you to manipulate the data as you wish.

like image 78
Clarence Leung Avatar answered Oct 13 '22 19:10

Clarence Leung