Wednesday, September 19, 2012

Selenium RC using Java Post 5 of 10

UI mapping and Page Objects

Today I’am going to tell you about UI mapping and Page Objects.
In your project create a Folder and names it as Resources
Now New à File -> and under the Resources folder create a new file and name it as locator.properties
Now in my locator file I need to create all control id’s of Login page. Here I’ll take username txt box , password txtbox and Login button
I can take it from my Login class

ABC.login.txtUserName=id=body_UserNameTextBox

ABC.login.txtPassword=id=body_PasswordTextBox

ABC.login.btnLogin=id=body_LogOnButton

Now we need to have a method for find the locator file and get the details from the file
In my Test Base class I creates 2 variables


public static String LocatorPrpertyPath = "D:\\worksplace\\Traning\\Resources\\locator.properties";

public static HashMap Locator = new HashMap();


Now in my method I am going to get the locator file and get the details of property file in to a Hash Map

@BeforeClass

public void getLocatorDetails() {

      Properties pro = new Properties();

      try {

 

            FileInputStream in = new FileInputStream(LocatorPrpertyPath);

            pro.load(in);

            Enumeration em = pro.keys();

            while (em.hasMoreElements()) {

                  String str = (String) em.nextElement();

                  Locator.put(str, (String) pro.get(str));

            }

      } catch (IOException e) {

            System.out.println(e.getMessage());

      }

}

This is also a Beforeclass method

Now I should change my code in Login class to get the control id’s from property file (actually from the hashmap )

public class Login {

 

      public Selenium SEL;

      public static String ptimeout;

      public static HashMap Locator;

     

      public Login(Selenium Sel, String PtimeOut, HashMaplocator){

           
            SEL = Sel;

            ptimeout = PtimeOut;

            Locator = locator;

      }

      public void login(String uname,String pword){

           

                SEL.windowMaximize();

                SEL.open("http://localhost:8080/LogOn.aspx");

              SEL.type(Locator.get("ABC.login.txtUserName"),uname);

              SEL.type(Locator.get("ABC.login.txtPassword"), pword);

              SEl.click(Locator.get("ABC.login.btnLogin"));

              SEL.waitForPageToLoad(ptimeout);

      }

}

This is the new login class. Now I am getting the login page controls from the locator.properties file and for this I used the filereading and hash map.
Now I have to change the login object initiation method since new variable is added to the the constructor.

Test Base class

public class TestBase {

     

      public Selenium Sel;

      public Login login;

      public static String SystemtimeOut;

      public static String LocatorPrpertyPath = "D:\\worksplace\\Traning\\Resources\\locator.properties";

      public static HashMap Locator = new HashMap();

 

@BeforeClass

public void InitSelenium(){

      Sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/LogOn.aspx");

      Sel.start();

      }

 

@BeforeClass

@Parameters({"pageTimeOut"})

public void setUp(String systemtimeout){

      SystemtimeOut = systemtimeout;

}

 

@BeforeClass(dependsOnMethods = { "setUp","InitSelenium" })

public void initPageObject(){

     

      login = new Login(Sel, SystemtimeOut, Locator);

     

}

@BeforeClass

public void getLocatorDetails() {

      Properties pro = new Properties();

      try {

 

            FileInputStream in = new FileInputStream(LocatorPrpertyPath);

            pro.load(in);

            Enumeration em = pro.keys();

            while (em.hasMoreElements()) {

                  String str = (String) em.nextElement();

                  Locator.put(str, (String) pro.get(str));

            }

      } catch (IOException e) {

            System.out.println(e.getMessage());

      }

 

Now you can see that login object has 3 parameters now. (Locator Hash map in the new one)
So using Page objects you can improve the maintainability of the Test automation project. If new control added or existing control changes you can handle it using property file.

No comments:

Post a Comment