Monday, September 24, 2012

Selenium RC using Java Post 6 of 10


Wrapping in Selenium

 



You can wrap frequently used selenium calls with functions or class methods of your own design. 

For example, many tests will frequently click on a page element and wait for page to load multiple times within a test.  Using this you can remove your duplicate codes as well as you can save time.

 

I create a new class under the …..test.util package.  I name my class as SeleniumUtil.java

Now I need to create a method for Click and Wait. In my tests sometimes I need to click a locator and wait for some time until something happens ( Eg- Until it changes values in another  dropdown)

 

Following is my SeleniumUtil class and mehod created





package com.ABC.test.util;

 

import com.thoughtworks.selenium.Selenium;

 

public class SeleniumUtil {

Selenium Sel;

 

public SeleniumUtil(Selenium sel){

     

      Sel = sel;

}

 

public void ClickAndWait(String locator, String timeOut)   

Sel.click(locator);

      Thread.sleep(Long.parseLong(timeOut));

      
}


}






Here I pass the locator and timeout as a parameter. In the body you can see that I click the locator and wait for some time.

 

 

Now whenever I have to use Click and wait I can use this method in my tests.

 

Now I use this in one of my other classes (Activity Class )


public class Activity {

public Selenium SEL;   

public SeleniumUtil sUtil;

     

      public Activity(Selenium Sel){

           

            SEL = Sel;

            sUtil = new SeleniumUtil(Sel);

           

      } 

      public void gotoSetupPage(String timeOut){           

            SEL.click("css=div.ajax__dropdown_arrow.ajax__dropdown_arrow_image");

            sUtil.ClickAndWait("link=Super Admin Menu", timeOut);

            sUtil.ClickAndWait("link=Setup", timeOut);

 

 

}

}

No comments:

Post a Comment