frass
frass

Reputation: 125

Multiple JUnit test in one browser session

I’ve written a program in selenium webdriver but for my next project I would like to make it more maintainable by using better programming techniques. The main part I want to focus on is launching the browser once (1 session) and run say 10 different test then close the browser but I’m not sure how to do this. Using JUnit this is how I currently have my project laid out :

package example1;
public class TestBase { //main class

@Before
public void setup () {
//launch browser
}

@Test //all test run here
public void test1(){
login();
homepage();
}

@After
public void teardown(){
//close browser
}
}

package example1;
public class login(){
//do some action
}

package example1;
public class homepage(){
//do some action
}

package example1;
public class storeMethods(){
//all methods are stored which are then called by different classes
}

I’m not sure if the @Test annotation should even be in the main class or if it should be in its own class (login(), homepage()) because I read somewhere that test should not depend on each other. I don’t have much experience in java but I’m more than willing to learn. I just need some guidance on best practices and how to write good maintainable test so if someone could help me out or point me in the right direction then I’d really appreciate it.

Upvotes: 0

Views: 1392

Answers (2)

Nathan Merrill
Nathan Merrill

Reputation: 8396

While what Robbie Wareham said is correct, reusing the browser is not a good idea, you said that your overall goal is maintainability.

The techniques I've found to increase maintainability is the Page Object pattern with separate functions to interact with it.

The Page Object pattern separates the selector from the rest of the code. That way, if an element on a page changes, and your tests uses that element 5 times...you only change your code in 1 spot. It is also standard to include isLoaded(), which is a function that can be used to identify if you are already on the page you need so you don't reload the page.

I would also recommend having your test not directly deal with that Page you created. If you had a toolbar that you had to use to go to X page...and then the toolbar changed so the link you wanted was in a sub-menu, then every time in your tests you used that link, you would have to change the method to click on that link. Creating sets of selenium commands that interact with the page will make your tests high-level and easy to read.

Upvotes: 1

Robbie Wareham
Robbie Wareham

Reputation: 3448

I would suggest that reusing the browser is not following better automation programming practice.

Reusing the browser will result in unstable and unreliable tests, with inter-test dependencies.

In my opinion, it is far better to have atomic self contained tests.

If test runtime is an issue, then look at parallelism and using selenium grid

Upvotes: 1

Related Questions