gpt4 book ai didi

webdriver - Jasmine - 通过 Webdriver I/O 测试链接

转载 作者:行者123 更新时间:2023-12-02 23:05:33 25 4
gpt4 key购买 nike

我一直在使用 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();
});
});
;
});

我什至无法确认我是否正确获取了链接标题。我相信有些事情我完全误解了。我什至没有获得每个链接的 title 属性。我肯定没有得到相应的页面标题。我想我在这里迷失了封闭的世界。不过,我不确定。

更新 - 11 月 24 日我还没弄清楚这一点。但是,我相信这与 Webdriver I/O uses 有关。 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();
});
});

但是,以下方法不起作用:

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();
});
})
;
});

我没有遇到任何异常。然而,Q.all 内部的代码似乎没有被执行。我不知道在这里做什么。

最佳答案

阅读WebdriverIO手册,我觉得您的方法有一些错误:

  • elements('a')返回 WebElement JSON 对象 ( https://code.google.com/p/selenium/wiki/JsonWireProtocol#WebElement_JSON_Object ) 而不是 WebElements,因此没有 title因此属性(property)linkTitle永远是undefined -http://webdriver.io/api/protocol/elements.html
  • 此外,因为它是一个 WebElement JSON 对象,所以您不能将其用作 client.click(..)输入,需要选择器字符串而不是对象 - http://webdriver.io/api/action/click.html 。单击 WebElement JSON 对象 client.elementIdClick(ID)相反,它采用 ELEMENT WebElement JSON 对象的属性值。
  • client.elementIdClick执行后,client将导航到该页面,尝试调用 client.elementIdClick在下一个带有下一个 ID 的 for 循环中将失败,因为当您离开页面时不存在这样的元素。听起来像 invalid element cache.... .

因此,我为您的任务提出了另一种解决方案:

  • 像使用 elements('a') 一样查找所有元素
  • 阅读 hreftitle使用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();
});

注意:

  • 当链接使用 JavaScript 事件处理程序而不是 href 触发导航时,这无法解决您的问题。属性。

关于webdriver - Jasmine - 通过 Webdriver I/O 测试链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33804569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com