Popular Articles

At one time Russia rivaled the USA as a global power, but then came Gorbochov and we all know that Russia broke up into 18 states. This was also the end
From the 8th century the Muslim invaders began to make forays into Hindustan and by the 12th century the entire North India and the throne at Delhi had
Privacy is an important thing in every section. Same goes for mobile phones too. People always have something to hide from others on their mobile phone

Latest Articles

Subhas Chandra Bose was a rival of Gandhi and during the war he threw his lot with the Fascist powers and joined them. He had a one point agenda to overthrow
At one time Russia rivaled the USA as a global power, but then came Gorbochov and we all know that Russia broke up into 18 states. This was also the end
I had in an earlier post pointed out that the great Hindu warrior  Hemu was struck by an arrow in his eye in the third battle of Panipat and lost

Using selenium getEval we can achieve many tasks which Selenium RC alone can't do. 

 

Syntax:

getEval("javascript code as string"); It returns result of Java script.

 

Let's have simple example first, below example written using Junit but you can convert getEval script it to your preferred language as it is.

 

public class selenium extends SeleneseTestCase{

     

      @BeforeClass

      public void setup() throws Exception

      {

            selenium = new DefaultSelenium("localhost",4444,"*firefox""http://www.google.com");

            selenium.start();

      }

     

      @AfterClass

      public void tearDown()

      {

            selenium.close();

            selenium.stop();

      }

      @Test

      public void testNew() throws Exception

      {

           

          selenium.getEval("alert(\"hi\")");

           

      }

}

 

When you run this script you can see an alert "Hi".

 

Now testNew() function  replace with below code.

 

public void testNew() throws Exception

      {

           

            selenium.open("http://www.google.com");

            String sCount =    selenium.getEval("window.document.getElementsByTagName('input').length");

            System.out.println(sCount);

      }

 

This will give output 11. 

 

Try other example now. To resize your browser and  type search text in google text box.


 

      publicvoid testNew() throws Exception

 

      {

 

              selenium.open("http://www.google.com");

 

            selenium.getEval("selenium.browserbot.getCurrentWindow().moveTo(0, 0)");

 

            String screenWidth = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availWidth");

 

        String screenHeight = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availHeight");

 

        selenium.getEval("selenium.browserbot.getCurrentWindow().resizeTo(" + screenWidth + ", " + screenHeight + ")");

 

      

 

            String txtBoxId = selenium.getEval("window.document.getElementById(\"q\").name");

 

            selenium.type(txtBoxId,"selenium rc");

 

                       

 

      }

Above script maximize the goole.com window and type search text in Text Box. You can execute any Java Script using this function. It can be used to get check box count, check box selected count for automation. 

 

Let us one more examp of AJAX. Problem with AJAX is only part of web page will be loaded so its hard to know whether page is loaded or not before performing any ohter actions. Sometimes encounter with issues like object not found, element not found and access denied error. One way of dealing with this is using waitForControlLoad, waitForElementPresent commands. 

 

Other alternative is using getEval funcion more effectively. AJAX come up with funciton called readySate of XmlDom object. Using below code snippet can achieve this functionality.

 

string state;

            try

            {

                state = _selObj.GetEval("selenium.browserbot.getCurrentWindow().document.readyState");

                if (state == "complete")

                    return true;

                else

                    return false;

            }

            catch (Exception ex)

            {

                throw new Exception("Error Occured at IsBrowser Loaded()" + ex.Message);

                return false;

            }

More can do with getEval! Happy automation!!

  • No comments found