使用 Robot Framework 减慢测试 运行 的好方法?

Good ways to slow down a test run using Robot Framework?

所以我是 Robot Framework 的新手,现在 运行 已经多次讨论过这个问题的各种形式。本质上,我创建了一套测试,最终通过并检查我期望的东西,然后它们通过 CI/CD 管道,在专用测试 运行ners 上我遇到了一些失败。从本质上讲,这似乎是因为测试 运行ners 做事快了一点,无论是由于硬件还是 selenium 速度,这并不重要。

为了避免这些失败,我最终将测试添加到 'slow' 某些点以确保成功,基本上是膨胀它们或只是多一点保护。那么对于有经验的人来说,你是如何处理这种情况的呢?我想尽可能地坚持最佳实践,即我不仅在所有地方都使用睡眠,而且还使用隐式等待,但我仍然不知道最好的方法是什么。

这是我最近遇到的一个问题的片段:

Wait Until Page Contains Element ${STATIC_TABLE_XPATH}/somepath 10
${image_text} = Selenium2Library.Get Element Attribute ${STATIC_TABLE_XPATH}/somepath
${second_paragraph_text} = Selenium2Library.Get Text ${STATIC_TABLE_XPATH}/somepath

所以基本上尽管这些在功能上是正确的,但当 运行 参加 运行 测试时,他们有时什么也不会退缩。我认为这是因为事情发生得太快了,所以我添加了一个 Wait Until Page Contains Element,不幸的是同样的问题仍然存在。有没有最好的方法来处理这种情况,使其始终如一地通过?

我认为在预期变量值上添加等待关键字成功会非常可怕,但很高兴被证明是错误的!

尽可能避免睡眠。

隐式等待也不是可扩展的解决方案。例如,如果您正在检查元素的 "non-existence",它们可能会拖延您很长时间。

我建议您充分利用 WebDriverWaits。

我经常需要等待某些元素或页面显示,所以我写了一些这样的方法:

public void waitUntilDisplayed(By elementLocator, int timeoutInSeconds) {
    WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver, timeoutInSeconds).ignoring(StaleElementReferenceException.class);
    wait.until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {

            return elementIsDisplayed(elementLocator);
        }
    });
}

public boolean elementIsDisplayed(By elementLocator) {
    if(elementExists(elementLocator)) {
        WebElement element = driver.findElement(elementLocator);
        return element.isDisplayed();
    }
    return false;
}

public boolean elementExists(By elementLocator) {
    List<WebElement> elements = driver.findElements(elementLocator);
    if (elements.size() < 1) {
        return false;
    }

    return true;
}

drkthng 的推理是正确的,但您不需要编写所有代码...它已经存在于 ExpectedConditions 中。一个简单的例子

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));

查看所有可用方法(有很多)的文档以及如何使用它们。

尽管您觉得这很糟糕,但我认为减慢 Selenium 速度的最佳方法是保持轮询元素属性,直到获得合适的变量。在成功之前尝试某事没有坏处。如果你有一个错误,属性值将永远不会正常。我相信 Wait Until Keyword Succeeds 关键字就是为这种情况而设计的。我一直在用它。