使用For-Loops干燥量角器点击测试 [英] DRYing up protractor clicks tests using a For-Loops
问题描述
问题:
如何点击 ul>中的每个链接? li a
来自一次测试?
问题:此测试正在通过;但是,它没有点击链接。我知道这是因为它没有重定向或等待2000ms。
Problem: this test is passing; however, it isn't clicking on the links. I know this because It isn't redirecting or waiting 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]);
});
}
});
更新:找到答案:答案
推荐答案
上述答案和方法似乎都采用了同步方法,所以我想提供我找到的使用标准异步量角器方法的相同问题的解决方案。
the above answers and approaches all seem to take a 'synchronous' approach, so I'd like to offer the solution I found for the same issue which uses the standard asynchronous protractor approach.
在此示例中,页脚链接是文本链接,例如关于,联系人等 - 并且这些链接映射到诸如/ about和/ contact等的URL。
In this example, the footer links are text links such as 'ABOUT', CONTACT', etc. - and these map to URLs such as '/about' and '/contact', etc.
编辑:ptor之前是从protractor.getInstance()定义的;
ptor is defined earlier from 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());
}
});
});
});
这篇关于使用For-Loops干燥量角器点击测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!