2
votes

I am having an issue with TestNG Selenium Webdriver 2.0, and Java. I set breakpoints and saw the strangest behavior.
I have several class files containing groups of tests. Each class begins with initializing some variables global to all tests in the class, including a call to another class which initializes the webdriver. Next is a @BeforeClass, and next are my @Test tests. I am running the classes from a testng.xml file.

On debugging an issue lately I found that at runtime, testNG does the following:

  1. Initialize the global variables and webdriver in class1
  2. Then skips over to the top of class2 and does the same
  3. Then skips back to class1 @BeforeClass
  4. Then runs the tests in class1 5 then skips back to class2

@BeforeClass and finishes from there...

Why would testNG behave this way. I have tried stepping through but testNG is compiled code so I can't figure out why it does not finish with class1, before step 2 above. Initializing the webdriver in class2 right after the webdriver in class1 creates an odd problem that I cannot do a driver.close() at the end of class1 without closing the driver of class2. And since class2 has already had its global variables and its webdriver initialized, when testNG finally moves back to class2 after class1 tests are finished, its webdriver initialization is ignored. Also at runtime I can see one webbrowser open up to one path (for class1) then go to another path (for class2). It's just not right. Any ideas why testNG is running in such an order?

2
Because TestNG does not guarantee the order of anything, except for tests, when they have @dependsOnMethods or @dependsOnGroups annotation. Would a solution to have a driver instance for each class?Nathan Merrill
Well actually you can control the order of class runs pretty easily with an testng.xml file, with suite (outer tags), classes (middle-tags), and even test cases (inner tags) and it will respect the order of your xml. Of course putting all your test method names in xml is a maintenance nightmare, but the classes are easily managed...M_Tech
Anyway, when testng starts running a classes test methods, it runs through them all before moving on to another classes tests. But why in the world it would open the test class, initialize the variables, then move to the next test class, initialize the variables, then move back to the first class and run the tests is beyond me. I can't imagine this is how testng is supposed to work. Something must be broken yes?M_Tech
In such situations, it might be helpful to just replace the testng jar or get latest from testng.orgAkbar
So just curious if you are running tests from multiple classes, is your initialization run on class 1 then on class 2 then the tests in class 1 run as with mine? If you set breakpoints is this what is actually happening for you? I have a feeling there is an XML configuration to avoid this but I haven't figured it out...M_Tech

2 Answers

0
votes

It turns out that the problem causing testNG to skip between classes was that I was initializing variables, classes, etc at the class level and not within a method (@Test). You can declare NULL objects but may not initialize them to any values anywhere except within a method. This includes the webdriver! So basically a setup method, first one run in the class is needed for any say variables that will need to be scoped to the class. Hope this helps somebody save some time. Thanks- JR.

-1
votes
you can prioritize that runs in sequence in TESTNG:

you can define in annotations simply,


@Test(priority=0) // this will run first test
public void methdone()
{
}

@Test(priority=1)
public void methodtwo()
{
}
@Test(priority=2) // this will run as third test,
public void methodthird()
{
}
hope it helps !!