Javascript 排毒:仅当存在时点击按钮

Javascript 排毒:仅当存在时点击按钮,javascript,testing,automated-tests,detox,Javascript,Testing,Automated Tests,Detox,在我的测试中,我想模拟“cancelUpgrade”按钮中的点击,仅当它显示时: it('should be in home menu', async () => { await waitFor(element(by.id('cancelUpgrade'))) .toBeVisible() .withTimeout(2000); await element(by.id('cancelUpgrade')).tap(); }); 它返回预期错误错误:找不到UI元素。

在我的测试中,我想模拟“cancelUpgrade”按钮中的点击,仅当它显示时:

it('should be in home menu', async () => {
  await waitFor(element(by.id('cancelUpgrade')))
    .toBeVisible()
    .withTimeout(2000);
  await element(by.id('cancelUpgrade')).tap();
});
它返回预期错误
错误:找不到UI元素。


您可以在try/catch块中环绕tap:

it('should be in home menu', async () => {
  await waitFor(element(by.id('cancelUpgrade')))
    .toBeVisible()
    .withTimeout(2000);
  try {
    await element(by.id('cancelUpgrade')).tap();
  } catch (e) {}
  // continue your tests
});

这不是最好的方法,但我认为这是目前排毒中可能的方法。

我怀疑我们可以将此模式与
结合使用

it('should be in home menu', async () => {
  await waitFor(element(by.id('cancelUpgrade')))
    .toBeVisible()
    .withTimeout(2000);
  try {
    await expect(element(by.id('cancelUpgrade'))).toExist();
  } catch (e) {}
  // continue your tests
});
如果您不需要等待:

it('should be in home menu', async () => {
  try {
    await expect(element(by.id('cancelUpgrade'))).toExist();
  } catch (e) {}
  // continue your tests
});

什么是“排毒”?似乎是这样的:对于新来的人来说,can是可重复使用的,而且很好:)你能解释一下你的做法与OP有什么不同,以及为什么你的示例能按预期工作吗?只是想提供更多关于此解决方案工作原理的信息。来自解毒文档:“注意:waitFor在到达超时时不会抛出,而是会继续到下一行。”因此,如果按钮不可用,点击()将抛出,在这种情况下,它会被捕获悄悄吞没。另外,FWIW,您可以使用
try/catch
“技巧”在
catch
块中放置备用代码路径。