我有一个使用Angular 6开发的非常基本的网站,我正在尝试为此编写一些e2e测试。但是第一个脚本本身给我带来了困难。我收到以下错误-
C:\Users\user\Documents\workspace-vs-code\protractor-poc>protractor
[18:25:12] I/launcher - Running 1 instances of WebDriver
[18:25:12] I/direct - Using ChromeDriver directly...
[14500:21224:0129/182513.778:ERROR:configuration_policy_handler_list.cc(91)] Unk
nown policy: PasswordManagerAllowShowPasswords
[14500:21224:0129/182513.866:ERROR:configuration_policy_handler_list.cc(91)] Unk
nown policy: PasswordManagerAllowShowPasswords
DevTools listening on ws://127.0.0.1:58686/devtools/browser/22a05a0a-e850-47e3-b
a7b-fecc6ed97ac8
Jasmine started
(node:20576) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmp
dir() instead.
service catalog search
x should login
- Failed: script timeout: result was not received in 20 seconds
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53
f3f74387),platform=Windows NT 6.1.7601 SP1 x86_64)
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53
f3f74387),platform=Windows NT 6.1.7601 SP1 x86_64)
at Object.checkLegacyResponse (C:\Users\user\AppData\Roaming\npm\no
de_modules\protractor\node_modules\selenium-webdriver\lib\error.js:546:15)
at parseHttpResponse (C:\Users\user\AppData\Roaming\npm\node_module
s\protractor\node_modules\selenium-webdriver\lib\http.js:509:13)
at doSend.then.response (C:\Users\user\AppData\Roaming\npm\node_mod
ules\protractor\node_modules\selenium-webdriver\lib\http.js:441:30)
at process._tickCallback (internal/process/next_tick.js:68:7)
From: Task: Protractor.waitForAngular() - Locator: By(css selector, *[id="
password"])
at Driver.schedule (C:\Users\user\AppData\Roaming\npm\node_modules\
protractor\node_modules\selenium-webdriver\lib\webdriver.js:807:17)
at ProtractorBrowser.executeAsyncScript_ (C:\Users\user\AppData\Roa
ming\npm\node_modules\protractor\built\browser.js:425:28)
at angularAppRoot.then (C:\Users\user\AppData\Roaming\npm\node_modu
les\protractor\built\browser.js:456:33)
at ManagedPromise.invokeCallback_ (C:\Users\user\AppData\Roaming\np
m\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1376:14
)
at TaskQueue.execute_ (C:\Users\user\AppData\Roaming\npm\node_modul
es\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (C:\Users\user\AppData\Roaming\npm\node_m
odules\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (C:\Users\user\AppData\Roaming\npm\node_modules\protrac
tor\node_modules\selenium-webdriver\lib\promise.js:2927:27)
at C:\Users\user\AppData\Roaming\npm\node_modules\protractor\node_m
odules\selenium-webdriver\lib\promise.js:668:7
at process._tickCallback (internal/process/next_tick.js:68:7)Error
at ElementArrayFinder.applyAction_ (C:\Users\user\AppData\Roaming\n
pm\node_modules\protractor\built\element.js:459:27)
at ElementArrayFinder.(anonymous function).args [as isDisplayed] (C:\U
sers\user\AppData\Roaming\npm\node_modules\protractor\built\element.js:91:29)
at ElementFinder.(anonymous function).args [as isDisplayed] (C:\Users\
user\AppData\Roaming\npm\node_modules\protractor\built\element.js:831:22)
at UserContext.<anonymous> (C:\Users\user\Documents\workspace-vs-co
de\protractor-poc\e2e\app.servicecatalogsearch.ts:17:44)
at step (C:\Users\user\Documents\workspace-vs-code\protractor-poc\e
2e\app.servicecatalogsearch.ts:32:23)
at Object.next (C:\Users\user\Documents\workspace-vs-code\protracto
r-poc\e2e\app.servicecatalogsearch.ts:13:53)
at C:\Users\user\Documents\workspace-vs-code\protractor-poc\e2e\app
.servicecatalogsearch.ts:7:71
at new Promise (<anonymous>)
at __awaiter (C:\Users\user\Documents\workspace-vs-code\protractor-
poc\e2e\app.servicecatalogsearch.ts:3:12)
at UserContext.<anonymous> (C:\Users\user\Documents\workspace-vs-co
de\protractor-poc\e2e\app.servicecatalogsearch.ts:51:16)
From: Task: Run it("should login") in control flow
From asynchronous test:
Error
at Suite.<anonymous> (C:\Users\user\Documents\workspace-vs-code\pro
tractor-poc\e2e\app.servicecatalogsearch.ts:15:5)
at Object.<anonymous> (C:\Users\user\Documents\workspace-vs-code\pr
otractor-poc\e2e\app.servicecatalogsearch.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (C:\Users\user\Documents\workspace-vs-code\pro
tractor-poc\node_modules\ts-node\src\index.ts:422:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\A
527629\Documents\workspace-vs-code\protractor-poc\node_modules\ts-node\src\index
.ts:425:12)
**************************************************
* Failures *
**************************************************
1) service catalog search should login
- Failed: script timeout: result was not received in 20 seconds
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f7
4387),platform=Windows NT 6.1.7601 SP1 x86_64)
Executed 1 of 1 spec (1 FAILED) in 24 secs.
[18:25:39] I/launcher - 0 instance(s) of WebDriver still running
[18:25:39] I/launcher - chrome #01 failed 1 test(s)
[18:25:39] I/launcher - overall: 1 failed spec(s)
[18:25:39] E/launcher - Process exited with error code 1
我的测试脚本是-
import { browser, element, by, until, ExpectedConditions, By } from 'protractor';
describe('service catalog search', function() {
it('should login', function() {
browser.get('http://localhost:4200');
element(by.id('password')).sendKeys('abc');
});
});
现在问题出在发送密钥时。如果我删除sendkey,则它会通过,但不知道它是否正确定位。
以下是html-
答案 0 :(得分:0)
我已经建立了一个非常基本的测试,与您的测试几乎相同:
it('should have value', () => {
browser.get('http://localhost:4200/register');
element(by.id('email')).sendKeys('abc');
expect(element(by.id('email')).getAttribute('value')).toEqual('abc');
});
就我而言,它工作正常。 您可以添加断言吗?也许这可以为我们提供更多信息,以解决问题。
答案 1 :(得分:0)
我注意到您在async
块中放置了it
关键字,可能您正在尝试实现async/await方法,在这种情况下,您的代码应如下所示
it('should login', async function() {
await browser.get('http://localhost:4200');
await element(by.id('password')).sendKeys('abc');
});
如果您怀疑表达式是否需要await
关键字,请检查protractor API,您会看到,例如,sendKeys返回!webdriver.promise.Promise
。这意味着您应该添加await
,否则量角器将不会等待完成此步骤再继续前进,这可能会导致错误,就像您遇到的错误一样。
P.S。要启用async/await
,应在conf文件中放置以下选项
SELENIUM_PROMISE_MANAGER: false
否则,您的代码不需要async/await
声明,Web Driver Control Flow
将为您完成所有工作,尽管目前已弃用该代码,并将在稍后将其删除。
答案 2 :(得分:0)
经过足够的努力,我得到了答案here。非常感谢Gleb分享。
我使用了element(by)
而不是browser.driver.findElement