【问题标题】:How to increase the execution speed through Selenium and Webdriver如何通过 Selenium 和 Webdriver 提高执行速度
【发布时间】:2018-08-29 09:23:42
【问题描述】:

在不知道原因的脚本执行过程中测试很慢。

这是我的脚本:

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

有什么建议可以加快测试速度吗?

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

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

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

    根据Explicit and Implicit Waits的文档:

    警告:不要混合隐式和显式等待。这样做会导致不可预测的等待时间。例如,设置 10 秒的隐式等待和 15 秒的显式等待,可能会导致 20 秒后发生超时。

    【讨论】:

    • 这正是正在发生的事情,但我首先需要隐式超时,因为网站需要时间来加载并且给我一个错误。至于显式等待我不知道如何解决这个问题,因为我有一个加载器,这个加载器掩盖了另一个元素。所以你建议我做什么,而不是重复显式等待和隐式等待
    • ImplicitWait 将很快死于火灾。不要再依赖ImplicitWait。使用量身定制的Explicit wait 满足您的所有需求。 Loader 可以用更简单的方法解决。
    • 这是我的加载器方法不可见:wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='loader-wrapper ng-trigger ng-trigger -visibilityChanged ng-animating']")));我在多个页面中有一个加载器,所以我需要添加多个上述方法
    • 也许您为最重要的问题Element is not clickable another element receive the click- Selenium c# 实施第一个可用解决方案还为时过早。您需要稍等片刻,以便贡献者为您的问题构建好的答案。
    • 好的,我刚刚在那里重新接受了答案。但仍然需要更好的解决方案。正如你在这里提到的,我可以通过使用显式而不是隐式来避免测试变慢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多