keylogger
keylogger

Reputation: 872

How to choose the next @Test method based on the runtime result in TestNG + Selenium WebDriver?

I have the first @Test method. The next steps will depend on the result of that first @Test method. Here is the example :

  @Test
    public void checkErrorMessage() {

        if(searching.isErrorExist()==true) {
        //Go to method dealWithErrorPart1();

        }else{
        //Go to method continue1();
        }
    }

  @Test (dependsOnMethods = { "checkErrorMessage" })
  public void  dealWithErrorPart1() {
  //Do something with the Error ... First Step
  }


    @Test (dependsOnMethods = { "dealWithErrorPart1" })
  public void  dealWithErrorPart2() {
  //Do something with the Error ... Second Step
  }

    @Test (dependsOnMethods = { "checkErrorMessage" })
  public void  continue1() {
  //Continue doing something ... Part one
  }

      @Test (dependsOnMethods = { "continue1" })
  public void  continue2() {
  //Continue doing something ... Part two
  }

If method checkErrorMessage() finds Error Message , then the next method that should be invoked is dealWithErrorPart1() . After that dealWithErrorPart2() should be run.

If method checkErrorMessage() finds No Error Message , then the next method that should be invoked is continue1() . After that continue2() should be run.

So, checkErrorMessage() will decide the next flow of the codes.

Because I can't change the application's behavior, I need to handle each behavior separately. If the application gives me outcome A , then my test automation should execute method A1() , A2() , and A3() . If the application gives me outcome B , then my test automation should execute method B1() , B2() , and B3() .

How can I achieve that in TestNG? I tried to use throw new SkipException("Skipping this exception"); but that doesn't seem to solve this case. Thank You.

Upvotes: 0

Views: 287

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14736

To the best of my knowledge, TestNG does not let you build this sort of fluid orchestration of your test methods.

TestNG lets users define order in which test methods will be executed in the ONLY below ways:

  • Using priority (Here users can define only the order, and the methods will always be executed)
  • Using dependsOnMethods/dependsOnGroups (Here users can define the order and the expectation is that, the method on which the current method is dependent on, will be executed if and only if that method ran to completion without failures)

You might want to just define a TestNG test method and have the test method handle all the application behavior as conditions rather than expect TestNG to control this orchestration for you.

Upvotes: 1

Related Questions