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()
方法,所以我需要在那里传递参数
谁能帮我摆脱这个小毛病,给我一个更好的主意来实现我的动机
# 1 楼答案
代码非常好,除了一个。我们需要为数据提供程序注释命名,并将其引用为相应的@test方法的输入源。下面的代码最适合您
当我们需要从excel工作表传递数据时,DataProvider变得很方便。testng文档中提供了更多详细信息
http://testng.org/doc/documentation-main.html#parameters-dataproviders
数据提供程序实现的一些教程 http://learn-automation.com/data-driven-framework-in-selenium-webdriver/
我希望这对你有帮助。 谢谢
# 2 楼答案
这可能是你能做到的
我认为这将是一种更干净的方法,更容易理解
示例代码
朋友。java
邀请朋友。java
您的测试课程