Detox - How to check if an element is present with

2019-07-29 09:31发布

Is there a way to check if an element is present without using expect with Detox? Right now I'm having to nest my logic in try/catch blocks to control the flow of a test to mitigate flakiness as it checks the state of a screen before moving forward with the test. I would much rather be able to do with using if/else.

Thanks in advance for any suggestions.

标签: detox
1条回答
家丑人穷心不美
2楼-- · 2019-07-29 10:16

Not the most elegant solution but I have used the following to stream line my code.

In a helper file, I create functions that wrap the try/catch, and return true/false:

For example:

const expectToBeVisible = async (id) => {
  try {
    await expect(element(by.id(id))).toBeVisible();
    return true;
  } catch (e) {
    return false;
  }
};

module.exports = { expectToBeVisible };

Then when I am performing tests that depend on that element being visible or not I can do the following:

import { expectToBeVisible } from './helpers';

describe('Test', () => {

  ...

  it('If button is visible do X else do Y', async () => {
    let buttonVisible = await expectToBeVisible('button');

    if (buttonVisible) {
      // do something with that button
    } else {
      // do something else as the button isn't visible
    }
  });

 ...
});

This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.

查看更多
登录 后发表回答