当我用angular2中的黄瓜+量角器编写功能测试时,我遇到了一些问题 这是我的代码
cucumberCong.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
framework: 'custom',
frameworkPath: '../node_modules/protractor-cucumber-framework/index.js',
// Spec patterns are relative to this directory.
specs: [
'spec/**/*.feature'
],
capabilities: {
'browserName': 'chrome',
'version': 'ANY'
},
baseUrl: 'http://' + (process.env.HTTP_HOST || 'localhost') + ':' + (process.env.HTTP_PORT || webServerDefaultPort),
cucumberOpts: {
require: 'spec/**/*.js',
tags: '@dev',
format: undefined,
profile: false,
'no-source': true
}
};
login.feature
Feature: Login
@dev
Scenario: Login funtion
Given go login page "http://localhost:8080/#/login"
Then input userName "username", password "password"
Then click login
Then see About page "http://localhost:8080/#/home"
loginSpec.ts
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var HttpBackend = require('http-backend-proxy');
var proxy = new HttpBackend(browser);
module.exports = function loginPage() {
var expect = chai.expect;
this.setDefaultTimeout(500 * 1000);
this.Given(/^go login page "([^"]*)"$/, function (url, next) {
browser.driver.get(url);
next();
});
this.Then(/^input userName "([^"]*)", password "([^"]*)"$/, function (userName, password, next) {
browser.driver.findElement(by.id('userName')).sendKeys(userName);
browser.driver.findElement(by.id('pass')).sendKeys(password);
next();
});
this.Then(/^click login$/, function (next) {
proxy.whenGET('http://localhost:3000/login').respond(function(method, url) {
return [200, {"data": "test"}];
});
browser.driver.findElement(by.id('login')).click();
next();
});
this.Then(/^see About page "([^"]*)"$/, function (url, next) {
expect(browser.getLocationAbsUrl()).to.equal(url);
next();
});
};
我的问题是:
有时用户名和密码不能输入到元素中,但解析仍然是通过。我不知道为什么
2.我想使用'http-backend-proxy'
模拟数据而不向服务器发送请求,但它不起作用,错误是angular is not defined
。如何在发送请求时模拟数据?
请帮帮我,谢谢。
答案 0 :(得分:1)
关于1)
this.Then(/^input userName "([^"]*)", password "([^"]*)"$/, function (userName, password, next) {
browser.driver.findElement(by.id('userName')).sendKeys(userName);
browser.driver.findElement(by.id('pass')).sendKeys(password);
next();
});
量角器是异步的。您对回调的使用
next()
在零件
之后直接执行browser.driver.findElement(by.id('userName'))
所以先前语句中的异步部分有时足够快,有时甚至更晚。
sendKeys(userName);
这导致功能步骤变为绿色,如果量角器事情正确完成则无法控制。
你也不要使用chai expect。
建议1.1:不要使用下一个回调,链式异步承诺:
this.Then(/^input userName "([^"]*)", password "([^"]*)"$/,
function (userName, password) {
return browser.driver.findElement(by.id('userName'))
.sendKeys(userName)
.then(function(){
return browser.driver.findElement(by.id('pass'))
.sendKeys(password);
}
});
外部" this.then" function使用promise链的结果作为退出点,替换next()回调以离开步骤。
建议1.2:使用chai的期望(也与chaiAsPromised同步)
this.Then(/^go to some url$/, function () {
var targetUrl = "http://example.com";
browser.get(targetUrl);
expect(browser.getCurrentUrl()).to.eventually.equal(targetUrl);
});
关于2), 您的回复必须包含正文中的ng-app标记,请参阅Uncaught ReferenceError: angular is not defined - AngularJS not working