For my internship, I have to use TestNG and selenium for testing a web-application. But I have a problem, sometimes selenium or the Browser is not working for some random reason
The other answers are the "right" way. For a "quick'n'dirty" way just move the actual test code to a private method. In your annotated test method, make a for-loop with a try..catch inside where you look for Throwable (superclass of all errors and exceptions). On error just continue loop or throw the last error, on success break loop.
Sample code:
@Test
public void testA() throws Throwable {
Throwable err = null;
for (int i=0; i<4; i++) {
try {
testImplementationA();
return;
} catch (Throwable e) {
err = e;
continue;
}
}
throw err;
}
private void testImplementationA()
{
// put test code here
}
Usually, though, it's better to write tests that do not fail randomly. Also using this approach you don't have information about how many tries failed, which is an important information after all. I personally rather re-run the whole test class/suite on Jenkins on failure, and investigate why it failed.