等待一个Ajax调用与Selenium 2networking驱动程序完成

我使用selenium2 Web驱动程序来testing使用AJAX的用户界面。

有没有办法使驱动程序等待一点,这样的Ajax请求将完成

基本上我有这个:

d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click(); // this^ click triggers an ajax request which will fill the below Id with content // so I need to make it wait for a bit Assert.IsNotEmpty(d.FindElement(By.Id("Hobbies")).Text); 
 var wait = new WebDriverWait(d, TimeSpan.FromSeconds(5)); var element = wait.Until(driver => driver.FindElement(By.Id("Hobbies"))); 

如果你使用jQuery来处理你的ajax请求,你可以等到jQuery.active属性为零。 其他图书馆可能有类似的select。

 public void WaitForAjax() { while (true) // Handle timeout somewhere { var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"); if (ajaxIsComplete) break; Thread.Sleep(100); } } 

你也可以在这里使用Selenium等待。 那么你不需要自己处理超时

 public void WaitForAjax() { var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0")); } 

Java解决scheme基于Morten Christiansens的答案

    公共无效WaitForAjax()抛出InterruptedException
     {

        而(真)
         {

            布尔ajaxIsComplete =(布尔)((JavascriptExecutor)驱动程序).executeScript(“return jQuery.active == 0”);
             if(ajaxIsComplete){
                打破;
             }
            了Thread.sleep(100);
         }
     }


通过添加一个超时参数只是一点点改善:

 internal static void WaitForAllAjaxCalls(this ISelenium selenium, IWebDriver driver, int timeout = 40) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { if (sw.Elapsed.Seconds > timeout) throw new Exception("Timeout"); var ajaxIsComplete = (bool)driver.ExecuteScript("return jQuery.active == 0"); if (ajaxIsComplete) break; Thread.Sleep(100); } } 

这是我的代码:

 public static void WaitForCommission (WebDriver driver) throws Exception { for (int second = 0;; second++) { if (second >= 30) fail("timeout"); try { if (IsElementActive(By.id("transferPurposeDDL"), driver)) break; } catch (Exception e) {} Thread.sleep(1000); } } private static boolean IsElementActive(By id, WebDriver driver) { WebElement we = driver.findElement(id); if(we.isEnabled()) return true; return false; } 

这段代码真的很有用。

 Just small improvement //Wait for Ajax call to complete public void WaitForAjax1() throws InterruptedException { while (true) { if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){ break; } Thread.sleep(100); } } 

如果你正在使用石墨烯,你可以使用这个:

 Graphene.waitModel().until((Predicate<WebDriver>) input -> (Boolean) ((JavascriptExecutor) input).executeScript("return jQuery.active == 0")); 

“XML Http Request”是用于向服务器发送Ajax请求的协议,因此这种请求的存在表示正在进行基于Ajax的操作。

有许多浏览器插件允许您监视浏览器发送的XML Http请求。 我个人使用Firefox的Firebug插件,这是一个非常有用的工具。 安装完成后,Firebug在浏览器窗口的右下angular显示一个类似Bug的图标。 点击错误图标启动Firebug,如上图所示。 select“Net”,然后select“XHR”来启动XHR控制台,浏览器发送的所有XML HTTP请求将被显示。

尽可能避免使用thread.sleep()。 这是一段接受等待时间作为input的代码,并在指定的时间运行秒表。

您可以将input时间设置为30秒开始。

 protected void WaitForAjaxToComplete(int timeoutSecs) { var stopWatch = new Stopwatch(); try { while (stopWatch.Elapsed.TotalSeconds < timeoutSecs) { var ajaxIsComplete = (bool)(WebDriver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"); if (ajaxIsComplete) { break; } } } //Exception Handling catch (Exception ex) { stopWatch.Stop(); throw ex; } stopWatch.Stop(); } 
Interesting Posts