webdriverio icon indicating copy to clipboard operation
webdriverio copied to clipboard

[💡 Feature]: Handling Failed scenarios

Open Rudecruels opened this issue 3 years ago • 1 comments

Is your feature request related to a problem?

Yes case 1 When testing a website, which contains a form. The title of the page will dynamically change.so when checking await expect(browser).toHaveTitle() it will fail. i don't wanna fail this scenario.

case 2

  it("click", async function () {
    await $('//*[@id="user-name"]').click();
  });
  it("set value", async function () {
    await $('//*[@id="user-name"]').setValue("standarduser");
  });

In the given case, my need is to click an input field and set value there. Both the actions are performed in same input field. If the click action failed, i don't wanna stop the execution, just need to throw an error and continue the execution

case 3 If the setValue action failed, i don't want to continue the execution. i want to stop that. for the reference and reproducible code check the repository https://github.com/Rudecruels/webdriverio-error-handling

Describe the solution you'd like.

There are three conditions

  1. Error and stop
  2. Error and continue
  3. Warning and continue

Based on these conditions i want to run each tests.

  it("go to url", async function () {
    await browser.url("https://www.saucedemo.com/");
  });
 it("go to url", async function () {
   await expect(browser).toHaveTitle("sauseDemo")
  });
 it("go to url", async function () {
   await  $('//*[@id="user-name"]').click()
  });
it("go to url", async function () {
   await  $('//*[@id="user-name"]').setValue("standarduser")
  });
 it("go to url", async function () {
   await  $('//*[@id="padssdsdssword"]').click()
  });
it("go to url", async function () {
   await  $('//*[@id="passwoqqqrd"]').setValue("standarduser")
  });
it("go to url", async function () {
   await  $('//*[@id="login-button"]').click()
  });

In the given case the condition for the second test is warning and continue. If the test fails, i don't wanna stop the execution. i just need to give warning to the output and continue the execution. The condition for the fifth test is error and continue. If it fails, no need to stop the exection, give error to the output and continue the execution. The condition for the sixth test is error and stop. i want to stop the execution, no need to continue.

Describe alternatives you've considered.

we have already implemented this by setting the bail to 0. Then checking the condition before start the test execution. Based on that i called deletesession() and process.exit(0). But it cause many wierd problems and the test does not reach the onsuiteEnd and some other hooks.

Additional context

Also tried to change value of bail in every test, but it does not worked, The value has been changed but didn't cause any effect

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Rudecruels avatar Nov 23 '23 05:11 Rudecruels

case 1

Don't use an assertion here if you don't want your test to fail.

case 2

You can always attach a .catch to the command to log an eventual error this command can cause:

  it("click", async function () {
    await $('//*[@id="user-name"]').click().catch((err) => console.error(err));
  });

you can also wrap the test block within a describe which makes the error pop up in the test report and will make your test fail but will continue the other tests.

If the setValue action failed, i don't want to continue the execution. i want to stop that.

You can set bail options for frameworks which will make the test framework e.g. Mocha not execute any more tests.

Please join our Discord channel if you have further questions. Note that we are not looking into introducing any conditional test handlers because it will make tests very unpredictable. If a command fails something is wrong and it should make your test fail. If your execution is highly conditional it will be more difficult to understand what was going on in the test which in my experience is not a recommended way to go.

christian-bromann avatar Nov 23 '23 17:11 christian-bromann