小编典典

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

selenium

在不知道原因的情况下,脚本执行期间的测试速度非常慢。

这是我的脚本:

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);
}

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


阅读 1074

收藏
2020-06-26

共1个答案

小编典典

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

  • 删除所有 ImplicitWait 实例为:
    • 您正在广泛使用 WebDriverWait显式等待

根据
显式和隐式等待
的文档:

警告
:请勿混合使用隐式和显式等待。这样做可能导致无法预测的等待时间。例如,将隐式等待设置为10秒,将显式等待设置为15秒,则可能导致20秒后发生超时。

2020-06-26