Friday, July 1, 2011

Selenium RC using Page Objects.

Within your web app's UI there are areas that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place. 

 Example for Page Object:
Homepage.java

package com.sai.pageobjects;

import com.thoughtworks.selenium.Selenium;

public class HomePage {

    private Selenium selenium;

    public HomePage(Selenium selenium) {
            if (!selenium.getText("//div[2]/div[2]/div[2]/div/div").equals("Now you can use Gmail in more languages!   Learn more")) {
                    throw new IllegalStateException("This is not Home Page of logged in user, current page" +
                                    "is: " +selenium.getLocation());
            }
    }

    public HomePage manageProfile() {
            // Page encapsulation to manage profile functionality
            return new HomePage(selenium);
    }
}

SignInPage.java

package com.sai.pageobjects;

import com.thoughtworks.selenium.Selenium;

public class SignInPage {

    private Selenium selenium;

 
    public SignInPage(Selenium selenium) {
            this.selenium = selenium;
            if(!selenium.getTitle().equals("Gmail: Email from Google")) {
                    throw new IllegalStateException("This is not sign in page, current page is: "
                                    +selenium.getLocation());
            }
    }

    /**
     * Login as valid user
     *
     * @param userName
     * @param password
     * @return HomePage object
     */
    public HomePage loginValidUser(String userName, String password) {
       
            selenium.type("Email", userName);
            selenium.type("Passwd", password);
            selenium.click("signIn");
            selenium.waitForPageToLoad("30000");

            return null ;
    }
}

testLogin.java

package com.sai.pageobjects;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;


@SuppressWarnings("deprecation")
public class TestLogin extends SeleneseTestCase{
    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
        SeleniumServer seleniumServer = new SeleniumServer();
         seleniumServer.boot();
        selenium.start();
        selenium.windowFocus();
        selenium.windowMaximize();
    }
@Test
    public void testLogin() throws Throwable {
    selenium.open("/");
    selenium.click("//li[7]/a/span[2]");
    Thread.sleep(10000);
            SignInPage signInPage = new SignInPage(selenium);
            HomePage homePage = signInPage.loginValidUser("username", "password");
            Assert.assertEquals(selenium.isElementPresent("compose button"),
                            "Login was unsuccessful");
    }
}

1 comment:

  1. Page Object Modeling Online Training
    http://www.21cssindia.com/courses/page-object-modeling-online-training-231.html
    Page Object Modeling Course Contents
    Why POM and what is POM
    About page objet gem and its features
    Creating POM Structure
    Finding elements and creating pages for application
    Creating panels, dialogues and other reusable components
    Creating and Identifying reusable actions and verifications in pages
    Page Factory
    Using pages and panels in you test cases
    About watir helper and handling Ajax elements
    Creating routes for your application
    Passing test data from yaml files
    Data Magic
    Using test data with Data Magic
    Creating your tests and running your tests

    ReplyDelete