Defining Session ID from WebDriverJS

I am trying to run WebDriverJS in a browser, but the documentation is somewhat vague on how to get it to control the host browser. It says here:

Running a browser to run the WebDriver test against another browser is a bit redundant (compared to just using node). Instead, using WebDriverJS in the browser is intended to automate the browser actually running the script. This can be done if the URL for the server and the session identifier for the browser are known. Although these values ​​can be passed directly to the builder, they can also be defined using wdurl and wdsid "environment variables", which are analyzed from the request data of the URL of the download page:

<!-- 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 my test page from Node.js and then run the commands included on the client side script. However, I do not know how I can extract the session identifier (wdsid request parameter) when I create the session. Somebody knows?

+6
source share
1 answer

Finally, it turned out that after a lot of experimentation and reading through 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 that will contain session data and other information after the instance is created. Using .then (callback (sessionData)) will allow you to manipulate the data as you wish.

+7
source

Source: https://habr.com/ru/post/918989/


All Articles