0

Can we use Data Driven approach with Page Object model using Page Factory, any example or link will help. I am trying to implement POM with PageFactory but at the same time I want to make it data driven using excel file Following is the code snippet after data driven and after data driven is not working

@Test
public void valid_email_password()
{
    int iNoofRows = objexcel.rowCount();
    for(int i=1; i<iNoofRows; i++)
    {
    objLogin = new ScoreLogin(driver);
    objLogin.loginScore(objexcel.getData(0, i, 0), objexcel.getData(0, i, 1));
    objHomePage = new HomePage(driver);
    String DashboardUserName = objHomePage.getHomePageUserName();
    Assert.assertTrue(DashboardUserName.contains("Super Admin"));
    }
}

Following is the code snippet before data driven

@Test
public void valid_email_password()
{
    int iNoofRows = objexcel.rowCount();
    for(int i=1; i<iNoofRows; i++)
    {
    objLogin = new ScoreLogin(driver);
    objLogin.loginScore("testuser", "testpassword");
    objHomePage = new HomePage(driver);
    String DashboardUserName = objHomePage.getHomePageUserName();
    Assert.assertTrue(DashboardUserName.contains("Super Admin"));
    }
}
  • That are two orthogonal problems. What problem are you having with combining both? – dzieciou Apr 27 '16 at 14:13
  • I've updated the question – Hassan Apr 27 '16 at 14:51
  • 2
    You have my permission to use data driven approach. Rest is "matter of simple programming". Sorry no examples, I try to avoid Java, and in Python I did not ever felt the need for factory pattern yet. – Peter M. - stands for Monica Apr 27 '16 at 14:57
  • What you're wanting is called a Parameterized Test in JUnit. github.com/junit-team/junit4/wiki/Parameterized-tests – Paul Muir Apr 27 '16 at 16:57
  • The overhead of the page factory will be minimal compared to the network latency and page waits. – Martin Spamer Jul 14 '18 at 19:49
0

Possible solution could be- You can use dataprovider annotation of testNG for the same. in dataprovider method write logic for reading excel file and in test cases use that dataprovider method.

  • 1
    Considering how old this question is, could you please edit your answer to show some example code? That would make this a better answer to a question that could benefit from the attention. – Kate Paulk Jul 19 '17 at 11:59
0

From your question, we have 2 parts as follow: Let me use C# Selenium.

  1. Organizing the Definition of test objects using page factory. Please have a look in my answer at another thread

  2. Data Driven approach for you test functions, and to handle this part we have to handle 2 points.

    1. Data management functions to handle use SQL queries to read/update Excel sheets.

Excel Connection String, to be stored in config file

<add name="MyExcelConn" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Path to Test Data Sheet];Extended Properties='Excel 12.0 Xml;HDR=YES';"/>

Sample Reading , function to return data table.

public static DataTable GetExcelDataTable(string query)
        {

            OleDbConnection conn = new OleDbConnection();
            if (ConfigurationManager.ConnectionStrings["MyExcelConn"] != null)
                conn.ConnectionString =ConfigurationManager.ConnectionStrings["MyExcelConn"].ConnectionString;
            DataTable dt = new DataTable();
            OleDbCommand cmd;
            try
            {
                conn.Open();
                cmd = new OleDbCommand(query, conn);
                dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));

                return dt;
            }
            catch (Exception ex)
            {

               // log error
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

2. How to Drive the Test Data for you test functions.

Design of Excel Sheet

Excel sheet sample login test data

Test Function Design, After following the previous shared answer above. your function will looks like this

public static bool Login(DataRow loginTable)
        {
            bool retRes = true;
            try
            {
                Browser.pgLogin.txtUserName.SetValue(loginTable["userID"].ToString().Trim());                
                Browser.pgLogin.txtPassword.SetValue(loginTable["Password"].ToString().Trim());                
                Browser.pgLogin.btnSignin.Click();

                return retRes;
            }
            catch (Exception ex)
            {
               // Log error
                return false;
            }
        }

Data Table to hold Test Data

 DataRow loginRow = null;
loginRow = GetExcelDataTable("Select * from [Login$] where RowID = " + iLoginRow).Rows[0];

Calling the Test Function with Test Data

Login(loginRow );

I hope this helps you and answers your question. for more details we can chat.

Your Answer

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

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