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

We are one community

Boddunan.com is a community driven articles and knowledge sharing portal. Members can share their knowledge with other members and visitors of the website and make them-self popular in the massive Internet world.

Boddunan.com is used not only as a platform to share the content and knowledge, but also a useful tool to learn from the vast amount of knowledge shared by thousands of registered users of the website. The content directory is a host of all the articles posted by the community and is available for the visitors of boddunan.com to read and learn from the extensive knowledge,