Selenium tests written using Junit are not running in order



  • Find the attached screen shot for reference(Scripts with result)

    I executed some selenium scripts, but they are not running properly. Execution is taking the scripts randomly and start execution. I want to execute the scripts as per script order. Please tell me a solution for this.

    For example the scripts' order is:

    1. Loginpage (1)
    2. Dashboard (2)
    3. firstlink (3)
    4. secondlink (4)
    5. thirdlink (5)

    Execution order:

    1. firstlink (3)
    2. Dashboard (2)
    3. secondlink (4)
    4. loginpage (1)
    5. thirdlink (5)

    How to execute these scripts in the proper order?



  • Two parts to this answer: First, to answer the question, there are many test runners. Some of them by default will run in a particular order, it may be the order they appear in code, or alphabetically, or some other order. There is often a way to tell the test runner to execute them in a particular order by providing some additional command line parameters, or altering the config. From your screenshot you're using junit, junit actually does not have a way to force the order of execution, see this for details: https://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4

    The second part is not directly answering the question, but is important and is also discussed in the linked answer from stackoverflow. You should avoid having tests that need to be run in a specific order. What if you want to execute only the last test, or debug the last test? Do you have to wait for all of the other tests to execute? What if one of the tests early in the chain fails, likely all of the rest of the tests will fail as well. What happens when you get a large suite of tests and you want to execute them in parallel? It is typically a best practice to have individual tests self contained and not relying on other other tests for the reasons outlined above.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2