gpt4 book ai didi

java - 将 Selenium 页面对象模式与 WebDriverWait 混合使用时出现问题

转载 作者:行者123 更新时间:2023-11-30 07:06:32 24 4
gpt4 key购买 nike

我在项目中使用了selenium页面对象,并且还使用了WebDriverWait来等待元素被添加。

@FindBy(how = How.ID, using = "username")
private WebElement username;

@FindBy(how = How.ID, using = "password")
private WebElement password;

public void login(String username, String password) {
WebDriverWait waiter = new new WebDriverWait(driver, 5, 200);
waiter.until(ExpectedConditions.presenceOfElementLocated(
By.id("username")));
this.username.sendKeys(username)
}

两个问题:

  1. 因为我们只需要:

    waiter.until(ExpectedConditions.presenceOfElementLocated(
    By.id("用户名"))).sendkey(用户名);

而不是页面对象用户名来返回你想要的元素,页面对象模式没有用吗?

  • 如果页面对象模式是必需的,我该如何处理字符串“username”?我是否需要一个新类来维护常量,例如:

    public final static String USERNAME = "用户名";

  • 所以我们可以在我的页面上调用它?

    最佳答案

    "...is the page object pattern useless?"

    几乎没有!例如,您的登录方法尚未在密码字段中输入值。因此,要在没有 LoginPage.login() 方法的情况下登录,每个测试中至少需要两行长代码才能登录。

    如果您的 LoginPage.login() 方法能够识别诸如登录页面上的预期错误之类的内容,并且它可以抛出您的测试可以响应的自定义异常,则可以将附加值添加到您的 LoginPage.login() 方法中。我敢打赌,您可能需要与该登录页面上的其他内容进行交互,因此需要将其他方法添加到 LoginPage 类中。

    Do I need a new Class for maintaining constants

    我通常更喜欢将定位器的字符串保留在将使用它们的类中。因此,我将在 LoginPage 中为 USERNAME_ID 设置一个私有(private)变量,然后您可以在其他地方使用它。

    关于java - 将 Selenium 页面对象模式与 WebDriverWait 混合使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40034463/

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