1

Selenium Java-How do I login once and run multiple test cases using the same instance of browser, instead open/close the browser for every single test.

Is there a way to run all the tests (Regression) by opening only one instance of the browser like : log in once and keep using that same instance for the rest of the test cases.

I do not want to close the browser for each and every single test case and reopen a new browser and log in again for new test.

1

1 Answer 1

1

Create BaseClass with

1.BeforeSuite annoation with 1.1 selenium session method 1.2 login method

2.AfterSuite annotation with 2.1 closing driver 2.2 logout method

3.No annotations generic methods for reporting,waits,logging etc

4.TestClass extending BaseClass with tests and different annotations required.

public class BaseClass {

    Webdriver driver;
    @BeforeSuite
    public void setUp() {
        startSeleniumSession();
        // other initialization like reporting,logger and globals required
        loginApplication();
        logoutApplication();
    }

    public void startSeleniumSession(){
        driver=new ChromeDriver()
    }

    // Other generic methods
    public void loginApplication(){

    }

    // Other generic methods
    public void logoutApplication(){

    }

    @AfterSuite
    public void tearDown() {
        driver.close
        logout();
    }
}


public class TestClass1 extends BaseClass{

    @BeforeTest
    public void prerquisiteClass1(){
        // Run before every test in class
    }


    @Test
    public void class1Test1(){
        // functional test 1
    }

    @Test
    public void class1Test2(){
        // functional test 2
    }

    @AfterTest(){
        // take screenshot on failure for class 1 tests
        // this will run for all tests in class
    }



}


public class TestClass2 extends BaseClass{

    @BeforeTest
    public void prerquisiteClass2(){
        // test needs these things
    }


    @Test
    public void class2Test1(){
        // functional test 1
    }

    @Test
    public void class2Test1(){
        // functional test 2
    }

    @AfterTest(){
        // take screenshot on failure for class 2 tests
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Amit Jain, I will try this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.