问题:
如何从一次测试点击ul > li a
中的每个链接?
问题:此测试正在通过;但是,它不是点击链接。我知道这是因为它没有重定向或等待2000ms。
测试
it("should have proper page linking to all footer link", function() {
browser.driver.sleep(2000);
browser.ignoreSynchronization = true;
//creates an array of strings of all the menu items
var titles = element.all(by.css('.leftMenu.first .submenu li a'))
.map(function(elm) {
return elm.getText().then(function(text){
return text;
});
});
//iterates through the links via titles array
for (var i = 0; i < titles.length; i++) {
// creates a link via selection of cssContainText of the titles array
var link = element(by.cssContainingText('.submenu li a', titles[i]));
//click event
link.click().then(function() {
browser.driver.sleep(2000);
//currently arbitrary expectation but will pass
expect(browser.driver.getTitle()).toBe('welcome to: ' + title[i]);
});
}
});
更新:找到答案:ANSWER
答案 0 :(得分:3)
上述答案和方法似乎都采取了同步的方式。方法,所以我想提供我找到的使用标准异步量角器方法的相同问题的解决方案。
在此示例中,页脚链接是文本链接,例如&#39;关于&#39;,联系人&#39;等等 - 并且这些链接映射到诸如&#39; / about&#39;之类的URL。和&#39; /联系&#39;等
编辑:ptor之前是从protractor.getInstance();
定义的it('should have a working set of footer links to internal pages', function() {
// element.all( -your selectors- )
// .then() is passed an ARRAY of element finders
element.all(by.css('.footer .nav .links')).then(function(elems) {
// for each element .getText() returns a promise
var txts = elems.map(function(elem) {
return elem.getText().then(function(txt) {
if(txt != ''){
return txt;
}
});
});
// txts is now an ARRAY of promises
// When they are ALL fulfilled the loop below is run
protractor.promise.all(txts).then(function(links) {
for (var i=0; i<links.length; i++) {
// reset browser back to page of interest
// the home page in this case
browser.get('/');
// get a fresh instance of the element and click it
// attempts to click pre-created element list
// will result in 'stale' elements references as pages
// are being navigated
element.all(by.css('.footer .nav .links')).get(i).click();
// expectation of navigation
expect(ptor.getCurrentUrl()).toContain(links[i].toLowerCase());
}
});
});
});
答案 1 :(得分:1)
DUPLICATE:ANSWER HERE
您需要将it
块包装在IIFE中以强制同步
for(var i=0; i < testParams.length; i++) {
(function(testSpec) {
it('write your test here', function() {
//test code here
}
})(testParams[i]);
};
答案 2 :(得分:0)
您的主要问题是:
return elm.getText;
getText是一个返回promise的方法。所以你需要的更像是
return elm.getText().then(function(text){
return text;
});
当您调用getText或getInnerHtml之类的方法时,它只返回一个promise。如果您没有使用 expect ,如果您希望解析承诺以获取值,则需要链接然后以返回值。