Java – TestNG runs all class methods multiple times and initially executes @ beforeclass using the provided data
I am writing a test suite for webapp using TestNG and selenium webdriver
What I need is to run all the methods of the test class multiple times for different authenticated users (data pulled out at run time: from the database) Therefore, I need to pass the data to the class with the data provider, which provides the credentials of all users, then create a web driver for each user (for the sake of experimental purity), authenticate with the credentials provided in the @ beforeclass method, run all @ test methods of this class, and make @ afterclass dismantle and close the web driver
How can I do this?
I'm trying to do this here: https://gist.github.com/4246674
Attempt1 is to place a @ test (DataProvider = "getusersidandname") in the class It doesn't work because it turns out that @ test (DataProvider =) on a class doesn't mean that all methods of the class should be called with the supplied data The result is that the method is not called at all because no data is passed to its parameters I got a strange test report in which this method was marked to pass in an execution time of 0 seconds (this is the Maven project opened in NetBeans, if it makes sense.)
Does @ test (DataProvider =) on this class mean anything?
Attempt2 is to add DataProvider on the @ test annotation of the method It doesn't work because it means calling the test method twice during a single class run As a result, it failed the second time because it tried to repeat the test and the web driver did not reinitialize: the browser had rendered another page
The disadvantage of the first and second attempts is that the login itself is completed in the test method, not in the @ beforeclass method, because I know I can't provide it with the data provider's data
Attempt3 is a desperate attempt to combine @ beforeclass and @ test on a single method It doesn't work because @ bestclass also needs @ parameters if the annotated method has a parameter list The result is that this method is called three times: for the above reasons, the first setting as a class fails, and when successful, it fails twice due to the data provider
Then I found that TestNG does not provide a method to call all class methods multiple times with different data: http://comments.gmane.org/gmane.comp.java.testng.user/864
I have two more ideas on how to archive the same effect
Idea4 is to place a @ test (DataProvider =) on each test method. Each method will perform its own authentication and isauthenticated () tests, and then execute its own tests Invalid and ugly
It seems that the most affordable idea5 is to use @ test to mark a method to aggregate all other test methods: it receives data from the data provider, performs authentication by a given user, and then runs isauthenticated tests and all other necessary tests In this case, @ beforeclass and @ afterclass just set up and remove the web driver The disadvantage of this method is that I lose the ability to use @ beforemethod and @ aftermethod, but it's affordable for me: it seems that I don't need them
Is there a better way to archive the same effect?
Sorry, the message is long
Thank you in advance
Solution
You can try to explore factories to meet your needs Factory allows you to run all the @ test annotated methods of classes that use different data In this case, you can initialize ur webdriver in ur beforeclass Explain here