2

I'm using POM framework and managed the code in following way :

This is My InviteFriendPage

public class InviteFriends 
{
    @FindBy(xpath ="//li/a[normalize-space()='Invite Friends']")
    public WebElement inviteFriendsLink;

    @FindBy(id = "divLoader")
    public WebElement loader;

    @FindBy(xpath = "//input[@name='lstInviteFriendsModel[1].FriendName']")
    public List<WebElement> friendName;

    @FindBy(xpath = "//input[@name='lstInviteFriendsModel[1].FriendMobile']")
    public WebElement friendNumber;

    WebDriver driver;

    public InviteFriends(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
        this.driver=driver;
    }

    public void inviteFriend(String friendName, String friendMobile)
    {

        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("arguments[0].click();", inviteFriendsLink);

        for(int i=1;i<=3;i++)
        {
            driver.findElement(By.xpath("//input[@name='lstInviteFriendsModel["+i+"].FriendName']")).sendKeys(friendName);
            driver.findElement(By.xpath("//input[@name='lstInviteFriendsModel["+i+"].FriendMobile']")).sendKeys(friendMobile);
        }
    }

}

This is my Executor Class from where I'm calling all the pages by creating object

public class MainSuitExecuter extends DriverSetup
{

    @Test()
    public  void submitFeedback() throws InterruptedException
    {

        ContactUsPage conpage = new ContactUsPage(driver);
        conpage.submitContactUsForm(TestDataComman.fName, TestDataComman.fEmail, TestDataComman.fMobile, TestDataComman.fType, TestDataComman.fMessage);

    }
    @Test()
    public void login() throws IOException, InterruptedException
    {
        UserLogin login = new UserLogin(driver);
        ReadExcel read = new ReadExcel();
        String uname = read.getCellData(1, 0);
        String passwd = read.getCellData(1, 1);
        login.doLogin(uname, passwd);

    }

    @Test()
    public void inviteFriend()
    {
        InviteFriends invitefriend = new InviteFriends(driver);
        invitefriend.inviteFriend();


    }

}

and Executing the MainSuitExecuter class from testing.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MyKart">

  <test name="Functional_Test">
  <parameter name="browser" value="chrome" />
         <classes>      

                        <class name="com.commonutils.DriverSetup"/>         
                        <class name="com.testexecuter.MainSuitExecuter"/> 

         </classes> 
  </test>

</suite>

The Problem is, For friend Invitation minimum 3 friends details(friendname and number) mandatory. So i'm using DataProvider of TestNG for that. But not getting the clear idea where i need to use in My code as i have structure as mentioned above.

I have tried it in InviteFriend.java class Like :

@Test(getData)
public void inviteFriend(String friendName, String friendMobile)
{

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", inviteFriendsLink);

    for(int i=1;i<=3;i++)
    {
        driver.findElement(By.xpath("//input[@name='lstInviteFriendsModel["+i+"].FriendName']")).sendKeys(friendName);
        driver.findElement(By.xpath("//input[@name='lstInviteFriendsModel["+i+"].FriendMobile']")).sendKeys(friendMobile);
    }
}

@DataProvider
public Object[][] getData()
{

Object[][] data = new Object[3][2];

// 1st row
data[0][0] ="A";
data[0][1] = "9442307801";

// 2nd row
data[1][0] ="B";
data[1][1] = "9887252210";

// 3rd row
data[2][0] ="C";
data[2][1] = "9925497562";

return data;
}

but no success as i have to call inviteFriend() method from MainSuitExecuter class so there i need to pass parameter.

Can anybody help me to get out from this glitch and suggest me better idea to accomplish my motive

NarendraR
  • 7,577
  • 10
  • 44
  • 82

2 Answers2

1

This how you can probably do.

  • Create a separate Friend Class. with attribute you required, i.e. Name and mobile number
  • Create the object or list of object of this friends. I think list of object would be better.
  • Rather than using data provider, in your test create this friend object and pass it some of the method in inviteFriend class.

I think this would be cleaner approach and better to understand.

Sample code

Friend.java

public class Friend {
    String name;
    String mobile;

    public Friend(String name, String mobile){
        this.name = name;
        this.mobile = mobile;
    }
}

InviteFriends.java

public class InviteFriends {

    public InviteFriends(WebDriver driver){
        PageFactory.initElements(driver, this);
    }

    public void createFriendInvitation(List<Friend> friendList){
        for (Friend friend: friendList) {
            System.out.println(friend.mobile);
            System.out.println(friend.name);
        }
    }
}

Your Test Class

public class TestClass {

    @Test
    public void testFriendInvitation(){
        WebDriver driver = new FirefoxDriver();
        List<Friend> friends = new ArrayList<Friend>();

        friends.add(new Friend("bestfriend", "11111"));
        friends.add(new Friend("newfriend", "222"));
        friends.add(new Friend("oldfriend", "33333"));

        InviteFriends inviteFriends = PageFactory.initElements(driver, InviteFriends.class);
        inviteFriends.createFriendInvitation(friends);
        driver.quit();
    }
}
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
  • Thanks Gaurang, for reply, Actually I'm looking for the dataprovider implementation in the way as i have mentioned in question. Else I have already created 2 arrays (friend name and number) in my TestData comman class and passing those in `inviteFriend()` method and all working fine but my intend to use `dataprovider ` – NarendraR May 26 '17 at 09:04
  • @Tuks DataProvider is not the correct way of dealing with this, as it will execute three test, one for each friend. creating a list of friend object and then passing it from test makes more sense as it's only single test or not multiple – Gaurang Shah May 26 '17 at 09:27
1

The code is pretty much good except one. We need a give a name to the data provider annotation and refer it as a source of input for the respectective @test method. The code below will ideally work for you,

public class MainSuitExecuter extends DriverSetup
{

@Test()
public  void submitFeedback() throws InterruptedException
{

    ContactUsPage conpage = new ContactUsPage(driver);
    conpage.submitContactUsForm(TestDataComman.fName, TestDataComman.fEmail, TestDataComman.fMobile, TestDataComman.fType, TestDataComman.fMessage);

}
@Test()
public void login() throws IOException, InterruptedException
{
    UserLogin login = new UserLogin(driver);
    ReadExcel read = new ReadExcel();
    String uname = read.getCellData(1, 0);
    String passwd = read.getCellData(1, 1);
    login.doLogin(uname, passwd);

}

@Test(dataProvider="users") //use dataprovider with name "users" as input source
public void inviteFriend(String name, String number)
{
    InviteFriends invitefriend = new InviteFriends(driver);
    invitefriend.inviteFriend(name, number);
}

@DataProvider(name = "users") //naming as users
public Object[][] getData()
{

Object[][] data = new Object[3][2];

// 1st row
data[0][0] ="A";
data[0][1] = "9442307801";

// 2nd row
data[1][0] ="B";
data[1][1] = "9887252210";

// 3rd row
data[2][0] ="C";
data[2][1] = "9925497562";

return data;
}

}

DataProvider becomes handy when we need to pass the data from excel sheet becomes handy. More details are available in the testng documentation.

http://testng.org/doc/documentation-main.html#parameters-dataproviders

Some tutorials for data provider implementation http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

I hope this helps you. Thanks.

santhosh kumar
  • 1,981
  • 1
  • 9
  • 28
  • Thanks It looking fine . But the thing is that it creating object every time for each data set which consumes memory – NarendraR May 26 '17 at 11:02
  • Yes. You are right, it creates object for every set of data. But this is the way how it works. I suggest you to pass the data from excel sheet for better Maintainability. Kindly accept my answer if this resolved your issue. – santhosh kumar May 26 '17 at 11:05