Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm running tests with a protractor, but it seems impossible to access the JS 'window' object. I even tried adding a tag in my html file that would contain something like:

var a = window.location;

and then try to expect(a) but I couldn't make it work, I always get undefined references...

How should I process to access variables that are in the browser scope?

1 Answer

0 votes
by (62.9k points)

Assuming you are using a recent version of Protractor, let's say >= 1.1.0, hopefully >= 1.3.1

Attempting to access Browser side JS code directly from protractor will not work as a result of protractor runs in NodeJS and each Browser side code is executed through selenium JsonWireProtocol. 

A working example is shown below:

browser.get('https://angularjs.org/');

One-liner promise that, as of today, resolves to '1.3.0-rc.3' browser.executeScript('return window.angular.version.full;');

You can use it directly in an expect statement given Protractors expect resolves promises for you:

expect(browser.executeScript('return window.angular.version.full;'))

toEqual('1.3.0-rc.3');

Longer example passing a function instead of a string and while not expected to resolve the promise for you. i.e.for additional control and for doing a little fancy thing with the result.

browser.driver.executeScript(function() ).then(function(result) );

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...