【发布时间】:2019-08-11 20:44:58
【问题描述】:
我必须在我的方法中传递这么多参数,所以请建议我如何优化这些参数。
如何优化这些参数,让我的代码看起来干净高效
//下面是代码。
public class ContactPage extends BasePage {
WebDriver driver;
@FindBy(xpath = "//*[text()='Contact Information']")
WebElement contactPageHeader;
@FindBy(xpath = "//*[@id='contactForm']/table/tbody/tr[2]/td[1]/table/tbody/tr[1]/td[2]/select")
WebElement title;
@FindBy(xpath = "//input[@id = 'first_name']")
WebElement firstName;
@FindBy(xpath = "//input[@id = 'middle_initial']")
WebElement middleName;
@FindBy(xpath = "//*[@id = 'surname']")
WebElement lastName;
@FindBy(xpath = "//input[@type='text' and @name='nickname']")
WebElement nickName;
@FindBy(xpath = "//input[@name='client_lookup']")
WebElement company;
@FindBy(xpath = "//input[@id='company_position']")
WebElement position;
@FindBy(xpath = "//input[@id='department']")
WebElement department;
@FindBy(xpath = "//input[@name='contact_lookup_sup']")
WebElement supervisor;
@FindBy(xpath = "//input[@type='text' and @name='contact_lookup_ref']")
WebElement referredBy;
@FindBy(xpath = "//input[@id='mobile']")
WebElement mobile;
@FindBy(xpath = "//input[@id='email']")
WebElement email;
@FindBy(xpath = "//input[@id='im_id']")
WebElement messengerId;
@FindBy(xpath = "//input[@id='skype_id']")
WebElement skypeId;
@FindBy(xpath = "//input[@type='text' and @name='identifier']")
WebElement identifier;
@FindBy(xpath = "//input[@type='text' and @name='address_title']")
WebElement addressTitle;
@FindBy(xpath = "//input[@id= 'tags']")
WebElement tags;
@FindBy(xpath = "//*[@id='contactForm']/table/tbody/tr[1]/td/input[2]")
WebElement save;
public ContactPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public boolean verifyContactPageHeader() {
WebDriverWait wait = new WebDriverWait(driver, Constants.DEFAULT_WAIT_TIMEOUT);
wait.until(ExpectedConditions.visibilityOf(contactPageHeader));
return contactPageHeader.isDisplayed();
}
public void createNewContact(String FirstName, String MiddleName, String LastName, String Nickname, String Company,
String Position, String Department, String Supervisor, String ReferredBy, String Mobile, String Email,
String MessengerID, String SkypeID, String Identifier, String AddressTitle, String tags,
String Description) {
}
只在上面的代码块中为我的给定代码寻求代码优化。
【问题讨论】:
-
您可以创建一个类,将这些参数存储为自己的字段。然后在 IDE 的帮助下为这个类生成
Builder以便更容易地创建对象。
标签: java oop selenium-webdriver automation parameter-passing