skip to Main Content

I’m trying to select an element by text, but the actual text changes because it has the current time stamp in it. Is there is way to select it without doing a substring search on getAttributes()? I can’t use getAttributes because we will also be testing on android.

The select I’m using:

await expect(element(by.text("James has initiated the chatbot: ShallowFaqChatbot"))).toBeVisible();

The element:

  {
      hittable: true,
      activationPoint: { x: 153.5, y: 14.75 },
      normalizedActivationPoint: { x: 0.5, y: 0.5 },
      elementFrame: { y: 9, x: 9, width: 307, height: 29.5 },
      enabled: true,
      elementBounds: { y: 0, x: 0, width: 307, height: 29.5 },
      layer: '<CALayer: 0x600002b907c0>',
      safeAreaInsets: { right: 0, top: 0, left: 0, bottom: 0 },
      visible: true,
      elementSafeBounds: { y: 0, x: 0, width: 307, height: 29.5 },
      label: 'James has initiated the chatbot: ShallowFaqChatbot - 11:08 am',
      className: 'RCTTextView',
      frame: { y: 473, x: 34, width: 307, height: 29.5 },
      text: 'James has initiated the chatbot: ShallowFaqChatbot - 11:08 am'
  }

You can see that it has the attribute text with the text I’m looking for, however detox is unable to grab it because of the " – 11:08 am".

2

Answers


  1. You can try using detox "and" matcher

    For this you’ll have to set your element’s testID.
    Now, let’s suppose your element’s testID is super_text_field

    If you want to check that this element contains a specific text like "something" you can use the following workaround:

    await expect(element(by.id('super_text_field').and(by.text('something')))).toBeVisible()
    

    Of course this workaround has its limitations and assumptions, but I hope it works for your case!

    Login or Signup to reply.
  2. Unfortunately, Detox doesn’t support Regex matchers: https://github.com/wix/Detox/issues/1441.

    The easiest fix here would be adding a testID to the element and matching on that.

    await expect(element(by.id("James has initiated the chatbot: ShallowFaqChatbot"))).toBeVisible();
    

    You would not need to use .and to do this match. Not an ideal solution, but since testID would be hidden away, this should be a working solution for your case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search