如何等待网页上的WebElement,在执行测试时有时会出现在随机位置?

时间:2019-05-23 07:41:31

标签: java selenium dom junit webdriverwait

我为一个Web应用程序编写了一个基于硒的自动化测试,它们可以在快速的Internet连接下完美运行,但是在连接不良的情况下行为无法预测。

Web应用程序的构建方式是,如果对网页上某项操作的请求<的响应持续时间大于250ms,则使用loader-wrapper元素,该元素将阻止用户采取任何行动,直到响应结束。 Loader-wrapper可以在测试执行的任何位置针对任何请求进行包装,因此我无法使用硒的显式等待,因为我不知道它将在何时何地出现。结果我收到一个例外:

org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click:(.show-component .loader-wrapper)

是否有任何方法可以设置“全局等待”,如果装入了包装程序,它将停止测试执行,并等待直到结束,然后测试执行才能继续?或其他任何想法。

3 个答案:

答案 0 :(得分:1)

我有点喜欢您对注释的想法,但是不确定如何实现它。

另一种可能的方法是编写自己的ExpectedCondition“ loaderWrapperDisappeared”(或类似的东西),该方法将等待加载程序包装器消失,然后返回目标WebElement,以便可以将单击链接到它。

然后您将像这样使用它;

(new WebDriverWait(targetWebElement, 50))
   .until(ExpectedConditions.loaderWrapperDisappeared(By.id("your div id"))).click();

(请原谅语法错误,这几年来我还没有写Java语言)

答案 1 :(得分:0)

如果使用Web驱动程序,则必须像这样使用。

WebElement webElement = (new WebDriverWait(driver, 50))
   .until(ExpectedConditions.elementToBeClickable(By.id("your div id")));

此处50表示50秒。

有关更多详细信息,请参阅下面的链接。 https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html#WebDriverWait-org.openqa.selenium.WebDriver-long-

答案 2 :(得分:0)

如果我正确理解,您正在寻找invisibilityOfElementLocated

您可以将其作为装饰器添加到您的步骤中...

希望这会有所帮助!