Mur*_*tso 48 javascript automated-tests angularjs selenium-webdriver protractor
我是量角器的新手,我正在尝试实施e2e测试.我不知道这是否是正确的方法,但是...我要测试的页面不是基于完整的角度页面,所以......我遇到了一些麻烦.
根据我的第一个规范,我有:
describe('should open contact page', function() {
var ptor = protractor.getInstance();
beforeEach(function(){
var Login = require('./util/Login');
new Login(ptor);
});
Run Code Online (Sandbox Code Playgroud)
我已创建此Login类,但登录后我想打开联系页面,但量角器会在页面完全加载之前立即尝试查找元素.
我试过用:
browser.driver.wait(function() {
expect(browser.findElement(by.xpath("//a[@href='#/contacts']")).isDisplayed());
ptor.findElement(by.xpath("//a[@href='#/contacts']")).click();
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用......它总是试图在页面加载之前找到元素.我也试过这个:
browser.driver.wait(function() {
expect(ptor.isElementPresent(by.xpath("//a[@href='#/contacts']")));
ptor.findElement(by.xpath("//a[@href='#/contacts']")).click();
});
Run Code Online (Sandbox Code Playgroud)
我能够使用browser.sleep();
但我不认为这是一个不错的选择.任何的想法?在我的登录课上,我有:
ptor.ignoreSynchronization = true;
Run Code Online (Sandbox Code Playgroud)
@href='#/contacts
在量角器尝试点击它之前,我该如何等待?
ale*_*cxe 51
量角器1.7.0
还引入了一项新功能:预期条件.
有几个预定义条件要明确等待.如果您想等待元素出现:
var EC = protractor.ExpectedConditions;
var e = element(by.id('xyz'));
browser.wait(EC.presenceOf(e), 10000);
expect(e.isPresent()).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
也可以看看:
Mur*_*tso 34
我终于找到了......
var waitLoading = by.css('#loading.loader-state-hidden');
browser.wait(function() {
return ptor.isElementPresent(waitLoading);
}, 8000);
expect(ptor.isElementPresent(waitLoading)).toBeTruthy();
var openContact = by.xpath("//a[@href='#/contacts']");
element(openContact).click();
Run Code Online (Sandbox Code Playgroud)
使用此量程器可以等待该元素,直到它加载页面消失.感谢那些试图帮助XD的人.
ash*_*ard 24
在使用量角器时,我遇到了同样的问题.在我的e2e测试中,我从非角度app开始,然后进入角度部分,然后返回到非角度部分.让事情变得棘手.关键是要了解承诺及其运作方式.以下是我在运行的e2e测试中的真实世界代码的一些示例.希望这能让您了解如何构建测试.可能在这段代码中有一些不好的做法,请随意改进,但我知道它有效,也许不是最好的方法.
为了达到我使用的角度
var ptor;
var events = require('events');
var eventEmitter = new events.EventEmitter();
var secondClick = require('./second-click');
beforeEach(function () {
browser.driver.get('http://localhost:8080/');
},10000);
it("should start the test", function () {
describe("starting", function () {
it("should find the link and start the test", function(){
var elementToFind = by.linkText('Start'); //what element we are looking for
browser.driver.isElementPresent(elementToFind).then(function(isPresent){
expect(isPresent).toBe(true); //the test, kind of redundant but it helps pass or fail
browser.driver.findElement(elementToFind).then(function(start){
start.click().then(function(){ //once we've found the element and its on the page click it!! :)
ptor = protractor.getInstance(); //pass down protractor and the events to other files so we can emit events
secondClick(eventEmitter, ptor); //this is your callback to keep going on to other actions or test in another file
});
});
});
});
});
},60000);
Run Code Online (Sandbox Code Playgroud)
虽然在角度这个代码工作
describe("type in a message ", function(){
it("should find and type in a random message", function(){
var elementToFind = by.css('form textarea.limited');
browser.driver.isElementPresent(elementToFind).then(function(isPresent){
element(elementToFind).sendKeys(randomSentence).then(function(){
console.log("typed in random message");
continueOn();
});
});
});
},15000);
Run Code Online (Sandbox Code Playgroud)
退出角度后
browser.driver.wait(function(){
console.log("polling for a firstName to appear");
return browser.driver.isElementPresent(by.name('firstName')).then(function(el){
return el === true;
});
}).
then(function(){
somefunctionToExecute()
});
Run Code Online (Sandbox Code Playgroud)
希望能给出一些指导并帮助你!
browser.driver.wait(function() {
return browser.driver.isElementPresent(by.xpath("//a[@href='#/contacts']"));
});
Run Code Online (Sandbox Code Playgroud)
这也适用于我(没有超时参数)..
有关更多信息,请参见http://angular.github.io/protractor/#/api?view=webdriver.WebDriver.prototype.wait