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

How to increase the execution speed through Selenium and Webdriver

测试脚本执行的时候很慢,不知道什么原因。

这是我的脚本:

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

关于如何使测试更快的任何建议?

让您的 script/program 更快的一个简单步骤是:

  • 删除 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.