Reputation: 36674
I try to bowser to a page with selenium web-driver.
I then inject and execute some js via selenium web-driver.
I try to access these vars in this opened browser console, but it seems they were not created. How come?
I have this code:
public void foo (){
String script =
"var aLocation = {};" +
"var aOffer = {};" +
"var aAdData = " +
"{ " +
"location: aLocation, " +
"offer: aOffer " +
" };" +
"var aClientEnv = " +
" { " +
" sessionid: \"\", " +
" cookie: \"\", " +
" lon: 34.847, " +
" lat: 32.123, " +
" venue: \"\", " +
" venue_context: \"\", " +
" source: \"\"," + // One of the following (string) values: ADS_PIN_INFO,
// ADS_0SPEED_INFO, ADS_LINE_SEARCH_INFO,
// ADS_ARROW_NEARBY_INFO, ADS_CATEGORY_AUTOCOMPLETE_INFO,
// ADS_HISTORY_LIST_INFO
// (this field is also called "channel")
" locale: \"\"" + // ISO639-1 language code (2-5 characters), supported formats:
" };" +
"W.setOffer(aAdData, aClientEnv);";
javascriptExecutor.executeScript(script);
}
which yields:
script =
var aLocation = {};
var aOffer = {};
var aAdData = {
location: aLocation,
offer: aOffer
};
var aClientEnv = {
sessionid: "",
cookie: "",
rtserver - id: 1,
lon: 34.847,
lat: 32.123,
venue: "",
venue_context: "",
source: "",
locale: ""
};
W.setOffer(aAdData, aClientEnv);
I evaluate aLocation
in this browser console and get "variable not defined"
. How can this be?
Upvotes: 2
Views: 745
Reputation: 14287
Because your variables are NOT global
. As soon as you declare them with var
they are scoped. If you want to test something out, just put nemo=100;
in your script above and try printing out in console, it should work.
Edit #1
By the way, by no means I'm advocating global variables here. I'm just trying to explain what happened to your variables in JS executed by WebDriver. If you want to use global variables then more explicit declaration like window.foo
makes more sense like others have suggested. However overall try to avoid using them. Moreover try to avoid executing JavaScript using WebDriver in the first place
unless you have no other choice. WebDriver is supposed to simulate a real user for you and your user is less likely to execute a JavaScript to interact with your web app.
Upvotes: 1
Reputation: 151461
It is important to know how Selenium executes the JavaScript that is executed in the browser.
Contrarily to what nilesh's answer implies slapping a var
in front of a variable declaration does not take it out of the global space. For instance if var foo = 1
is executed outside of a function scope, it will declare a global variable named foo
.
The key is how Selenium executes the script. It would be possible for Selenium to execute the script passed to executeScript
in the global space. (There are ways.) However, it does not. What it does is wrap the script in a new function so any var
that appears in the code passed to executeScript
is going to declare a local variable.
Just dropping the var
would work but I prefer to be explicit when I want to manipulate the global space. I explicitly access the window
object (e.g. window.foo = 1
). Dropping var
looks like it could be a mistake, whereas using window.
looks deliberate.
Upvotes: 3