Appium结合WDIO与Jasmine时错误处理失效问题求助
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 successfulthenhandlers 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 youthrow new Error()inside thethenerror callback, this creates a new rejection, but since you're not passing this to thedonecallback, Jasmine might not pick up the failure. Thedonefunction 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 likeno such element. This will validate exactly where the failure is occurring. - Verify WDIO configuration: Ensure your
wdio.conf.jsdoesn't have settings that suppress errors (e.g., avoid overly aggressivebailvalues, 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

