E2E测试 - WebdriverJS,Selenium和Jasmine [英] E2E Testing - WebdriverJS, Selenium and Jasmine
问题描述
好的,所以我按照了几个略有不同的例子,你可以在下面的评论代码中看到。他们都声称工作,但我不能这样做。
OK so I have followed several slightly differing examples, as you can see in my commented code below. They all claim to work, but I cannot get it to do so.
我正在使用;
- selenium-webdriver
- jasmine-node-reporter-fix
( jasmine-node
错误)
I'm using;
- selenium-webdriver
- jasmine-node-reporter-fix
(jasmine-node
errors)
所以这是一个非常简单的异步测试,打开Google并搜索,然后获取页面标题。
So its quite a simple asynchronous test, opening Google and searching, then getting the page title.
问题;
返回的网页标题是Google主页,而不是搜索结果页。 (浏览器最终显示在搜索结果页面上。)
Problem; The page title returned is the Google homepage and not the search results page. (Browser ends up on the search results page).
代码
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.firefox()).
build();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 9999999;
describe('basic test', function () {
it('should be on correct page', function (done) {
//driver.get('http://www.wingify.com');
//driver.getTitle().then(function (title) {
// expect(title).toBe('Wingify');
// // Jasmine waits for the done callback to be called before proceeding to next specification.
// done();
//});
driver.get("http://www.google.com");
driver.findElement(webdriver.By.name("q")).sendKeys("webdriver");
driver.findElement(webdriver.By.name("btnG")).click();
//driver.getTitle().then(function (title) {
// console.log(title);
// console.log(expect);
// expect(title).toBe('webdriver - Google Search');
// done();
//});
driver.wait(function () {
driver.getTitle().then(function (title) {
expect(title).toBe('webdriver - Google Search');
done();
});
}, 5000);
});
});
结果
Failures:
1) basic test should be on correct page
Message:
Expected 'Google' to be 'webdriver - Google Search'.
Stacktrace:
Error: Expected 'Google' to be 'webdriver - Google Search'.
at C:\Stash\Will-Hancock\grunt-jasmine\spec\test-spec.js:31:19
at C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\goog\base.js:1243:15
at webdriver.promise.ControlFlow.runInNewFrame_ (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:1539
:20)
at notify (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:362:12)
at notifyAll (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:331:7)
at resolve (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:309:7)
at fulfill (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:429:5)
at Object.webdriver.promise.asap (C:\Stash\Will-Hancock\grunt-jasmine\node_modules\selenium-webdriver\lib\webdriver\promise.js:671:5)
Finished in 4.281 seconds
1 test, 1 assertion, 1 failure, 0 skipped
所以有些人说我需要延长茉莉花超时,这没什么区别。
So some people have said I need the jasmine timeout extended, this makes no difference.
其他人说你需要Jasmine done()方法 - 没有这个测试没有完成。
Others saying you need the Jasmine done() method - without this the test doesn't complete.
我不明白为什么等待不等! - 无论超时提供什么,结果都会立即返回。
I cannot see why the wait doesn't wait! - the result is returned immediately whatever the timeout provided.
推荐答案
所以,上面的问题是我无法获得Jasmine使用driver.wait在执行断言之前等待页面加载。
So, the problem with the above was that I could not get Jasmine to wait for the page load before doing the assertion, using driver.wait.
Jasmine在driver.wait返回之前完成。
Jasmine was completing before driver.wait returned.
相反,我使用了Jasmine的waitsFor()方法。
Instead I used Jasmine's waitsFor() method.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
describe('basic test', function () {
it('should be on correct page', function () {
var match = 'webdriver - Google Search',
title = '';
driver.get("http://www.google.com");
driver.findElement(webdriver.By.name("q")).sendKeys("webdriver");
driver.findElement(webdriver.By.name("btnG")).click();
// wait for page title, we know we are there
waitsFor(function () {
driver.getTitle().then(function (_title) {
title = _title;
});
return title === match;
}, 'Test page title, so we know page is loaded', 6000);
// test title is correct
runs(function () {
expect(title).toEqual(match);
});
});
});
这篇关于E2E测试 - WebdriverJS,Selenium和Jasmine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!