[英]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.
他们都声称自己可以工作,但我做不到。
I'm using; 我正在使用; -
selenium-webdriver
- jasmine-node-reporter-fix
( jasmine-node
errors) selenium-webdriver
jasmine-node-reporter-fix
( jasmine-node
错误)
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.
返回的页面标题是Google主页,而不是搜索结果页面。 (Browser ends up on the search results page).
(浏览器最终显示在搜索结果页面上)。
Code 码
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);
});
});
Result 结果
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. 所以有人说我需要延长茉莉花超时时间,这没有什么区别。
Others saying you need the Jasmine done() method - without this the test doesn't complete. 其他人则说您需要Jasmine done()方法-没有此方法,测试将无法完成。
I cannot see why the wait doesn't wait! 我不明白为什么等待不等待! - the result is returned immediately whatever the timeout provided.
-无论提供什么超时,结果都将立即返回。
See this answer - "The inner function will return a promise that driver.wait will wait for and will take its value (true/false) as the waiting condition" 看到这个答案 -“内部函数将返回一个承诺,driver.wait将等待,并将其值(true / false)作为等待条件”
The change you should apply is - 您应该应用的更改是-
driver.wait(function () {
return driver.getTitle().then(function (title) {
return title === 'webdriver - Google Search';
});
}, 5000);
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. 因此,上述问题是我无法使用driver.wait使Jasmine在执行断言之前等待页面加载。
Jasmine was completing before driver.wait returned. 茉莉花在driver.wait返回之前完成。
Instead I used Jasmine's waitsFor() method. 相反,我使用了Jasmine的waitsFor()方法。
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);
});
});
});
Like you said in your own answer, the problem was that you weren't waiting for the page to load. 就像您在自己的答案中说的那样,问题在于您没有等待页面加载。 I really don't like the method name for driver.wait, because it implies that it will wait the specified amount of time before continuing, but that's not the case.
我真的不喜欢driver.wait的方法名称,因为这意味着它将在继续之前等待指定的时间,但是事实并非如此。 It's only a timeout if your promise does not resolve.
如果您的承诺没有解决,那只是超时。 To wait, you should use driver.sleep(1000).
要等待,您应该使用driver.sleep(1000)。 Then you can add your expectations, like so:
然后,您可以添加您的期望,如下所示:
driver.sleep(1000).then(function () {
return driver.getTitle().then(function (title) {
expect(title).toBe('webdriver - Google Search');
done();
});
});
(Since you were using done in your question, I'm assuming Jasmine 2.0 and done
) (由于您在问题中使用了“完成”,因此我假设使用Jasmine 2.0并
done
)
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.