有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何通过在testNG中创建对象来调用测试方法

我正在使用POM框架,并以以下方式管理代码:

这是我的朋友页

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

}

这是我的Executor类,通过创建对象从中调用所有页面

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


    }

}

以及从testing.xml执行MainSuitExecuter

<?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>

问题是,对于好友邀请,必须提供至少3个好友详细信息(好友姓名和号码)。所以我使用TestNG的DataProvider来实现这一点。但是我没有弄清楚我需要在我的代码中使用什么,因为我有上面提到的结构

我在InviteFriend.java课上试过,比如:

@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;
}

但是没有成功,因为我必须从MainSuitExecuter类调用inviteFriend()方法,所以我需要在那里传递参数

谁能帮我摆脱这个小毛病,给我一个更好的主意来实现我的动机


共 (2) 个答案

  1. # 1 楼答案

    代码非常好,除了一个。我们需要为数据提供程序注释命名,并将其引用为相应的@test方法的输入源。下面的代码最适合您

    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;
    }
    
    }
    

    当我们需要从excel工作表传递数据时,DataProvider变得很方便。testng文档中提供了更多详细信息

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

    数据提供程序实现的一些教程 http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

    我希望这对你有帮助。 谢谢

  2. # 2 楼答案

    这可能是你能做到的

    • 创建一个单独的朋友类。具有您所需的属性,即姓名和手机号码
    • 创建此好友的对象或对象列表。我认为物品清单会更好
    • 在测试中,不要使用数据提供程序,而是创建这个friend对象,并在inviteFriend类中向其传递一些方法

    我认为这将是一种更干净的方法,更容易理解

    示例代码

    朋友。java

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

    邀请朋友。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);
            }
        }
    }
    

    您的测试课程

    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();
        }
    }