Popular Articles

India is home to great literature and two of the greatest epics originated from the sub-continent. They are the Ramayana and the Mahabharata. The Mahabharata
Gift buying is something most people just swear by. Of all things it counts up to make a gift count, it can become a nightmare for some to zero in on the
As per science, the universe was born with the Big Bang. Religions argue that everything was created by an almighty God. Amidst the scientific theories

Latest Articles

Gift buying is something most people just swear by. Of all things it counts up to make a gift count, it can become a nightmare for some to zero in on the
India is home to great literature and two of the greatest epics originated from the sub-continent. They are the Ramayana and the Mahabharata. The Mahabharata
As per science, the universe was born with the Big Bang. Religions argue that everything was created by an almighty God. Amidst the scientific theories

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