我(最近)使用 Protractor 来运行 e2e cucumber 测试。 我有一个基于 angularJS 的网络应用程序。我正在使用 appium 在真实的 android 设备上远程运行测试。以下是我使用的版本:
windows8.1
protractor@1.3.1 (with submodule selenium-webdriver@2.43.5)
appium@1.3.0beta1
android device with 4.4.4
我的 Protractor 配置(摘录),对应https://github.com/angular/protractor/blob/master/docs/browser-setup.md :
currentDeviceUDID = (...);
var appToTestURL = 'http://my.website.com:9000/app/index.html';
exports.config = {
seleniumAddress: 'http://localhost:4723/wd/hub';
chromeOnly: false,
specs: ['features/sample.feature'],
capabilities: {
browserName: 'chrome',
'appium-version': '1.0',
platformName: 'Android',
platformVersion: '4.4.4',
udid: currentDeviceUDID
},
baseUrl: appToTestURL
framework: 'cucumber',
cucumberOpts: {
require: 'features/stepDefinitionsSample.js',
tags: '@dev',
format: 'progress'
},
// configuring wd in onPrepare
onPrepare: function () {
var wd = require('wd'),
protractor = require('protractor'),
wdBridge = require('wd-bridge')(protractor, wd);
wdBridge.initFromProtractor(exports.config);
},
allScriptsTimeout: 30000,
getPageTimeout: 30000
};
如您所见,我已将 Protractor 的 webdriver url 替换为 appium webdriver。我使用“appium &”从命令行启动 appium,然后使用“protactor cucumbertest.conf”运行测试
手机打开 chrome 浏览器并导航到我用“browser.get(url)”给它的 url
问题如下: 异步等待网站加载和所有打开的 http 请求(据我所知)的调用 waitForAngular() 未在电话上成功执行。手机没有响应调用,webdriver 代理返回 500。
对应https://github.com/angular/protractor/issues/1358 ,我知道 waitForAngular() 函数在 Protractor 中混合到调用中
['getCurrentUrl', 'getPageSource', 'getTitle'];
在文件 protractor.js 的 waitForAngular() 后面是下面的函数,它被代理到手机:
functions.waitForAngular = function(selector, callback) {
var el = document.querySelector(selector);
try {
if (angular.getTestability) {
angular.getTestability(el).whenStable(callback);
} else {
angular.element(el).injector().get('$browser').
notifyWhenNoOutstandingRequests(callback);
}
} catch (e) {
callback(e);
}
};
附加信息:当我在 webdriver(浏览器)对象上激发错误时,错误消息指向 Protractor 目录中的 chromedriver.exe。我不明白为什么错误不是来自 appium 的 chromedriver
所以 tldr; 如果没有成功调用 waitForAngular,我无法(稳定或根本)访问手机页面上的元素,因此无法测试。也许我在这里误解了一些基本的配置细节,欢迎所有提示。
编辑:在此处添加了 appium 服务器日志:http://pastebin.com/vqBGUdXH
最佳答案
我想我已经确定了问题所在。 Appium 和 Protractor 工作正常。
我的 angularJS 应用导致了这个问题。它使用 $timeout 进行轮询(我强制使用没有 $interval 的 Angular 1.07)。这会导致 Protractor 期望页面仍处于加载阶段并且未完成。因此,函数调用 waitForAngular()
永远不会返回,并且测试会在指定的超时时间后超时。
此行为是预期和已知的,也记录在案(最好先阅读文档;))在 http://angular.github.io/protractor/#/timeouts
文档建议以下内容用于连续轮询:将 $timeout
替换为 $interval
:
If your application continuously polls
$timeout
or$http
, it will never be registered as completely loaded. You should use the$interval
service (interval.js) for anything that polls continuously (introduced in Angular 1.2rc3).
现在,我用另一种方式解决了这个问题:禁用内置 Angular 同步并手动同步
this.Before(function(next){
ptor = protractor.getInstance();
ptor.ignoreSynchronization = true; //disables waitForangular()
next();
});
同步方式一:
//at a testcase, wait for an element to be visible with a promise/then browser.wait(function () { element.all(by.css('.myCssClass')).then(function (items) { items[0].getText().then(function (text) { console.log(text); }); }); return true; }
同步方式二:
// "eventually" (chai-as-promised) internally uses "promise" (and therefore acts like "then") browser.get(url); expect(browser.getTitle()).to.eventually.equal("connect me").and.notify(next);
关于android - Protractor 的 waitForAngular() 在 angular-webapp 上失败(真实设备上的 appium/chrome),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299173/