Reputation: 31
I am running a Selenium test and I'm trying to generate a different random String for each of my test scenarios, but I keep getting the same String.
Here is an example of what I'm running:
String randomString = RandomStringUtils.randomAlphabetic(8);
@Test(priority = 1)
private void testScenario_1(){
System.out.println(randomString);
}
@Test(priority = 2)
private void testScenario_2(){
System.out.println(randomString);
}
Upvotes: 1
Views: 323
Reputation: 1734
Another possible method would be to generate the random string in a @BeforeMethod
annotated method.
String randomString = "";
@BeforeMethod
private void init() {
randomString = RandomStringUtils.randomAlphabetic(8);
}
@Test(priority = 1)
private void testScenario_1() {
System.out.println(randomString);
}
@Test(priority = 2)
private void testScenario_2() {
System.out.println(randomString);
}
The method init
will be called before each test method. One might think this is not needed in this case but if you have to do more preparations this is the way to go. You will reduce duplicate code.
Upvotes: 2
Reputation: 509
The same randomeString value is referred to in both of the tests. Move the generation of randomAlphabetic inside the test as below.
String randomString = "";
@Test(priority = 1)
private void testScenario_1(){
randomString = RandomStringUtils.randomAlphabetic(8);
System.out.println(randomString);
}
@Test(priority = 2)
private void testScenario_2(){
randomString = RandomStringUtils.randomAlphabetic(8);
System.out.println(randomString);
}
Upvotes: 0