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 (pared to simply using node). Instead, using WebDriverJS in the browser is intended for automating the browser actually running the script. This can be acplished 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 mands 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?
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 (pared to simply using node). Instead, using WebDriverJS in the browser is intended for automating the browser actually running the script. This can be acplished 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 mands 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?
Share Improve this question asked Jun 25, 2012 at 20:22 Clarence LeungClarence Leung 2,56621 silver badges25 bronze badges2 Answers
Reset to default 7Finally 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.
My Selenium version: 4.1.0
Get sesion_id with await
const get_session_id = async (SeleniumDriver) =>
{
const res1 = await SeleniumDriver.getSession();
return res1.getId();
}
call in other function:
await get_session_id(SeleniumDriver);