遵循使用c#在维基百科上测试搜索功能的教程。我的测试一直失败,因为我尝试返回的h1元素中的文本保持返回为空。 h1标题内部肯定有文字。知道为什么这个元素在有文本的时候返回空?
IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
driver.Navigate().GoToUrl("https://en.wikipedia.org/wiki/Main_Page");
IWebElement searchInput = driver.FindElement(By.Id("searchInput"));
searchInput.SendKeys("Christiaan Barnard");
searchInput.SendKeys(Keys.Enter);
IWebElement firstHeading = driver.FindElement(By.Id("firstHeading"));
Assert.AreEqual("Christiaan Barnard", firstHeading.Text);
driver.Quit();
答案 0 :(得分:1)
可能是因为找到了元素,但还没有预期的值。最好的方法是使用WebDriverWait:
等待文本具有预期值var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
var result = wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.Id("firstHeading"), "Christiaan Barnard"));
Assert.IsTrue(result);