c# - 如何通过Selenium和Webdriver提高执行速度

标签 c# selenium selenium-webdriver webdriver webdriverwait

在不知道原因的情况下,执行脚本时测试很慢。

这是我的脚本:

driver.Navigate().GoToUrl(url);       
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
driver.FindElement(By.LinkText("Register Here")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(
    SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(
        (By.XPath("//div[@class='loader-wrapper    ng-trigger ng-trigger-visibilityChanged ng-animating']"))));
driver.FindElement(By.XPath("(.//*[normalize-space(text()) and    normalize-space(.)='Organization    Type'])[2]/following::select[1]")).Click();
new SelectElement(driver.FindElement(By.XPath("(.//*[normalize-space(text())    and normalize-space(.)='Organization    Type'])[2]/following::select[1]"))).SelectByText("Hospital");
driver.FindElement(By.XPath("(.//*[normalize-space(text()) and    normalize-space(.)='Organization    Type'])[2]/following::button[1]")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(
    SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(
        (By.XPath("//div[@class='loader-wrapper    ng-trigger ng-trigger-visibilityChanged ng-animating']"))));
driver.FindElement(By.XPath("(.//*[normalize-space(text()) and    normalize-space(.)='Phone    Number'])[1]/following::button[1]")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(
    SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(
        (By.XPath("//div[@class='loader-wrapper    ng-trigger ng-trigger-visibilityChanged ng-animating']"))));

try
{
    Assert.AreEqual("Title is Required.", driver.FindElement(By.XPath("(.//*[normalize-space(text()) and    normalize-space(.)='Title'])[1]/following::span[1]")).Text);
}
catch (Exception e)
{
    verificationErrors.Append(e.Message);
}

有什么建议可以让测试更快吗?

最佳答案

让您的脚本/程序更快的一个简单步骤是:

  • 删除 ImplicitWait 的所有实例:
    • 您正在大量使用 WebDriverWait,即 Explicit Wait

根据 Explicit and Implicit Waits 的文档:

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

关于c# - 如何通过Selenium和Webdriver提高执行速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52073952/

相关文章:

c# - WebHook 实现示例?

c# - 为什么 Synchronized 方法总是返回 false?

c# - Selenium C# 的第二个测试方法失败

python - 如何在Windows机器上的python中使用tor和selenium-webdriver?

C# WinForm - 加载屏幕

c# - 哪个是异常处理的更好做法?

java - 如何使用 xpath 和 showmodal 单击元素

javascript - 如果按钮已启用,如何检查 Protractor javascript(在 Shadow DOM 中)?

java - 如何处理 selenium 脚本来验证使用 selenium webdriver 需要 20 分钟的报告的完成情况?

java - WebDriver 在一定时间后关闭弹出窗口