4

I am using page object pattern with java language.First step is driver initialization then go to login page.These steps are repeatable in every test pages.

Here is my code..

util

package util;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


/**
 * Created by naveen on 29/11/16.
 */
public class DriverManager {
    public static WebDriver driver;
    String baseUrl="http://qmsadm.local";
    public DriverManager()
    {

        System.setProperty("webdriver.chrome.driver","/home/naveen/chromedriver");
        driver=new ChromeDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();

    }

}

Test cases

package client;




import org.testng.Assert;
import org.testng.annotations.*;

import pageobjects.ClientPage;
import pageobjects.HomePage;
import steps.LoginSteps;
import steps.clientsteps;
import util.DriverManager;

import static util.DriverManager.driver;

public class clientTest {
    @BeforeSuite(groups = {"regression"})
    public void initDriver() {
        DriverManager manager = new DriverManager();

        LoginSteps loginSteps = new LoginSteps();
        HomePage expected_message = loginSteps.Login("geosony", "1");
    }

    @Test
    public void verifyValidLoginWithDataProvider() throws Exception {
        clientsteps log = new clientsteps();



        ClientPage ex = log.Valid("guru99","Test Group","ekm","active","1","2");
         Assert.assertTrue(driver.getPageSource().contains("Hello Sony George"));

    }
}

package steps;


import org.openqa.selenium.support.PageFactory;
import pageobjects.ClientPage;
import pageobjects.HomePage;

import util.DriverManager;

/**
 * Created by srikanth on 20/11/16.
 */
public class LoginSteps {

    public HomePage Login(String nam, String pas) {
        HomePage homePageObj = PageFactory.initElements(DriverManager.driver, HomePage.class);
        homePageObj.userLogin(nam,pas);

        return homePageObj;
    }


}

Here initdriver method. Every time for new test pages I want to write the same method again again. How to reduce the repeatable statement?

4

You can use a base-class to contain the duplicated code for each test.

Read about inhertance and subclasses here: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

If you have a TestBase class which contains the init/setup functions you need for each test in your test you can extend like this:

public class clientTest extends TestBase {
}

This is a programming 101, maybe first ask your local developers for ideas?

For testSteps I prefer the AAA pattern:

@Test
public void verifyValidLoginWithDataProvider() throws Exception {
    // Arrange
    HomePage page= new LoginSteps().Login("geosony", "1");
    clientsteps log = new clientsteps();

    // Act  
    ClientPage ex = log.Valid("guru99","Test Group","ekm","active","1","2");

    // Assert
    Assert.assertTrue(driver.getPageSource().contains("Hello Sony George"));
}

In your tests you want to find everything the tests needs to be able to work, hiding the loginSteps is not a good thing in my book.

Code duplication like this is not so bad, because it makes it very clear what the test actually does. Also keep in mind that the login steps will not change to much anyways, centralizing it in a PageObject like you did might be good enough. I also login with different users, you do need to get rid of the "strings" and centralize those into data objects, this could become a pain to change in hundreds of tests

| improve this answer | |
  • Ya i got the idea about inheritance I wrote public class clientTest extends DriverManager Now there is repeatable code for loginsteps. what is the solution for this ? – user21268 Dec 5 '16 at 8:45
  • There is a repeatable code for login page. Every time I use this steps for login. What will do for this repeable code ? – user21268 Dec 5 '16 at 8:58
  • hai sir can you review my code ? please give ur mail id.. I want to correct my code... – user21268 Dec 5 '16 at 9:30
  • 2
    No, I cannot review your code I have other work todo, maybe find a local coding/testing meetup to find people around you that can help you. Also just grow over time. You will learn what works and what doesn't. Reads books, blogs and keep asking questions. – Niels van Reijmersdal Dec 5 '16 at 9:43
1

Generally, there is nothing wrong with having login steps in your initialize method. What I would do here:

Create helper class AuthHelper and put it as property in TestBase. Then I wouldn't have to initialize LoginSteps class every time the login is needed (it might be quite often) or change every test when I rename or do something else to LoginSteps. Then I would call it like that:

AuthHelper().Login("username", "password");

or class with static method in it, something like:

Login.WithCredentials("username", "password");

If you need code review, post your code to stack exchange - Code Review, a lot of people will found time to help you.

Good luck!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy