2

I am working in Selenium with page object design pattern, without any Selenium frameworks. I did a sample login page. I am keeping my test data in a property file:

baseurl=http://
nam=geo,sony,geo,geogr,ge
pas=1234,123,56,345,12

There is pageobject package to identify the locators.

package pageobject;

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

/**
 * Created by naveen on 6/3/17.
 */


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)
    {
        username.sendKeys(user);
        password.sendKeys(pass);
        button.click();
    }
}

and steps package for initializing the elements for locators class.

package steps;

import org.openqa.selenium.support.PageFactory;
import pageobject.HomePage;
import util.DriverManager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by naveen on 6/3/17.
 */
public class LoginSteps extends DriverManager {

    public LoginSteps() throws IOException {
    }

    //    public HomePage Login(String nam, String pas) {
    public HomePage Login() {
        String nam, pas;
        //  SamleFileInput();

        String[] namArray = prop.getProperty("nam").split(",");
        String[] pasArray = prop.getProperty("pas").split(",");

        HomePage homePageObj = PageFactory.initElements(DriverManager.driver, HomePage.class);

        String resultData;
        String status;
//        BufferedWriter writer = null;

        String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
        File logFile = new File(timeLog + ".txt");

//        writer = new BufferedWriter(new FileWriter(logFile))

//        homePageObj.userLogin(nam,pas);
        int len = namArray.length;
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFile))) {
            for (int i = 0; i < len; i++) {
                nam = namArray[i];
                pas = pasArray[i];

                homePageObj.userLogin(nam, pas);


                if (driver.getPageSource().contains("Hello Sony George")) {
                    status = "Success";
                } else {
                    status = "Failed";
                }

                resultData = "User: " + nam + " Pwd: " + pas + "  Status: " + status+"\n";
                System.out.println(resultData);


                writer.append(resultData);


            }
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Close the writer regardless of what happens...

            } catch (Exception e) {
            }


//        homePageObj.userLogin(nam,pas);

            return homePageObj;
        }
    }
}

util package to initilize the webdriver

package util;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by naveen on 6/3/17.
 */
public class DriverManager
{
    public static WebDriver driver;
    String baseUrl;
    public Properties prop;

    public DriverManager() throws IOException {
        // Get baseUrl property
        File file = new File("config.properties");
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //load properties file
        prop = new Properties();
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        }




        // Start driver
        System.setProperty("webdriver.chrome.driver", "/home/naveen/chromedriver");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        baseUrl = prop.getProperty("baseurl");
        driver.get(baseUrl);
    }
}

Here is the login test itself.

import org.testng.Assert;
import org.testng.annotations.Test;
import pageobject.HomePage;
import steps.LoginSteps;

import java.io.IOException;

import static util.DriverManager.driver;

/**
 * Created by naveen on 6/3/17.
 */
public class LoginTest
{
    public LoginTest() throws IOException {
    }



    @Test
    public void initDriver() throws InterruptedException, IOException {
        //Arrange
        try {
     HomePage page = new LoginSteps().Login();
            Assert.assertTrue(driver.getPageSource().contains("Hello Sony George"));


        } catch (IOException e) {
            e.printStackTrace();
        }

        //AppointmentPage page1=new Appointmentsteps().Valid(prop.getProperty("num"),prop.getProperty("usernam"),prop.getProperty("ema"),prop.getProperty("cliid"),prop.getProperty("strdate"),prop.getProperty("cat"));


    }


}

Is it a good practice to keep the looping statement for test data in loginsteps ? whether I have a user register page, so I think it will more shuffled code. so Is it possible to keep the looping statement for all common class files to put in drivermanager ? whats your opinion ?

2

Since you are using TestNG, I would suggest you to go with @DataProvider. This way you can be more sure that what test data passed and what failed.

If you loop inside login method, you will have only one test case. And it will be difficult to figure out what failed when you have 1000s of test data to be tested.

Also, loginStep is not meant to parse the data. I would keep that away from it. May be looping part, I will include into test class. It will increase my re usability of loginStep class and method.

  • Is it possible to keep the looping statement in driver manager class as a common for all classes ? – user24314 Mar 7 '17 at 5:08
  • It is not about possibility. Its about making the code reusable. Whats your idea behind keeping it into driver manager? – Mayur Mar 7 '17 at 7:52
0

I cannot see any reason for the looping. It appears to simply be doing the same happy path test 5 times with no clear reason why the test is doing so. This fails the principle of least surprise. If there is a valid reason then make it clear.

I suspect you are gaming your test metrics believing this offers some sort of extra value. After all more tests must be better, right? In functional testing the answer is a clear no. It offers no extra value but increases the costs, complexity, maintenance and run time.

Instead consider and list the different scenarios necessary to prove the feature works as expected; each test should know the pre-condition(s) that make it valid, how to execute the feature under test (the test objective), the post-condition(s) justify a pass. I can think of three plausible scenarios (expressed below in Gherkin).

Given an Authorised user
When the authentication is performed
Then access is granted

Given a Suspended user
When the authentication is performed
Then access is refused

Given an Unregistered user
When the authentication is performed
Then access is refused

This could be data-driven, but with only three test cases, offers marginal benefit. If you had to test thousands of different products, then a data-driven test would be a clear advantage. Weigh up the cost vs benefit.

Your Answer

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