detox-getprops
v0.1.2
Published
Fetch element props using Detox
Downloads
10,871
Readme
detox-getprops
Allows fetching an element text / properties using Detox.
The standard Detox API allows fetching element text / attributes on iOS using the getAttributes
method, but Android currently has no standard way of fetching element text. This package works on both.
Installation:
npm install --save-dev detox detox-getprops
Usage:
const { getText } = require('detox-getprops');
const text = await getText(element(by.id('heading')));
expect(text).toEqual('Step One');
See also a full example in its own repo.
Good to know
This is a horrible hack/workaround inspired by this Stack Overflow answer.
It searches for text that is not present in the element, catches the resulting exception, and parses the platform-dependent error message for the element properties.
This package should not be used once Detox #2083 is resolved.
Methods
getText
getText
returns a promise for the text from a <Text>
element. Usage example await getText(element(by.id('heading')))
.
Using getText
on a non-<Text>
element is platform-dependent: on iOS it returns the text content of the sub-elements, on Android it returns undefined
.
getProps
getProps
returns a promise for some properties of the element (which can also be a non-<Text>
element). Usage example await getProps(element(by.id('myview')))
.
The properties are highly platform-dependent. The only common fields are type
(the underlying component type), device
(either android
or ios
) and text
(the text content returned by getText
).
Example Android output:
{
id: '15',
visibility: 'VISIBLE',
width: '954',
height: '85',
'has-focus': 'false',
'has-focusable': 'false',
'has-window-focus': 'true',
'is-clickable': 'false',
'is-enabled': 'true',
'is-focused': 'false',
'is-focusable': 'false',
'is-layout-requested': 'false',
'is-selected': 'false',
'layout-params': 'android.view.ViewGroup$LayoutParams@92e27dd',
tag: 'heading',
'root-is-layout-requested': 'false',
'has-input-connection': 'false',
x: '63.0',
y: '84.0',
text: 'Step One',
'input-type': '0',
'ime-target': 'false',
device: 'android',
type: 'ReactTextView'
}
Example iOS output:
{
AX: 'Y',
'AX.id': 'heading',
'AX.label': 'Step One',
'AX.frame': '{{24, 260}, {366, 29.5}}',
'AX.activationPoint': '{207, 274.75}',
'AX.traits': 'UIAccessibilityTraitStaticText',
'AX.focused': 'N',
frame: '{{24, 0}, {366, 29.5}}',
device: 'ios',
type: 'RCTTextView',
text: 'Step One'
}
The properties are what they are. Don't ask for more.
License
ISC license.