Can you please tell me how to write test data in the page object pattern.

Here I am using multiple packages. One is for locators identification, another is for page factory for initializing elements, and the other is package utilities for common values like get url. I also have a test package for testing a login module.

What I don't know is where should I put the test data class ? Here is my selenium code..

1). I want to keep the test data separately. Not scattering all over the script. Keep the test data in Json. And read it from there where ever it is necessary.

Still I'm getting confusing about where Should I put the json format?



package pageobjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class HomePage {
    @FindBy(how= How.NAME, using = "username")
    WebElement username;
    @FindBy(how=How.NAME, using = "password")
    WebElement password;
    @FindBy(how=How.XPATH, using="//*[@id=\'login-container\']/form/div[3]/div/p/input[1]" )
    WebElement button;

    //enter  username
    public void  userLogin(String user, String pass)



package steps;

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

import util.DriverManager;

public class LoginSteps {

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

        return homePageObj;



package util;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverManager {
    public static WebDriver driver;
    String baseUrl="//http:qms";
    public DriverManager()

        driver=new ChromeDriver();





 package login;

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

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

    import static util.DriverManager.driver;

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

        @DataProvider(name= "login")
        public static java.lang.Object[][] loginData(){

            return new Object[][]{{"geoso","1"},{"ges","2"},{"geo","1"}};
        @Test(dataProvider = "login")
        public void verifyValidLoginWithDataProvider(String userName,String password)
            LoginSteps loginSteps= new LoginSteps();
            HomePage ex=loginSteps.Login(userName,password);
            Assert.assertTrue(driver.getPageSource().contains("Hello Naveen"));

  • create a resources directory or similar, and store the request bodies there . . . or if want to get really fancy/need to scale, create a DB and store the items there.
    – ernie
    Jan 23, 2017 at 18:59
  • @ernie Not get the point . how to create a class file and keep the test data in json format with my above mentioned code ?
    – user21268
    Jan 24, 2017 at 7:10
  • @AnthonyNaveen So you create a resource file in your project that is your JSON data file. Then in your code you include the resource and read the values from the JSON file as it loads it into memory at runtime.
    – mutt
    Aug 9, 2017 at 15:52

1 Answer 1


At this point its worth building a user data generator that can be used to provide coherent sets of login data and credentials for your tests.

Something like faker https://github.com/DiUS/java-faker would do the job and can be customised to fit your needs. It could later be rolled into a service to support CI environments.

By having a point that tests go to get resources you can ensure they get credentials that will work and you have an opportunity to initialise the corresponding state for the fixtures that use those credentials. This also keeps credentials out of source code which makes it easier to share or move the tests.

  • Yes, agree create a test data layer. Mar 12, 2018 at 22:39

Your Answer

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