Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JavaScript variables with Selenium IDE

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?

like image 595
Filip Dupanović Avatar asked Sep 14 '09 08:09

Filip Dupanović


People also ask

How can I get JavaScript variable in selenium?

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.

How do I use variables in selenium?

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 .

What is the command in Selenium IDE to execute a JavaScript?

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.

How do you run a variable in JavaScript?

After declaring a variable or function with the var keyword, you can call it at any time by invoking its name.


1 Answers

You should be able to get variables using the getUserWindow rather than getCurrentWindow. See the following example:

assertEval | this.browserbot.getUserWindow().myVar | Hello World!
like image 102
Dave Hunt Avatar answered Nov 05 '22 14:11

Dave Hunt