Search This Blog

Friday, October 28, 2011

Selenium RC getEval tutorial and examples.

Using selenium getEval we can achieve many tasks which are not possible to achieve normally. To achieve this  Selenium RC provided with getEval function.

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. 


public void 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.


Lot can do with getEval! Happy automation!!

No comments:

Post a Comment