Tuesday, September 18, 2012

Selenium RC Using Java Post 4 of 10

Now I am going to talk about concept of page object. This is an extremely useful design pattern for UI tests as it abstracts away how we get to different pages or screens. Page Object design helps us create a DSL for our tests that if something were to change on the page we don't need to change the test, we just need to update the object that represents the page

Now I’m going to create a new class which is to place my all the functional methods related to Login

Page. Before that I create a new package which I am going to label as com.ABC.Test.Functional under this package I creates the class Login

public class Login {

public Selenium Sel;

public static String ptimeout;

public Login(Selenium Sel, String PtimeOut){

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

ptimeout = PtimeOut;

}

public void login(String uname,String pword){

Sel.windowMaximize();

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

Sel.type("id=body_UserNameTextBox",uname);

Sel.type("id=body_PasswordTextBox", pword);

Sel.click("id=body_LogOnButton");

Sel.waitForPageToLoad(ptimeout);

}

}

Now I create the Selenium instance and login method and included all of login steps here. So I removed my Login steps from the testSucessLogin method.

Now I’m going back to TestBase class and create new method initPageObject()

Within this method I am going to create new object of Login class. (All the page objects will be initialize here )

public void initPageObject(){

login = new Login(Sel, SystemtimeOut);

}

I creates a new method “setup “ which is going to list all the setup variables . Here I am going to setup the “systemtimeout” I define this as a @Beforeclass and do the parameterization using TestNG xml.

@BeforeClass

@Parameters({"pageTimeOut"})

public void setUp(String systemtimeout){

SystemtimeOut = systemtimeout;

}

Now in my PageObject method I might get a problem because before create the objects I need to run the initSelenium and Setup method. For this I use TestNG annotation - @BeforeClass(dependsOnMethods = { <method names})

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

public void initPageObject(){

login = new Login(Sel, SystemtimeOut);

}

Now I need to call my login method under my testSucessLogin Test method

login.login(uname, pword);

No comments:

Post a Comment