我使用POM框架并按以下方式管理代码:
这是我的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);
}
}
}
这是我的Executor Class,我通过创建对象
来调用所有页面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();
}
}
并执行MainSuitExecuter
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>
问题是,对于朋友邀请最少3个朋友的详细信息(朋友姓名和号码)是强制性的。所以我正在使用DataProvider
的TestNG。但是没有明确的想法我需要在我的代码中使用,因为我有上面提到的结构。
我在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;
}
但没有成功,因为我必须从inviteFriend()
类调用MainSuitExecuter
方法,所以我需要传递参数。
任何人都可以帮助我摆脱这个小故障并建议我更好地实现我的动机
答案 0 :(得分:1)
你可以这样做。
我认为这将是更清洁的方法,更好地理解。
示例代码
<强> 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);
}
}
}
您的测试类
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();
}
}
答案 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/
我希望这会对你有所帮助。 感谢。