3

In my previous project, I worked in selenium with java. In that project I following page object pattern. This is my sample code of selenium with java:

# Create Page Objects

public class HomePage {
   @FindBy(how= How.LINK_TEXT, using = "MY ACCOUNT")
   WebElement my_account;
   @FindBy(how= How.ID, using = "link-to-login")
   WebElement login_link;
   @FindBy(how= How.LINK_TEXT, using = "LOGOUT")
   WebElement logout;
   public void clickOnLoginLink()
   {
       login_link.click();}
   public String getMyAccountText()
   {
       return  my_account.getText();}
   public void clickLogOut() {
       logout.click();  }
}

Separate out Tests , Objects , and Implementation Flows

Steps

public String InvalidLogin(String userName, String password) {
   HomePage homePage = PageFactory.initElements(DriverManager.driver, HomePage.class);
   homePage.clickOnLoginLink();

   LoginPage loginPage = PageFactory.initElements(DriverManager.driver, LoginPage.class);
   loginPage.enterUsername(userName);
   loginPage.enterPassword(password);
   loginPage.clickSubmit();
   return loginPage.getInvalidLoginText();}

util

public class DriverManager {
    public static WebDriver driver;
    String baseUrl;
 // Start driver
        System.setProperty("webdriver.chrome.driver", "/home/naveen/chromedriver");
        driver = new ChromeDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();
    }
}

Test

@Test
public void verifyValidLogintoSpree()
{
   LoginSteps loginSteps = new LoginSteps();
   String expected_message = loginSteps.Login("[email protected]", "xtxtxt");
   Assert.assertEquals(expected_message, "MY ACCOUNT");
}

These page object is for identifying locators in for one class and then initialising the web elements for steps class. and test code for test file. I want to know the detailed explanation and advantages of using page object pattern. what should I need to improve or going good? I want to know that my understand are correct or not

1

3 Answers 3

3

I want to know the detailed explanation and advantages of using page object pattern.

Selenium has this documented in their Test Design Considerations page.

What should I need to improve or going good?

The way you handle the WebDriver will not work in parallel runs, as you have a static global variable. I have the driver setup in a TestBase class that each Test class inherits. Each test creates a driver for itself.

Also, you are using Assert backwards. The expected object is "MY ACCOUNT" so it should be the first parameter. This way when an exception occurs the error will read "Expected : MY ACCOUNT. Encountered: {valueFromTheVariable}."

The good news is your basic understanding is correct.

POMs contain:

  1. The object constructor (which initializes the Page Objects)
  2. The find element locators
  3. Page specific functions

Tests should follow Arrange, Act, Assert. Tests include:

  1. Arrange - Call a POM method to get you where you need to go (like a login page)
  2. Act - Call a POM method to perform some page specific action (like filling out and submitting a form)
  3. Assert - Use xUnit/TestNG/Etc. assert classes to ensure that the series of events ends with some expected condition
2

I've written extensively on your question: "I want to know the detailed explanation and advantages of using page object pattern."

The key is that classes should only do one thing! This is called "Separation of Concerns" or "The Single Responsibility Pattern" which are bedrock patterns for keeping code maintainable. If you mix concerns, the code is not maintainable in the long term.

Secondly, the most important other concept is "Favor Composition over inheritance". With this concept, we learn to create add-in parts to our applications. With composition we are able to add, maintain, remove parts without impacting any other parts.

Finally, by doing this you are much more ready to create tools that generate code automatically. Automate the Automation!

Here's an article on going even further than just the Page Object Model.

0

You are correct. And, in addition to what others suggested, page object pasterns scope is to write simple and less code, make code repeatable, easy to maintain. With that mind you can improve by just using:

@FindBy(linkText="My Account")
private static WebElement my_account;

instead of repeating for every elements locator

how= How.LINK_TEXT, using = "MY ACCOUNT"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.