【发布时间】:2021-02-21 16:38:46
【问题描述】:
我正在为一个简单的 Web 应用程序编写自动化 UI 测试:
应用非常简单:只有服务器端逻辑,没有客户端脚本,没有 AJAX,没有客户端框架。
这是我的测试场景:
- 使用 Selenium 打开应用主页: https://shorturl.nakov.repl.co
- 点击链接[添加网址]导航至:https://shorturl.nakov.repl.co/add-url
- 在https://shorturl.nakov.repl.co/add-url填写表格并提交
- 提交后,断言新元素已添加并在主页可见 (https://shorturl.nakov.repl.co)
除非我使用 dirty hack Thread.Sleep(500),否则表单元素的 SendKeys() 和 Click() 命令 fail 并且表单保持为空且未提交。仅当表单由链接导航加载时才会发生这种情况。如果我通过 URL 打开它,它可以工作。
我尝试了很多等待和修复,但没有任何帮助,除了Thread.Sleep()。
这个问题看起来像 Chromium 中的错误,因为相同的逻辑 在 Firefox 中可以正常工作,但在 Opera 中却以同样的方式失败和边缘。
这是失败的 Selenium ChromeDriver 测试:
[Test]
public void Test_AddUrl_Chrome()
{
// Setup the Chrome Web Driver
using (var driver = new ChromeDriver())
{
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
// Open the Web app and navigate to the "Add URL" page
driver.Navigate().GoToUrl("https://shorturl.nakov.repl.co/");
var addUrlLink = driver.FindElementByXPath("//header/a[text()='Add URL']");
addUrlLink.Click();
// Wait until the page "Add Short URL" is loaded --> does not help
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(d => d.FindElement(By.XPath("//main/h1[text()='Add Short URL']")));
// Wait until the form [submit] button becomes clickable --> does not help
wait.Until(ExpectedConditions.ElementToBeClickable(
By.CssSelector("form button")));
// This wait works, but is а bad practice
//Thread.Sleep(500);
// Fill the form and submit it
var newUrl = "http://newurl" + DateTime.Now.Ticks + ".com";
var textBoxUrl = driver.FindElementByCssSelector("input.url");
// SendKeys() does not change the element value (unless I use Thread.Sleep())
textBoxUrl.SendKeys(newUrl);
var buttonCreate = driver.FindElementByCssSelector("form button");
// Click() does not submit the form (unless I use Thread.Sleep())
buttonCreate.Click();
// Assert the "Short URLs" page holds the new URL
var newUrlLink = driver.FindElementByLinkText(newUrl);
Assert.AreEqual(newUrl + "/", newUrlLink.GetAttribute("href"));
}
}
驱动程序日志显示执行成功。没有错误,但 SendKeys() 和 Click() 静默失败,没有错误。表单字段保持空白,未单击提交按钮。
您可以在您的机器上运行测试以确保它失败。它没有依赖关系,并且目标 Web 应用程序在 Web 上运行。
如果您在页面导航后取消注释Thread.Sleep(500),则测试成功。
我使用最新的 Chrome 88.0.4324.182(64 位,在 Windows 10 上)及其对应的 64 位 chromedriver.exe(相同版本)。
同样的测试在 FirefoxDriver 上运行良好:
[Test]
public void Test_AddUrl_Firefox()
{
// Setup the Firefox Web Driver (use ::1 for faster execution)
var ffService = FirefoxDriverService.CreateDefaultService();
ffService.Host = "::1";
using (var driver = new FirefoxDriver(ffService))
{
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
// Open the Web app and navigate to the "Add URL" page
driver.Navigate().GoToUrl("https://shorturl.nakov.repl.co/");
var addUrlLink = driver.FindElementByXPath("//header/a[text()='Add URL']");
addUrlLink.Click();
// Fill the form and submit it
var newUrl = "http://newurl" + DateTime.Now.Ticks + ".com";
var textBoxUrl = driver.FindElementByCssSelector("input.url");
textBoxUrl.SendKeys(newUrl);
var buttonCreate = driver.FindElementByCssSelector("form button");
buttonCreate.Click();
// Assert the "Short URLs" page holds the new URL
var newUrlLink = driver.FindElementByLinkText(newUrl);
Assert.AreEqual(newUrl + "/", newUrlLink.GetAttribute("href"));
}
}
这是 Chromium Web 驱动程序中的错误,还是我做错了错误?
【问题讨论】:
-
我不认为这是个问题,但是
<title>标记存在于<head>和<body>之外作为<html>标记的子级.<label>标签具有for属性,但输入上没有对应的id属性。只是出于好奇,修复 HTML 语法错误会修复 ChromeDriver 的问题吗?如有疑问,请先检查最简单的问题。 -
感谢您的建议。我修复了该应用程序,使其所有页面都包含有效的 HTML5。这并不能解决问题。还有其他建议吗?
-
这些是 ChromeDriver 日志:bugs.chromium.org/p/chromedriver/issues/…
标签: c# selenium selenium-webdriver selenium-chromedriver