2

This is a interview question which took my wicket. We have a selenium test class with 5,6 selenium tests.

Test 3 is in execution, and network failure happened. Now once network resumes back, how to configure framework so that it should start from where it has failed, like from 3rd test. And should execute the already executed test 1 & 2. Assuming framework is junit.

halfer
  • 18,701
  • 13
  • 79
  • 158
Mandar Kale
  • 317
  • 1
  • 13

1 Answers1

3

I can't agree with javaguy on his answer. I think that the question is perfectly valid. There is a big difference between unit tests as a concept and JUnit as a tool.

What was described in javaguy's answer is unit tests. These indeed can't use anything but load the class into memory and write. But certainly Tests that are using Selenium web driver are not unit tests. And its valid to chose a JUnit as a framework for implementation of these tests. Of course we can argue whether the JUnit is the best tool for that, but its a different story, bottom line if JUnit was chosen as an implementation tool for these tests, this is a valid choice.

Now regarding your question. There are many different ways to achieve what you want.

  1. Create a JUnit Rule that will allow to Rerun the test. In general Rules are somewhat like interceptors or aspects if you're familiar with AOP terminology. They intercept the test, so that it fails they can rerun it. Here you can find a question that explains the rules and provides some links.

  2. Implement a custom runner. This approach will work only if you're not using other runners because JUnit provides only one slot for runner. This custom runner will again intercept the test calls and will re-run upon network failure/ping the network and wait and only when it will 'come back' will rerun. Then you can use @RunWith annotation to activate your actual runner on test.

  3. Applicative solution. Assuming, selenium web driver is the only thing that is actually dependent on network in your test, wrap selenium driver calls into your calls. Provide try/catch for all calls in selenium and when you catch the exception - retry. You can even use aspects for this or implement the wrapper manually.

  4. There are also some solutions at the level of Jenkins/your build tool, but I think these are out of scope actually.

halfer
  • 18,701
  • 13
  • 79
  • 158
Mark Bramnik
  • 31,144
  • 4
  • 41
  • 69