首页 > 解决方案 > 如何优化参数

问题描述

我必须在我的方法中传递这么多参数,所以请建议我如何优化这些参数。

如何优化这些参数,使我的代码看起来干净高效

// 下面是代码。

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) {

}

仅在上面的块中为我的给定代码寻求代码优化。

标签: javaoopselenium-webdriverautomationparameter-passing

解决方案


  1. 创建一个 Contact 类,将您传递给 createNewContact() 的所有值作为字段,并为每个字段创建 getter 方法
  2. 将构建器模式应用于该类,如此 处所示
  3. 重构 createNewContact() 以将 Contact 对象作为参数,并使用 getter 方法从 Contact 对象中提取值

推荐阅读