我正在使用 POM 框架并按以下方式管理代码:
这是我的邀请 friend 页面
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);
}
}
}
这是我的执行器类,我通过创建对象来调用所有页面
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()
方法,所以我需要传递参数。
任何人都可以帮助我摆脱这个故障,并建议我更好的想法来实现我的动机
最佳答案
这可能是你可以做的。
- 创建一个单独的 Friend 类。包含您所需的属性,即姓名和手机号码
- 创建该好友的对象或对象列表。我认为对象列表会更好。
- 在测试中创建此好友对象并向其传递 inviteFriend 类中的一些方法,而不是使用数据提供程序。
我认为这将是更清晰的方法并且更好地理解。
示例代码
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);
}
}
}
您的测试类(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();
}
}
关于java - 如何通过在testNG中创建对象来调用测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195465/