2015-02-11 31 views
0
package com.objects; 
import java.util.ArrayList; 
import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.How; 
import org.openqa.selenium.support.PageFactory; 
import com.tests.BaseClass; 

public class LinkedInHomePage extends BaseClass { 

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

@FindBy(how = How.ID, using = "firstName-coldRegistrationForm") 
public WebElement newFirstNameTexthBox; 

@FindBy(how = How.NAME, using = "lastName") 
public WebElement newLastNameTexthBox; 

@FindBy(how = How.ID, using = "email-coldRegistrationForm") 
public WebElement newEmailTexthBox; 

@FindBy(how = How.ID, using = "password-coldRegistrationForm") 
public WebElement newPasswordTexthBox; 

@FindBy(how = How.ID, using = "btn-submit") 
public WebElement signUpButton; 

public void EnterNewFirstName(String inComingNewFirstName) { 
    newFirstNameTexthBox.clear(); 
    newFirstNameTexthBox.sendKeys(inComingNewFirstName); 
} 

public void EnterNewLastName(String inComingNewLastName) { 
    newLastNameTexthBox.clear(); 
    newLastNameTexthBox.sendKeys(inComingNewLastName); 
} 

public void EnterNewEmail(String inComingNewEmail) { 
    newEmailTexthBox.clear(); 
    newEmailTexthBox.sendKeys(inComingNewEmail); 
} 

public void EnterNewPassword(String inComingNewPassword) { 
    newPasswordTexthBox.clear(); 
    newPasswordTexthBox.sendKeys(inComingNewPassword); 
} 

public void ClickSignUp() { 
    signUpButton.click(); 
} 

public void JoinNow(String FName, String LName, String Email, 
     String Password) { 
    EnterNewFirstName(FName); 
    EnterNewLastName(LName); 
    EnterNewEmail(Email); 
    EnterNewPassword(Password); 
    ClickSignUp(); 
} 

}使用Java数组或列表,以填补硒页面设计的页面上的文本框对象模型

上面你可以看到的JoinNow()函数有多个参数,它工作完美的罚款。我想使用数组或列表来减少参数的数量,然后使用循环来填充文本框。我想完成类似如下的操作,但由于我使用的是页面对象模型设计,因此无法使用findElement。

@SuppressWarnings("unchecked") 
protected void JoinNow(String... var) { 
    List<MyElements> inputElements = new ArrayList<MyElements>(); 
    inputElements.add((MyElements) driver.findElement(By 
      .id("firstName-coldRegistrationForm"))); 
    inputElements.add((MyElements) driver.findElement(By.id("lastName"))); 
    inputElements.add((MyElements) driver.findElement(By 
      .id("email-coldRegistrationForm"))); 
    inputElements.add((MyElements) driver.findElement(By 
      .id("password-coldRegistrationForm"))); 

    for (int i = 0; i < var.length; i++) { 
     ((WebElement) inputElements.get(i)).sendKeys(var[i]); 
    } 
} 
+0

我很困惑,其中的代码段第二届“保护无效JoinNow”所在。驱动程序的引用来自哪里? – 2015-02-11 00:14:02

+0

第二个JoinNow(String ... var)就是我在实现页面对象模型设计之前使用的东西,现在不再使用了 – user3325340 2015-02-11 00:17:31

回答

0

为什么不派MapJoinNow方法。我建议你保持你的PageObject模式。我相信这是更具可读性和更易于维护的长期

Map<String,String> data = new HashMap<String,String>(); 
data.put("firstname", "George"); 
data.put("lastname", "Clooney"); 
data.put("email", "[email protected]"); 
data.put("password", "Dontguess"); 
JoinNow(data); 

protected void JoinNow(Map<String,String> data) { 
    firstNameElement.sendKeys(data.get("firstname")); 
    lastNameElement.sendKeys(data.get("lastname")); 
    emailElement.sendKeys(data.get("email")); 
    passwordElement.sendKeys(data.get("password")); 
} 
+0

感谢您的回复。你能告诉我怎样才能声明map变量并调用JoinNow函数? – user3325340 2015-02-11 04:54:31

+1

@ nilesh。多谢兄弟。 – user3325340 2015-02-11 05:29:53

+0

很高兴帮助:) – nilesh 2015-02-11 05:31:41

0

在做页面对象模型时,您需要考虑几个项目。根据您的做法(通过使用@FindBy),有时您最终会得到过时的数据。为了获得更好的结果,你应该像下面那样定义你的id并用它们来获得你的Webelemment。这是我设计它的方式。定义所有By.id并将其放入列表中,并使用下面的函数来简化它。由

定义: static final By FNAME_REGFORM_BY_ID = By.id("firstName-coldRegistrationForm");

public void fillTextBoxes(List<By> bys, List<String> valuesToPopulate) { 
    List<WebElement> webelements = new ChromeDriver().findElements(By.cssSelector("input[type=text]")); 
    for (int i = 0; i < bys.size(); i++) { 
     new ChromeDriver().findElement(bys.get(i)).sendKeys(valuesToPopulate.get(i)); 
    } 
} 

编辑:为了更好地理解,fillTextBoxes相当于joinNow的。

List<String> values=new LiskedList<String>(); 
Values.add("emailid"); 
values.add("firstname"); 

List<By> bys=new LinkedList<By>(); 
bys.add(FNAME_REGFORM_BY_ID); 
bys.add(EMAIL_ID_By_ID); 

public void joinNow(List<By> bys, List<String> values) { 
    List<WebElement> webelements = new ChromeDriver().findElements(By.cssSelector("input[type=text]")); 
    for (int i = 0; i < bys.size(); i++) { 
     new ChromeDriver().findElement(bys.get(i)).sendKeys(valuesToPopulate.get(i)); 
    } 
} 

不要使用注释@FindBy(如何= How.ID使用=“电子邮件coldRegistrationForm”)等,因为这延缓执行以及给你过时的数据。 FindBy往往会减慢,因为只有在所有元素都可用后,整个页面对象才会完全初始化。尝试使用Static By就像我提到的。我们遇到了perf问题,并开始从注释的Findby切换。

根据要求,这里是一个示例实现。您可能需要根据您的项目需求进行调整,但这应该开始。

您的Page Class(为了我而改名为SO)。

public class SO { 

    WebDriver driver; 

    static final By FNAME_REGFORM_BY_ID = By.id("firstName-coldRegistrationForm"); 
    static final By LNAME_REGFORM_BY_NAME = By.className("lastName"); 
    static final By EMAIL_REGFORM_BY_ID = By.id("email-coldRegistrationForm"); 
    static final By PWD_REGFORM_BY_ID = By.id("password-coldRegistrationForm"); 
    static final By SUBMIT_REGFORM_BY_ID = By.id("btn-submit"); 

    public SO(WebDriver driver) { 
     this.driver = driver; 
    } 

    public List<By> getAllInput() { 
     List<By> bys = new LinkedList<By>(); 
     bys.add(FNAME_REGFORM_BY_ID); 
     bys.add(LNAME_REGFORM_BY_NAME); 
     bys.add(LNAME_REGFORM_BY_NAME); 
     bys.add(LNAME_REGFORM_BY_NAME); 
     bys.add(LNAME_REGFORM_BY_NAME); 
     return bys; 
    } 

    public void joinNow(List<String> values) { 
     PageFactory.initElements(this.driver, HotelDetailsPage.class); 
     List<By> bys = this.getAllInput(); 
     List<WebElement> webElements = this.driver.findElements(By.cssSelector("input[type=text]")); 
     for (int i = 0; i < bys.size(); i++) { 
      new ChromeDriver().findElement(bys.get(i)).sendKeys(values.get(i)); 
     } 
    } 

    public SO open(final String url) { 
     this.driver.get(url); 
     return this; 
    } 
} 

测试方法:

@Test 
public void testForm() { 
    WebDriver driver = new FirefoxDriver(); 
    SO so = new SO(driver); 
    SO soPage = so.open("your app url"); 

    List<String> values = new LinkedList<String>(); 
    values.add("firstname"); 
    values.add("lastName"); 
    values.add("emailId"); 
    values.add("password"); 
    values.add("id"); 
    soPage.joinNow(values); 
} 
+0

你能告诉我怎样使用参数从测试类中调用JoinNow函数:(List bys,列表 valuesToPopulate) – user3325340 2015-02-11 05:00:29

+0

@ minion,你能告诉我如何从TestClass调用joinNow(List bys,List values)方法吗?另外,我使用的不是Chrome浏览器的驱动程序。如果您能向我展示完整的Testclass,完整的POM类和完整的基类,我将不胜感激。 – user3325340 2015-02-11 18:01:57

+0

看看我的编辑。 – minion 2015-02-11 21:44:44

0

以下是另一种方式:

HashMap<WebElement, String> d = new HashMap<WebElement, String>(); 
d.put(newFirstNameTexthBox, "Michael"); 
d.put(newLastNameTexthBox, "Johnson"); 
d.put(newEmailTexthBox, "[email protected]"); 
d.put(newPasswordTexthBox, "Michael123$"); 
joinNow(d); 


protected void JoinNow(Map<WebElement,String> data) { 
    for (Entry<WebElement, String> objValue : data.entrySet()) 
     objValue.getKey().sendKeys(objValue.getValue()); 
} 
相关问题