我已从 Protractor 1.8 升级到 Protractor 2.0。在执行 it block 中的测试场景之前使用 beforeAll (jasmine 2.0) 登录我的应用程序时,我看到奇怪的行为。似乎对 element.clear() 的第一次调用是最后发生的。我看到这种情况发生:
- 在 usernameFld 中输入“teacher01”
- institutionFld 已清除
- “school01”被输入到titutionsFld
- passwordFld 已清除
- 在passwordFld中输入“teacherpassword”
- usernameFld 已清除
- 点击了loginBtn
- 测试失败,因为 usernameFld 为空
有人可以告诉我我做错了什么吗(参见下面的代码)?
注释:
我尝试切换到 directConnect。我尝试将clear() 和sendKeys() 分成不同的行。两者似乎都没有帮助。
使用相同的代码并回滚到 Protractor 1.8.0,我没有这个问题。先清除usernameFld,然后在usernameFld中输入用户名,测试能够正常登录。将登录代码从 beforeAll 移动到“it” block 中也使测试的行为符合预期。
请注意,passwordFld 是密码类型的输入,loginBtn 只是一个按钮。这是对非 Angular 网页的测试。我使用 jasmine 2.0、chrome 浏览器 41.0.2272.101、firefox 36.0.4 并从测试脚本启动 selenium 服务器。
HTML:
<input type="text" name="teacherUsername" size="20" value="" tabindex="1" id="username" title="Enter your Username">
<input type="text" name="teacherInstitution" size="20" value="" tabindex="2" id="institution" title="Enter your Institution">
<input type="password" name="teacherPassword" size="20" tabindex="3" id="password" title="Enter your Password">
<button class="submit" tabindex="4" id="loginbtn">Log In</button>
conf.js:
exports.config = {
framework: 'jasmine2',
specs: ['school_spec.js']
};
测试:
describe('Protractor 2.0', function() {
browser.ignoreSynchronization = true;
var loginBtn = element(by.id('loginbtn'));
var usernameFld = element(by.id('username'));
var institutionFld = element(by.id('institution'));
var passwordFld = element(by.id('password'));
beforeAll(function() {
browser.get('http://mypage.net/login.html');
usernameFld.clear().sendKeys('teacher01');
institutionFld.clear().sendKeys('school01');
passwordFld.clear().sendKeys('teacherpassword');
loginBtn.click();
});
it('should work with beforeAll', function() {
expect(browser.getTitle()).toEqual('My Classes');
});
});
最佳答案
我不会向您提供解释,但希望能提供解决方案。
将 browser.ignoreSynchronization = true;
移至 beforeAll()
并仅在 clear()
完成后调用 sendKeys()
:
beforeAll(function() {
browser.ignoreSynchronization = true;
browser.get('http://mypage.net/login.html');
usernameFld.clear().then(function () {
usernameFld.sendKeys('teacher01');
});
institutionFld.clear().then(function () {
institutionFld.sendKeys('school01');
});
passwordFld.clear().then(function () {
passwordFld.sendKeys('teacherpassword');
});
loginBtn.click();
});
关于angularjs - 在 Protractor 2.0 中,在 beforeAll 中调用 clear() 行为不当,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243548/