10

I want to test a view that has a list and filtering functionality:

  1. I want to check the text of the first row and save it
  2. Filter using that text
  3. Check again that the same element is rendered

Thing is, when I match and element using element(by.id('some-id')), how can I retrieve info from that element (if it is possible) like the text it contains?

Ferran Negre
  • 3,453
  • 3
  • 32
  • 55
  • If your component is a `Text` component, you can use `expect(element(by.id('some-id'))).toHaveText('Welcome to React Native!')` – Dan Nov 17 '17 at 13:42
  • But what if I do not know the text that is going to be in there? – Ferran Negre Nov 17 '17 at 14:05
  • 2
    This feature is not yet supported, it's a good extension of the API, PRs are very welcome! – Rotemmiz Nov 19 '17 at 20:46
  • @FerranNegre "But what if I do not know the text that is going to be in there?" - always good to try and keep your tests deterministic. – Antoni4 Nov 24 '17 at 11:13
  • Oh no, I wish this feature was supported :( I'm trying to use detox to run automated benchmarks and now I'm stuck at the final part, that is getting the benchmark result from the screen. – Bruno Lemos Dec 02 '17 at 11:56
  • 1
    Opened an issue for this: https://github.com/wix/detox/issues/445 – Bruno Lemos Dec 02 '17 at 12:07

3 Answers3

6

I created the detox-getprops npm package incorporating the hack mentioned by Maxime Helen. It allows retrieving the text and some other (platform-dependent) properties of the element.

const { getText } = require('detox-getprops');

const text = await getText(element(by.id('heading')));
expect(text).toEqual('Step One');

I hope Detox #445 will be resolved soon after which this package can be deprecated.

Update: You can now fetch the text on iOS using the getAttributes method. The detox-getprops library is still needed for Android (tracked using Detox #2083).

Sampo
  • 2,619
  • 3
  • 25
  • 38
4

Hacky/funny workaround elaborated from this comment:

const readVisibleText = async (testID) => {
  try {
    await expect(element(by.id(testID))).toHaveText('_unfoundable_text');
    throw 'We never should get here unless target element has unfoundable text';
  } catch (error) {
    if (device.getPlatform() === 'ios') {
      const start = `accessibilityLabel was "`;
      const end = '" on ';
      const errorMessage = error.message.toString();
      const [, restMessage] = errorMessage.split(start);
      const [label] = restMessage.split(end);
      return label;
    } else {
      // Android to be tested
      const start = 'Got:';
      const end = '}"';
      const errorMessage = error.message.toString();
      const [, restMessage] = errorMessage.split(start);
      const [label] = restMessage.split(end);
      const value = label.split(',');
      var combineText = value.find(i => i.includes('text=')).trim();
      const [, elementText] = combineText.split('=');
      return elementText;
    }
  }
};
Maxime Helen
  • 1,218
  • 6
  • 15
0

This is currently not supported, the progress is being tracked in this issue.

Daniel Schmidt
  • 10,316
  • 4
  • 32
  • 63