ClausI
ClausI

Reputation: 13

Selenium javascript webdriver has access to javascript global variables?

I am trying to access global variables using webdriver with javascript.

my code:

this.Then(/^I read global var$/, function (selectedElement) {
    readGlobalVar(window.location.href);
});

function readGlobalVar(varName){
return varName;
}

the error: ReferenceError: window is not defined

Upvotes: 1

Views: 1287

Answers (2)

Quentin
Quentin

Reputation: 944114

Assuming that you are using this module

The JS execution environment of Node and the JS execution environment of the browser you are controlling with Selenium are different. They don't share variables between them. You communicate between them by passing messages through the webdriver.

In order to read a variable from the currently loaded page, you need to use the execute method to pass some JS into the page.

browser.execute(function () {
    return window.location.href;
}).then(function (result) {
    console.log(result.value);
});

Upvotes: 0

cjungel
cjungel

Reputation: 3791

The code is running on node and not in the browser so when you pass window.location.href to your readGlobalVar function it fails because window is not defined.

If what you need is to wait until the url matches certain value you should consider until.urlMatches

Upvotes: 1

Related Questions