如何在 Blazor 应用程序的 Selenium / Puppeteer 测试中等待导航完成

问题描述

我有一个带有标准路由器的 Blazor 服务器应用程序。

我想编写一个 Selenium UI 自动化,导航到特定的 URL 并等待页面呈现。

现在我只检查特定于该页面的元素并等待它显示

_driver.Navigate().GoToUrl("/"); //initial load
...
_driver.Navigate().GoToUrl("/counter"); //navigate async
var wait = new webdriverwait(_driver,TimeSpan.FromSeconds(3));
wait.Until(d => d.FindElementsByCssSelector(...).Count == 1)

是否有更好、更通用的方法来查找页面上的预期元素?

解决方法

您可以使用其他 ExpectedConditions,例如 ElementIsVisible() 或 ElementToBeClickable()

new WebDriverWait(driver,TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("your selector")));

new WebDriverWait(driver,TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("your selector")));