使用Detox的React Native端到端测试:获取匹配元素的高度、宽度和其他属性



如标题所述,我想以编程方式检索Detox中匹配元素的属性。我知道排毒看到它们,因为我们都知道当toBeVisible期望通过75%视图规则失败时,我们可以看到排毒"得到"什么,通常是这样的消息:

Got: "ReactTextView{id=13725, visibility=VISIBLE, width=376, height=102, 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@ac227fb, tag=PROFILE_NAME_TEXT, root-is-layout-requested=false, has-input-connection=false, x=486.0, y=120.0, text=Anonymous, input-type=0, ime-target=false, has-links=false}"

我们有width,height,focus等。

我的问题是,我们如何通过编程获得这些属性?具体来说,我有一个元素,它在点击按钮后展开,我想确保它的高度比之前大。

如果您检查element(by.id("SOME_ID"))可用的方法,则只有操作方法可用…

我终于找到了答案,因为当时detox缺少类型,所以很难找到最初的答案。我提到的heightwidth等属性可以通过getAttributes()方法获得:

const attributes = await element(by.text('Tap Me')).getAttributes();
expect(attributes.text).toBe('Tap Me');
const multipleMatchedElements = await element(by.text('Multiple')).getAttributes();
expect(multipleMatchedElements.elements.length).toBe(5);
expect(multipleMatchedElements.elements[0].identifier).toBe('FirstElement');

可用的属性在Android和iOS之间也有所不同。请参阅getAttributes()库中的文档,以及关于添加所有类型的detox的pull请求。

最新更新