javascript - angularjs 中有类似 jQuery.active 的东西吗?

标签 javascript angularjs selenium-webdriver

我正在使用 selenium 来测试我的应用程序。我有很多使用 $resource 或 $http 的 ajax 调用。如果有一种方法可以用 Angular 轮询任何事件的 ajax 请求,这样 selenium 就可以等到这些请求完成,那就太好了。

我想我可以在页面上放置一个元素(供 selenium 查找)并将它连接到一些成功设置的标志,但这可能会变得非常困惑。

按照here.所述使用jQuery 时,有一种非常有效的方法可以做到这一点

或者 selenium 是否有一种我没有找到的方法可以做到这一点?

在文档中找不到任何内容?有什么建议么?谢谢。

编辑: Caleb Boyd 的回答是正确的,并且是在使用 selenium 网络驱动程序时检测 Angular ajax 调用问题的一个很好的解决方案。这是我如何使用它的快速实现。我实际上使用了来自这个 link 的 caleb 代码的变体。 ,其中包括 ajax 错误。然而,本质上是一样的。谢谢迦勒。

将此脚本和元素添加到页面底部。在部署前删除:

<html>
<head><!--My Angular Scripts--></head>
<body ng-app="MyApp">
<!--Your Html -->
<script>
            MyApp.config(function($httpProvider) {
                $httpProvider.interceptors.push('requestInterceptor');
            })
            .factory('requestInterceptor', function($q, $rootScope) {
                $rootScope.pendingRequests = 0;
                return {
                    'request': function(config) {
                        $rootScope.pendingRequests++;
                        return config || $q.when(config);
                    },
                    'requestError': function(rejection) {
                        $rootScope.pendingRequests--;
                        return $q.reject(rejection);
                    },
                    'response': function(response) {
                        $rootScope.pendingRequests--;
                        return response || $q.when(response);
                    },
                    'responseError': function(rejection) {
                        $rootScope.pendingRequests--;
                        return $q.reject(rejection);
                    }
                };
            });
    </script>
    <span id="http-status">{{pendingRequests}}</span>
</body>
</html>

我使用 NUnit 作为我的测试框架。

[TestFixture]
public class MyTestClass
{
  [Setup}
  public void Setup()
  {
    _webDriver = new ChromeDriver(@"...path to chromedriver.exe")
    //any other properties you need
   }

   [TearDown]
   public void TearDown()
   {
     if(_webDriver == null)
        return;
     _webDriver.Quit();
     _webDriver.Dispose();
    }

    [Test]
    public void Test_my_page_functionality()
    {
      var pageBtn = _webDriver.FindElement(By.Id("my-btn-id"));
      pageBtn.Click();
      _webDriver.WaitForAjax();//see extension below
      //test whatever result you want after ajax request has come back
     }
}

这是 WaitForAjax 扩展

public static class WebDriverExtensions
{
  public static void WaitForAjax(this IWebDriver webDriver)
        {
            while (true)
            {
                //Note: FindElement is another extension that uses a timer to look for an element
                //It is NOT the one that selenium uses - the selenium extension throws exceptions on a null element
                var ajaxIsComplete = webDriver.FindElement(By.Id("http-status"), 5);
                if (ajaxIsComplete != null && ajaxIsComplete.Text.Equals("0"))
                {
                    //optional wait for angularjs digest or compile if data is returned in ajax call
                    Thread.Sleep(1000);
                    break;
                }
                Thread.Sleep(100);
            }
        }
}

尝试通过将 Thread.Sleep(5000) 放在 Controller 方法的底部来测试 WaitForAjax 扩展。 希望这对某人有帮助。再次感谢 Caleb。

最佳答案

是的,我相信您可以在 Angular 上使用回调。我已经在一个大型项目中使用它来使用 Ruby 进行测试自动化。下面是调用。我们正在等待 30 秒,直到挂起的 Requests.length 变为 0。字符串“0”,因为返回值始终是字符串。

Watir::Wait.until(30) {
@browser.execute_script("return angular.element(document.body).injector().get(\'$http\').pendingRequests.length;") == "0"
}

关于javascript - angularjs 中有类似 jQuery.active 的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21947877/

相关文章:

javascript - 更新的 Angular 版本导致 ngMessages 出现错误

python - EC.element_to_be_clickable 和 EC.presence_of_element_located to click() 一个元素的区别

JavaScript 拆分后视

javascript - 如何并排显示图像

javascript - AngularJS 多选使用 ng-repeat

javascript - Selenium Webdriver 不执行 JavaScript

xpath - Selenium - @FindBy 和 WebElement.findElement() 之间的区别

javascript - 在 React 全局组件(例如 AppRoot 中的 snackbar )中使用 CustomEvent 是一个坏主意吗?

javascript - 我在使用 Node js 对 dynamoDB 中的表执行更新操作时遇到 ConditionalCheckFailedException

javascript - c# 转换为 html 时停止“转换为”