排毒-如何在不使用Expect的情况下检查元素是否存在

时间:2018-11-11 02:10:45

标签: detox

是否有一种方法可以在不使用Detox的情况下检查元素是否存在?现在,我必须将我的逻辑嵌套在try / catch块中,以控制测试流程以减轻不稳定感,因为它会在继续测试之前检查屏幕的状态。我更希望能够使用if / else。

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

这不是最优雅的解决方案,但是我使用以下内容简化了我的代码。

在帮助程序文件中,我创建包装try / catch并返回true / false的函数:

例如:

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

module.exports = { expectToBeVisible };

然后,当我执行依赖于该元素是否可见的测试时,我可以执行以下操作:

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
    }
  });

 ...
});

这不是最好的解决方案,但是在Detox提出拥有if / else的能力之前,此刻就足够了。