我已经完成了这个SO问题中提到的操作,将一个文件包含到我的 Protractor 测试中:How to reuse code in Protractor / AngularJS Testing 。问题是从包含的代码内部调用的方法对我看到的浏览器窗口没有影响。
我在一个单独的文件中创建了一个登录方法并将其包含在内,但是当我从 Protractor 测试中调用该方法时,打开的浏览器只会在那里停留几秒钟,然后继续,就像我没有调用任何东西一样。我可以通过 console.log 验证该方法是否被调用。下面是我的代码文件。
login.js,包含的登录功能:
exports.login = function ()
{
console.log("Login method called");
it('logs in', function () {
browser.get('http://my-url.com/login/');
// log in to continue with checkout
element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();
// login functions on timeout
browser.driver.sleep(1000);
});
}
conf.js, Protractor 配置文件:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: [/*'specs/overview.js',*/ 'specs/shopping-cart-state.js', 'specs/checkout-state.js', 'specs/buy-item.js'],
onPrepare: function () {
var SpecReporter = require('jasmine-spec-reporter');
// add jasmine spec reporter
jasmine.getEnv().addReporter(new SpecReporter({ displayStacktrace: 'all' }));
protractor.login = require('./login.js');
},
jasmineNodeOpts: {
print: function () { },
defaultTimeoutInterval: 5000000
}
};
spec.js,测试规范:
describe('Log in with an included function', function () {
it('logs in', function () {
protractor.login.login();
browser.driver.sleep(5000);
});
});
即使我将浏览器对象作为参数传递给登录函数,仍然没有任何反应,也没有收到任何错误消息。谁能告诉我我做错了什么?
最佳答案
不要将逻辑包装在“登录”模块的 it()
内:
exports.login = function ()
{
this.login = function () {
console.log("Login method called");
browser.get('http://my-url.com/login/');
// log in to continue with checkout
element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();
// login functions on timeout
browser.driver.sleep(1000);
};
}
关于node.js - 如何从 Protractor 中包含的文件进行导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184623/