You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium结合WDIO与Jasmine时错误处理失效问题求助

Troubleshooting Failed Error Handling in Appium + WDIO + Jasmine Tests

Hey there, let's break down why your error handling isn't kicking in and fix it step by step.

Key Issues in Your Current Code

Looking at your test snippet, there are a couple of reasons the error handler isn't working as expected:

  • Incorrect placement of error handling in the Promise chain
    Your error callback is attached to the .getText() call, but if the preceding .element('someName') fails (e.g., element not found), that rejection won't reach this callback. The Promise chain skips successful then handlers when a rejection occurs, so you need a top-level catch to capture any errors from earlier steps.

  • Throwing errors inside a Promise callback without proper propagation
    When you throw new Error() inside the then error callback, this creates a new rejection, but since you're not passing this to the done callback, Jasmine might not pick up the failure. The done function needs to explicitly receive any errors to mark the test as failed.

  • Missing catch for unhandled rejections
    Without a .catch() at the end of the chain, any unhandled rejections can be swallowed, especially in older Promise implementations or when using WDIO's custom Promise wrapper.

Fixed Test Code

Here's the revised version of your test with proper error handling:

it('wtf', (done) => {
  client.init()
    .element('someName')
    .getText()
    .then(text => {
      expect(text).toBe('correct');
    })
    .catch(err => {
      // Capture any error from the entire chain
      const errorMsg = `Cannot get text of element: #someName\n${err}`;
      done(new Error(errorMsg)); // Pass error to Jasmine's done callback to mark test as failed
    })
    .finally(() => {
      // Optional: Clean up the session regardless of pass/fail
      return client.end();
    });
});

Additional Troubleshooting Steps

  • Check full Appium server logs: Your provided log snippet only shows the start of the request—look for the corresponding response (e.g., [HTTP] <-- POST /wd/hub/session/...) to confirm if Appium returned an explicit error like no such element. This will validate exactly where the failure is occurring.
  • Verify WDIO configuration: Ensure your wdio.conf.js doesn't have settings that suppress errors (e.g., avoid overly aggressive bail values, and check that no custom error interceptors are swallowing rejections).
  • Use async/await for cleaner code: If your environment supports it, switching to async/await makes error handling more intuitive with try/catch blocks:
    it('wtf', async () => {
      try {
        await client.init();
        const element = await client.element('someName');
        const text = await element.getText();
        expect(text).toBe('correct');
      } catch (err) {
        throw new Error(`Cannot get text of element: #someName\n${err}`);
      } finally {
        await client.end();
      }
    });
    

内容的提问来源于stack exchange,提问作者Alex Cap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:45:13