【问题标题】:How to Optimize the parameter [closed]如何优化参数
【发布时间】: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


【解决方案1】:
  1. 创建一个 Contact 类,其中包含作为字段传递给 createNewContact() 的所有值,以及每个字段的 getter 方法
  2. 将构建器模式应用于该类,如图所示 here
  3. 重构 createNewContact() 以将 Contact 对象作为参数,并使用 getter 方法从 Contact 对象中提取值

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2015-04-28
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2011-08-25
    • 2021-04-20
    • 2016-05-20
    相关资源
    最近更新 更多