Friday 8 March 2013

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.


  1. public class selenium extends SeleneseTestCase{  
  2.        
  3.       @BeforeClass  
  4.       public void setup() throws Exception  
  5.       {  
  6.             selenium = new DefaultSelenium("localhost",4444,"*firefox""http://www.google.com");  
  7.             selenium.start();  
  8.       }  
  9.        
  10.       @AfterClass  
  11.       public void tearDown()  
  12.       {  
  13.             selenium.close();  
  14.             selenium.stop();  
  15.       }  
  16.       @Test  
  17.       public void testNew() throws Exception  
  18.       {  
  19.              
  20.           selenium.getEval("alert(\"hi\")");  
  21.              
  22.       }  
  23. }  




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

Now testNew() function  replace with below code.


  1. public void testNew() throws Exception  
  2.       {  
  3.              
  4.             selenium.open("http://www.google.com");  
  5.             String sCount =    selenium.getEval("window.document.getElementsByTagName('input').length");  
  6.             System.out.println(sCount);  
  7.       }  
This will give output 11. 


  1. public void testNew() throws Exception  
  2.       {  
  3.              
  4.             selenium.open("http://www.google.com");  
  5.             selenium.getEval("selenium.browserbot.getCurrentWindow().moveTo(0, 0)");  
  6.             String screenWidth = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availWidth");  
  7.         String screenHeight = selenium.getEval("selenium.browserbot.getCurrentWindow().screen.availHeight");  
  8.         selenium.getEval("selenium.browserbot.getCurrentWindow().resizeTo(" + screenWidth + ", " + screenHeight + ")");  
  9.         
  10.             String txtBoxId = selenium.getEval("window.document.getElementById(\"q\").name");  
  11.             selenium.type(txtBoxId,"selenium rc");  
  12.                          
  13.       }  


Above script maximize the goole.com window and type search text in Text Box.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...