I'm wondering if it's possible to access page JavaScript variables with Selenium. I have an application that's using a variable attached to the window object. It has a global scope and I can access it either with window._myvar
, window['_myvar']
, _myvar
, this['_myvar']
, this._myvar
depending on the context.
So I tried to get Selenium to echo it. As far as I understand, in Selenium IDE the context in which everything runs is selenium
. I tried doing this.browserbot.getCurrentWindow()._myvar
, this.browserbot.getCurrentWindow()[_myvar]
to no avail. I get bumped with the following error Unexpected Exception: message -> eval(match[1]) is undefined
.
Anyone managed to access their page's JavaScript?
We can read Javascript variables with Selenium webdriver. Selenium can run Javascript commands with the help of executeScript method. The Javascript command to be executed is passed as an argument to the method. Also we have to add the statement import org.
Create a store command to store a variable in Selenium IDE. Right-click the row where the type command is and select Insert new command. Click the new row that is inserted and select store from the Command list. Set the Value field with the variable name you want, such as username .
execute script is one of the commands in Selenium IDE. The purpose of execute script command in Selenium IDE, is to execute the JavaScript code in Selenium IDE.
After declaring a variable or function with the var keyword, you can call it at any time by invoking its name.
You should be able to get variables using the getUserWindow rather than getCurrentWindow. See the following example:
assertEval | this.browserbot.getUserWindow().myVar | Hello World!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With