好的,我遵循了几个略有不同的示例,正如您在下面的注释代码中看到的那样。他们都声称可以工作,但我无法让它这样做。
我正在使用;
-selenium-webdriver
- jasmine-node-reporter-fix
(jasmine-node
错误)
所以这是一个非常简单的异步测试,打开 Google 并搜索,然后获取页面标题。
问题; 返回的页面标题是 Google 主页,而不是搜索结果页面。 (浏览器最终出现在搜索结果页面上)。
代码
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
所以有些人说我需要延长 jasmine 超时时间,这没有什么区别。
其他人说您需要 Jasmine did() 方法 - 没有这个方法测试就无法完成。
我不明白为什么等待不等待! - 无论超时时间如何,结果都会立即返回。
最佳答案
参见this answer - “内部函数将返回一个 driver.wait 将等待的 promise ,并将其值(true/false)作为等待条件”
您应该应用的更改是 -
driver.wait(function () {
return driver.getTitle().then(function (title) {
return title === 'webdriver - Google Search';
});
}, 5000);
关于javascript - E2E 测试 - WebdriverJS、Selenium 和 Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24651231/