This is my selenium code.

I am just finish my selenium code. I'm following page object pattern. My code does not follow any frameworks.

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.

How to write the code in json format ? Anybody please help me How to generate the code in json format? compare with above mentioned code


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"));


1 - Your JSON is wrong because you a trying to create a list using a key-value syntax, in the outer-most level. The first level should be a dictionary, with a list as value.







Read about JSON format here. A Java JSON reader can be found here.

2 - You must be careful to not fall in the trap of accessing the JSON file in your tests. You should create:

  • An interface (and an implementation class) that models the data; with method like getListOfGeoLocations

    Any change in the JSON file structure can be updated directly in the class.

  • Create a factory for that reads from the file and creates instance of this data model class.

    This way, any change in the files/folder organization or the way of creating the data model objects can be update directly in the factory.

Your tests should depend only on the interface.

  • Hai.. Can you please provide the code of how to keep test data in json ? Jan 23 '17 at 5:58
  • still I am not getting. in the above mentioned which class should I add interface Jan 23 '17 at 6:54
  • Checkout this tutorial on coding-decoding JSON with Java: tutorialspoint.com/json/json_java_example.htm Jan 23 '17 at 10:36
  • The interface serves to make your tests dependent upon an abstraction, not a concrete class - as I said, this will make your code more resilient to different formats of JSON. The interface would determine methods to get specific data - getListOfGeoLocations, e.g. - and the concrete classes would know how to parse a specific JSON structure to retrieve this data. Jan 23 '17 at 10:43
  • @AntonyNaveen I think you should make separated question; I'll keep an eye on the newest question and try to answer you right away. Jan 23 '17 at 16:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.