Detox - How to check if an element is present without using expect

╄→尐↘猪︶ㄣ 提交于 2020-04-17 16:17:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/53245252/detox-how-to-check-if-an-element-is-present-without-using-expect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!