wait.until似乎没有正确等待

时间:2018-03-15 11:48:42

标签: c# selenium selenium-webdriver

我有一个测试,删除表中的特定条目。页面必须加载,然后在可单击后导航到additionalGroupRecharges选项卡。但是,间歇性地,驱动程序在可见之前尝试单击additionalGroupRecharges选项卡,并且" {"元素无法滚动到视图中"}"抛出异常。

我已宣布等待 -

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 30, 0));
//WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));); (Same Issue)

用户登录后,驱动程序应该等到additionalGroupRecharges选项卡变为可点击,然后点击它,就像这样 -

string additionalGroupRecharges = "//*[@id='mvcPageControlFinanceData_T2']";
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(additionalGroupRecharges))).Click();

我知道选项卡的XPath是正确的,因为这有时会起作用,但间歇性地尝试在可见之前单击该按钮。

此特定测试的完整代码(然后在TestMethod中调用) -

public static string deleteRecharge(string newReportingName)
    {
        //Opens Firefox and maximizes the browser.
        IWebDriver driver = new FirefoxDriver();
        driver.Manage().Window.Maximize();

        //Allows webdriver to execute JavaScript and wait for conditions.
        var javascriptExecutor = driver as IJavaScriptExecutor;
        //WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 30, 0));
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

        //Buttons
        string deleteButton = "//a[child::img[@title='Delete'] and parent::td[following-sibling::td[text()='" + newReportingName + "']]]";
        string additionalGroupRecharges = "//*[@id='mvcPageControlFinanceData_T2']";

        //Fields
        string firstRow = "//*[@id='additionalGroupRechargeGrid_DXDataRow0']";

        //Filters
        string productNameFilter = "//*[@id='additionalGroupRechargeGrid_DXFREditorcol4_I']";

        try
        {
            //Go to the URL and log in, switch to group recharges tab.
            SeleniumHelper.LogIn(driver, "FinanceData/");
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(additionalGroupRecharges))).Click();

            //Search using filters
            wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(productNameFilter)));
            driver.FindElement(By.XPath(productNameFilter)).SendKeys(newReportingName + Keys.Enter);

            string rowData = driver.FindElement(By.XPath(firstRow)).Text;

            //Delete the record containing the newReportingName and assert it is no longer visible.
            driver.FindElement(By.XPath(deleteButton)).Click();

            //Click the delete button of the new recharge and assert it is no longer visible.
            Assert.IsFalse(driver.PageSource.Contains(rowData));

            return newReportingName;
        }

        catch (Exception ex)
        {
            //Add 1 to number of fails and store the exception.
            TestSetup.Errors.Add(ex.ToString());
            SeleniumHelper.FailCount = SeleniumHelper.FailCount + 1;
            Console.WriteLine(ex);
            return null;
        }

        finally
        {
            //Go to log out URL.
            driver.Navigate().GoToUrl(SeleniumHelper.BaseUrl + "/Account/Logout.aspx");

            //Close driver.
            driver.Close();
        }
    }

正在测试的网页 - additionalGroupRecharges

2 个答案:

答案 0 :(得分:0)

错误说明了一切:

{"Element could not be scrolled into view"}

由于您的脚本无法元素滚动到视图中,因此您需要将 WebElement WebDriverWait引导为visible,然后 scrollIntoView ,然后再次将 WebDriverWait WebElement 引入clickabe,如下所示:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 30, 0));
IWebElement additionalGroupRecharges = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='mvcPageControlFinanceData_T2']")));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);",additionalGroupRecharges);
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='mvcPageControlFinanceData_T2']"))).Click();

答案 1 :(得分:0)

通过添加此功能来实现它 -

    public static void actionwait(Func<bool> action)
    {
        int seconds = 60;

        for (int i = 0; i < seconds * 5; i++)
        {
            try
            {
                if (action())
                {
                    break;
                }
            }
            catch
            {

            }
            Thread.Sleep(200);
        }
    }

然后这样称呼它 -

            var element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(additionalGroupRecharges)));
            SeleniumHelper.actionwait(() => { element.Click(); return true; });