gpt4 book ai didi

java - 将常量 Webdriver 元素存储在枚举中

转载 作者:行者123 更新时间:2023-12-02 06:49:59 25 4
gpt4 key购买 nike

我目前将元素位置存储在远离其访问器方法的单独类中,并且我正在寻找一种更有效的存储它们的方法。从我读到的内容来看,枚举是可行的方法。我的问题是我的假设正确吗?我该如何实现?以下是它现在如何工作的一个小示例:

元素类:

public class GeneralJobElements {
protected By ProcurementWorkFlow = By.name("procurementWorkflow");
protected By JobOwner = By.name("jobOwner");
protected By JobTitle = By.name("i18n_jobTitle_minimized");

}

访问器类:

public class CreateJobGeneralTabActions extends GeneralJobElements {
public CreateJobGeneralTabActions selectJobOwner(int index) {

logMessage(JobOwner, "Job Owner");
Select sele = new Select(getWebElement(JobOwner));
sele.selectByIndex(index);

return this;
}

公共(public) CreateJobGeneralTabActions fillJobTitle() {

    logMessage(JobTitle, "Job Title");
getWebElement(JobTitle).sendKeys(JOB_TITLE);
logger.info("The job title is: " + JOB_TITLE);

return this;
}

public CreateJobGeneralTabActions selectJobApprovalWF(int index) {

logMessage(ProcurementWorkFlow, "Procurement Workflow");
Select sele = new Select(getWebElement(ProcurementWorkFlow));
sele.selectByIndex(index);

return this;
}

我不喜欢他们认为我正在扩展元素类

最佳答案

我不认为在枚举中存储定位器是一个好主意,在属性文件中存储定位器也不是一个好主意。两者都有问题。相反,我建议您使用 PageFactory它支持 PageObjects

  1. 为了调试、更新、添加元素,您需要跳转到外部属性文件或枚举。在 PageFactory 中,元素将使用 @FindBy 注释进行注释,并且它们位于页面对象中,以便您可以轻松使用它们
  2. PageFactory 将为您查找元素,从而避免 driver.findElement 或 driver.findElements 样板代码的开销
  3. PageFactory 找不到元素时,它会抛出异常,准确指出失败的位置。
  4. PageFactory 可以支持元素加载、缓存等。您无需编写代码
  5. 维护属性文件很痛苦,它们增长得很快,您可能会添加重复的键等。还有解析和读取键的开销。如果您使用 PageFactory
  6. ,这一切都可以避免

浏览器自动化是一个很难解决的问题,更不用说正在测试中不断发展的应用程序,并且需要支持不同的浏览器/平台。因此,我们应该避免添加更多层,从而增加复杂性,而应尽可能使用开源团队提供的解决方案。

关于java - 将常量 Webdriver 元素存储在枚举中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18163841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com