【发布时间】:2014-02-22 00:27:53
【问题描述】:
我正在使用 selenium 来测试我的应用程序。我有很多使用 $resource 或 $http 的 ajax 调用。如果有办法以 angular 轮询任何活动的 ajax 请求,这样 selenium 可以等到这些请求完成,那就太好了。
我想我可以在页面上放置一个元素(供 selenium 查找)并将其连接到一些设置成功但可能会变得非常混乱的标志。
如here. 所述,使用 jQuery 时有一种非常有效的方法
或者 selenium 有没有办法做到这一点,我还没有找到?
在文档中找不到任何内容?有什么建议么?谢谢。
编辑: Caleb Boyd 的回答是正确的,并且是在使用 selenium web 驱动程序时检测角度 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) 放在控制器方法的底部来测试 WaitForAjax 扩展。 希望这可以帮助某人。再次感谢 Caleb。
【问题讨论】:
标签: javascript angularjs selenium-webdriver