我一直在使用Jasmine的Webdriver I/O进行端到端测试.一个特定的场景给了我很大的挑战.
我有一个页面,上面有5个链接.由于页面是动态的,链接的数量实际上是挑战.我想测试链接,看看每个链接是否title与title它链接到的页面相匹配.由于链接是动态生成的,我不能只为每个链接进行硬编码测试.所以,我正在尝试以下方法:
it('should match link titles to page titles', function(done) {
client = webdriverio.remote(settings.capabilities).init()
.url('http://www.example.com')
.elements('a').then(function(links) {
var mappings = [];
// For every link store the link title and corresponding page title
var results = [];
for (var i=0; i<links.value.length; i++) {
mappings.push({ linkTitle: links.value[0].title, pageTitle: '' });
results.push(client.click(links.value[i])
.getTitle().then(function(title, i) {
mappings[i].pageTitle = title;
});
);
}
// Once all promises have resolved, compared each link title to each corresponding page title
Promise.all(results).then(function() {
for (var i=0; i<mappings.length; i++) {
var mapping = mappings[i];
expect(mapping.linkTitle).toBe(mapping.pageTitle);
}
done();
});
});
;
});
Run Code Online (Sandbox Code Playgroud)
我甚至无法确认我是否正确获得了链接标题.我相信有些事我完全误解了.我甚至没有获得每个链接title属性.我肯定没有得到相应的页面标题.我想我在封闭世界中迷失了.但是,我不确定.
更新 - 11月24日 我仍然没有想到这一点.但是,我认为它与Webdriver I/O 使用 Q promise库这一事实有关.我得出了这个结论,因为以下测试工作:
it('should match link titles to page titles', function(done) {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() { resolve(); }, 1000);
});
promise.then(function() {
var promises = [];
for (var i=0; i<3; i++) {
promises.push(
new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, 500);
})
);
}
Promise.all(promises).then(function() {
expect(true).toBe(true)
done();
});
});
Run Code Online (Sandbox Code Playgroud)
但是,下列情况不工作:
it('should match link titles to page titles', function(done) {
client = webdriverio.remote(settings.capabilities).init()
.url('http://www.example.com')
.elements('a').then(function(links) {
var mappings = [];
// For every link store the link title and corresponding page title
var results = [];
for (var i=0; i<links.value.length; i++) {
mappings.push({ linkTitle: links.value[0].title, pageTitle: '' });
results.push(client.click(links.value[i])
.getTitle().then(function(title, i) {
mappings[i].pageTitle = title;
});
);
}
// Once all promises have resolved, compared each link title to each corresponding page title
Q.all(results).then(function() {
for (var i=0; i<mappings.length; i++) {
var mapping = mappings[i];
expect(mapping.linkTitle).toBe(mapping.pageTitle);
}
done();
});
})
;
});
Run Code Online (Sandbox Code Playgroud)
我没有任何例外.然而,里面的代码Q.all似乎没有被执行.我不知道该怎么做.
阅读 WebdriverIO 手册,我觉得您的方法有一些问题:
elements('a')返回 WebElement JSON 对象(https://code.google.com/p/selenium/wiki/JsonWireProtocol#WebElement_JSON_Object)而不是 WebElements,因此没有title属性,因此linkTitle将始终是undefined- http://webdriver.io/api/protocol/元素.htmlclient.click(..)输入,它需要选择器字符串而不是对象 - http://webdriver.io/api/action/click.html。单击 WebElement JSON 对象,client.elementIdClick(ID)该对象采用ELEMENTWebElement JSON 对象的属性值。client.elementIdClick执行 a 时,client将导航到页面,尝试使用client.elementIdClick下一个 ID 调用下一个 for 循环将失败,因为当您离开页面时不存在该元素。听起来就像是invalid element cache....。因此,我为您的任务提出了另一个解决方案:
elements('a')href并title使用client.elementIdAttribute(ID)每个元素并存储在对象中href使用 导航到每个 -s client.url('href'),使用title获取页面的.getTitle并将其与 进行比较object.title。我尝试过的源代码不是由 Jasmine 运行的,但应该给出一个想法:
var client = webdriverio
.remote(options)
.init();
client
.url('https://www.google.com')
.elements('a')
.then(function (elements) {
var promises = [];
for (var i = 0; i < elements.value.length; i++) {
var elementId = elements.value[i].ELEMENT;
promises.push(
client
.elementIdAttribute(elementId, 'href')
.then(function (attributeRes) {
return client
.elementIdAttribute(elementId, 'title')
.then(function (titleRes) {
return {href: attributeRes.value, title: titleRes.value};
});
})
);
}
return Q
.all(promises)
.then(function (results) {
console.log(arguments);
var promises = [];
results.forEach(function (result) {
promises.push(
client
.url(result.href)
.getTitle()
.then(function (title) {
console.log('Title of ', result.href, 'is', title, 'but expected', result.title);
})
);
});
return Q.all(promises);
});
})
.then(function () {
client.end();
});
Run Code Online (Sandbox Code Playgroud)
笔记:
href。| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |