Selenium C#中的显式等待不起作用.怎么了? [英] Explicit waits in Selenium C# doesn't work . What is wrong?

查看:429
本文介绍了Selenium C#中的显式等待不起作用.怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在显式等待时遇到了这个问题.我不想使用Thread.Sleep().这是一个简单的测试,它将打开一个页面,然后前进和后退.加载此页面大约需要2-3秒,而我想以一种动态方式(测试)来完成.希望我不要太困惑.我做了很多研究,但没有任何效果,也许我做错了什么. (我正在使用Resharper进行单元测试)

So I have this issue with explicit waits. I don't want to use Thread.Sleep(). This is an simple test which it opens a page and then goes back and forward. It takes about 2-3 seconds to load this page and I want to do this in a dynamic way (testing). Hope I am not too confusig. I did a lot of research but nothing works, maybe I am doing something wrong. ( I'm using Resharper to run unit tests)

这里我也有解决方法:

https://www.dropbox.com/s/1k5vjc5czvmdd3u/ConsoleApplication2.rar?dl=0

我正在使用FindElement方法的扩展,因此我相信仅调用此方法并自行等待将很容易.我在解决方案中评论了一些解释.如果有人给我一些帮助,我将不胜感激. (对不起,英语不是那么完美).

I am using an extension to FindElement method so I believe it would be easy to just call this method and wait by itself. I have some explanation commented in the solution. I would appreciate if somebody give me some help. (Sorry for not so perfect english).

using System;
using System.Threading;
using ConsoleApplication2.Extensions;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2
{
    class UnitTest1
    {
        private IWebDriver driver = new ChromeDriver();
        [SetUp]
        public void Initialize()
        { 
            driver.Url = "some url";
            Thread.Sleep(3500);
           // driver.Manage().Timeouts().ImplicitlyWait(TimeSpan(20));

        }

       [Test]
        public void CheckBackForward()
        {
            //Go to first page in Online Help
            driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();
            // So if I am commenting this Thread.Sleep
            // it will throw an exception at the extension method at line 13 at "@by"
            // I've also checked this without extension method but still the same problem. It doesn'w wait at all, it will throw this 
            // exception as soon as FindElement method is called.
            // I know that I shouldn't mix explicit waits with implicit ones.
            //Thread.Sleep(1500);
            //Store title
            var title_text = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span"),60).Text;
            //Check if Back is enabled
            Assert.IsTrue(driver.FindElement(By.XPath("//div[3]/a/span")).Enabled);
            //Go back
            driver.FindElement(By.XPath("//div[3]/a/span")).Click();
            //Check if Forward is enabled
            Assert.IsTrue(driver.FindElement(By.XPath("//div[4]/a/span")).Enabled);
            //Go forward
            driver.FindElement(By.XPath("//div[4]/a/span")).Click();
            //Store title
            var title_text2 = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span")).Text;
            //Check if you are on the same page
            Assert.AreEqual(title_text2,title_text);
        }



        [TearDown]
        public void EndTest()
        {
            driver.Quit();
        }

    }
}

这是扩展名:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2.Extensions
{
    public static class Extension
    {
        public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            if (timeoutInSeconds <= 0) return driver.FindElement(@by);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => drv.FindElement(@by));
        }



    }
}

推荐答案

我正在使用Selenium进行编码6个月以上,并且遇到了与您相同的问题.我已经创建了这种扩展方法,并且每次都对我有用.

I am coding with Selenium for 6+ months and I had the same problem as yours. I have created this extension method and it works for me every time.

代码的作用是: 在20秒内,它将每500ms进行一次检查,以确定页面上是否存在该元素.如果20秒后未找到,它将引发异常. 这将帮助您进行动态等待.

What the code does is: During 20 seconds, it checks each 500ms, whether or not the element is present on the page. If after 20 seconds, it's not found, it will throw an exception. This will help you make a dynamic wait.

  public static class SeleniumExtensionMethods {
      public static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
      public static void SafeClick(this IWebElement webElement) {
          try {
              wait.Until(ExpectedConditions.ElementToBeClickable(webElement)).Click();
          } catch (TargetInvocationException ex) {
              Console.WriteLine(ex.InnerException);
          }

      }

,然后替换您的代码:

driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();

具有:

IWebElement x = driver.FindElement(By.XPath("//span/ul/li/span/a"));
x.SafeClick();

这篇关于Selenium C#中的显式等待不起作用.怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆