Alex Rivera | Logout

Selenium + JUnit: test order/flow?

Asked 2012-09-13T10:41:28.963
12

I am using Selenium to test my java web app's html pages (JSPs actually). My web app requires a flow to access each pages (it is a small online game web app), as in: to get to page B you'd need to go to page A, enter some text and press a button to get to page B. Obviously I already have some tests to verify that page A is working properly.

I would like to be able to write more tests in order to check that after the tests for page A run I'll get my tests for page B running (and so on for the rest of the app). So in short: define some order in my tests somehow.

After doing lots of reading about testing in the last few days, I cannot find anything interesting on this specific subject. Hence I'm asking for advice now.

Possible Solutions I have identified:

  1. Define (in the same test class) test methods for page A, then test methods for test B. Then order the execution of the test methods. But we know JUnit (but TestNG does) does not allow test methods execution ordering, see SO question selenium-junit-tests-how-do-i-run-tests-within-a-test-in-sequential-order

  2. Grouping all the tests (for page A, page B, & so on) under one test method. But I've read it's bad, see SO question: junit-one-test-case-per-method-or-multiple-test-cases-per-method. Is it that bad when doing selenium test? I've seen some code doing it so I assume it may not be.

  3. Grouping all the tests (for page A, page B, & so on) under one test method but use the JUnit's ErrorCollector class: ErrorCollector allows you to execute ordered checks in the same m

Edit
Report

1 Answer

2

You can implement your own Runner that will probably wrap other Runner and sort tests according to criteria yo9u defined. This is if you really need this.

But I do not think it is so necessary. I understand that if page A does not work the pass to B will not work too. So you want to run the test A first and then run test A->B. But does it really make sense? If for example test A->B runs first and fails because it cannot arrive to page A other test that verifies test A will fail too. So, Both tests will fail and id does not depend on the tests order.

If however you mean that you want to use test A as a set up operation of test B it is very bad practice. You can use the logic of test A as a start of test B but you should not couple between 2 tests. One obvious reason is that this makes debugging very hard. To debug test A->B you have to run both A and A->B tests that means that you probably have to run all tests (at least within one test case).

answered 2012-09-13T10:56:58.513

Your Answer