Using selenium.GetEval(script)to execute Javascript

>The task at hand was to navigate through the DOM of a particular webpage and check the
style.display attribute of an object or element if you prefer. There are two elements on this page that displayed either success or failure messages.(msgSuccess and msgFailed)
For perfomance reasons, if previously a successful message was displayed and subsequently the you get a failure message, what the developers of the page did was to amend the style.display attribute.

<span style=”display: none;” id=”msgSuccess” class=”MyMsgSuccess”>

The HTML code above implies the element ‘msgSuccess’ would not be displayed

<span style=”” id=”msgSuccess” class=”MyMsgSuccess”>

In the second case, the msg success would be displayed ….

The problem i encountered was, sometimes i would have

< span style=”display: none;” id=”msgSuccess” class=”MyMsgSuccess” > Operation Successful </span >

If i do a selenium.GetText(“msgSuccess”), i get the text returned even though the style.display is set to none. Hence the need for the Selenium.GetEval to check the value of the style.display attribute as that seem as a true test for the web page

I have done:

const string script = “document.getElementById(\”msgSuccess\”).style.display”;
string evalResult = selenium.GetEval(script);

The above script failed as selenium was executing the javascript in the selenium console and not on the Application under test (AUT)
and searching the internet got me doing:

const string script = “selenium.browserbot.getCurrentWindow().document.getElementById(\”msgSuccess\”).style.display”;
string evalResult = selenium.GetEval(script);

This way i got the javascript to be run on the Application under test ….

Advertisements
This entry was posted in Software Testing and tagged , , , , . Bookmark the permalink.

One Response to Using selenium.GetEval(script)to execute Javascript

  1. Anonymous says:

    >That was useful for me!Thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s