我的页面上有一个带有ID的元素,即<p id="blabla">The Text</p>
当我运行量角器的elementExplorer,即protractor ./config/protractor-conf.js --elementExplorer http://localhost:9001
时,我输入element(by.id('blabla'))
我得到一个元素结果(复制如下)。
然而,我意识到,如果我给出任何愚蠢的id(即foo
),我看似相同的结果。我期待得到一个错误或者#34; Not Found&#34;信息。如何判断元素是否确实存在?
每次 element(by.id())
来电的结果:
> element(by.id('foo'))
ElementFinder {
ptor_:
Protractor {
getProcessedConfig: [Function],
forkNewDriverInstance: [Function],
restart: [Function],
controlFlow: [Function],
schedule: [Function],
setFileDetector: [Function],
getSession: [Function],
getCapabilities: [Function],
quit: [Function],
actions: [Function],
touchActions: [Function],
executeScript: [Function],
executeAsyncScript: [Function],
call: [Function],
wait: [Function],
sleep: [Function],
getWindowHandle: [Function],
getAllWindowHandles: [Function],
getPageSource: [Function],
close: [Function],
getCurrentUrl: [Function],
getTitle: [Function],
findElementInternal_: [Function],
findElementsInternal_: [Function],
takeScreenshot: [Function],
manage: [Function],
switchTo: [Function],
driver:
WebDriver {
session_: [Object],
executor_: [Object],
flow_: [Object],
fileDetector_: null },
element: { [Function] all: [Function] },
'$': [Function],
'$$': [Function],
baseUrl: 'http://localhost:9001',
rootEl: 'body',
ignoreSynchronization: false,
getPageTimeout: 10000,
params: {},
ready:
Promise {
flow_: [Object],
stack_: null,
parent_: null,
callbacks_: null,
state_: 'fulfilled',
handled_: true,
value_: null,
queue_: null },
plugins_:
Plugins {
setup: [Function],
teardown: [Function],
postResults: [Function],
postTest: [Function],
onPageLoad: [Function],
onPageStable: [Function],
waitForPromise: [Function],
waitForCondition: [Function],
pluginObjs: [],
assertions: {},
resultsReported: false },
resetUrl: 'data:text/html,<html></html>',
trackOutstandingTimeouts_: true,
mockModules_: [ [Object] ],
allScriptsTimeout: 11000,
dbgCodeExecutor_:
{ execPromise_: [Object],
execPromiseResult_: undefined,
execPromiseError_: undefined,
replServer_: [Object],
execute_: [Function],
execute: [Function],
complete: [Function],
resultReady: [Function],
getResult: [Function] } },
then: null,
parentElementArrayFinder:
ElementArrayFinder {
ptor_:
Protractor {
getProcessedConfig: [Function],
forkNewDriverInstance: [Function],
restart: [Function],
controlFlow: [Function],
schedule: [Function],
setFileDetector: [Function],
getSession: [Function],
getCapabilities: [Function],
quit: [Function],
actions: [Function],
touchActions: [Function],
executeScript: [Function],
executeAsyncScript: [Function],
call: [Function],
wait: [Function],
sleep: [Function],
getWindowHandle: [Function],
getAllWindowHandles: [Function],
getPageSource: [Function],
close: [Function],
getCurrentUrl: [Function],
getTitle: [Function],
findElementInternal_: [Function],
findElementsInternal_: [Function],
takeScreenshot: [Function],
manage: [Function],
switchTo: [Function],
driver: [Object],
element: [Object],
'$': [Function],
'$$': [Function],
baseUrl: 'http://localhost:9001',
rootEl: 'body',
ignoreSynchronization: false,
getPageTimeout: 10000,
params: {},
ready: [Object],
plugins_: [Object],
resetUrl: 'data:text/html,<html></html>',
trackOutstandingTimeouts_: true,
mockModules_: [Object],
allScriptsTimeout: 11000,
dbgCodeExecutor_: [Object] },
locator_:
name(name) {
return By.css('*[name="' + escapeCss(name) + '"]');
} { using: 'css selector', value: '*[id="blabla"]' },
actionResults_: null,
getWebElements: [Function],
click: [Function],
sendKeys: [Function],
getTagName: [Function],
getCssValue: [Function],
getAttribute: [Function],
getText: [Function],
getSize: [Function],
getLocation: [Function],
isEnabled: [Function],
isSelected: [Function],
submit: [Function],
clear: [Function],
isDisplayed: [Function],
getOuterHtml: [Function],
getInnerHtml: [Function],
getId: [Function],
getRawId: [Function],
serialize: [Function],
takeScreenshot: [Function] },
elementArrayFinder_:
ElementArrayFinder {
ptor_:
Protractor {
getProcessedConfig: [Function],
forkNewDriverInstance: [Function],
restart: [Function],
controlFlow: [Function],
schedule: [Function],
setFileDetector: [Function],
getSession: [Function],
getCapabilities: [Function],
quit: [Function],
actions: [Function],
touchActions: [Function],
executeScript: [Function],
executeAsyncScript: [Function],
call: [Function],
wait: [Function],
sleep: [Function],
getWindowHandle: [Function],
getAllWindowHandles: [Function],
getPageSource: [Function],
close: [Function],
getCurrentUrl: [Function],
getTitle: [Function],
findElementInternal_: [Function],
findElementsInternal_: [Function],
takeScreenshot: [Function],
manage: [Function],
switchTo: [Function],
driver: [Object],
element: [Object],
'$': [Function],
'$$': [Function],
baseUrl: 'http://localhost:9001',
rootEl: 'body',
ignoreSynchronization: false,
getPageTimeout: 10000,
params: {},
ready: [Object],
plugins_: [Object],
resetUrl: 'data:text/html,<html></html>',
trackOutstandingTimeouts_: true,
mockModules_: [Object],
allScriptsTimeout: 11000,
dbgCodeExecutor_: [Object] },
locator_:
name(name) {
return By.css('*[name="' + escapeCss(name) + '"]');
} { using: 'css selector', value: '*[id="blabla"]' },
actionResults_: null,
getWebElements: [Function],
click: [Function],
sendKeys: [Function],
getTagName: [Function],
getCssValue: [Function],
getAttribute: [Function],
getText: [Function],
getSize: [Function],
getLocation: [Function],
isEnabled: [Function],
isSelected: [Function],
submit: [Function],
clear: [Function],
isDisplayed: [Function],
getOuterHtml: [Function],
getInnerHtml: [Function],
getId: [Function],
getRawId: [Function],
serialize: [Function],
takeScreenshot: [Function] },
click: [Function],
sendKeys: [Function],
getTagName: [Function],
getCssValue: [Function],
getAttribute: [Function],
getText: [Function],
getSize: [Function],
getLocation: [Function],
isEnabled: [Function],
isSelected: [Function],
submit: [Function],
clear: [Function],
isDisplayed: [Function],
getOuterHtml: [Function],
getInnerHtml: [Function],
getId: [Function],
getRawId: [Function],
serialize: [Function],
takeScreenshot: [Function] }
答案 0 :(得分:1)
这就是量角器的设计工作方式 - 你不会得到一个未找到的元素&#34;错误,直到您开始使用元素。例如,尝试调用:
> element(by.id('something_that_does_not_exist')).getText()
以下是reference in the documentation支持:
element()函数返回一个ElementFinder对象。 ElementFinder知道如何使用您作为参数传入的定位器来定位DOM元素,但它实际上还没有这样做。 在调用操作方法之前,它不会与浏览器联系。