Nandy
Nandy

Reputation: 11

How to reuse the drivers in testNG parallel execution

I am new to TestNG. I am able to do parallel testing with BeforeMethod and AfterMethod class where I am opening new driver and closing the same respectively.

Below is my code:

@BeforeMethod
public void run() {
    driver.get().get(url);
    driver.get().manage().window().maximize();
    driver.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

    Demo_Page objDemoPage = new Demo_Page(driver.get(), prop, date);
    objDemoPage.admin_demo("local");
    Home_page objHomePage = new Home_page(driver.get(), prop, date);
    objHomePage.pwd_change_later();
}

public WebDriver getDriver() {

    return driver.get();

}

@AfterMethod
public void tearDown() {

    getDriver().quit();
}

@Test(priority = 1)
public void Testcase1(String switch_browser, String domain_drop, String mode, String 
domain, String first_name,
        String last_name, String login_name, String emp_id, String pwd, String 
conf_pwd, String simp_name,
        String case_type, String manage_tab, String create_user, String account_tab, 
String OU, String button_id,
        String protect) {
    WebDriver driver = getDriver();
    XXX
    XXX
    XXX
 }

Similarly, I have N number of @Test methods. And my testNG will look like

<suite name="Suite" parallel="tests" thread-count="2">
<listeners>
    <listener class-name="listeners3.Proj_Listener" />
</listeners>

followed by Test tags.

Here, each time, the driver is launched and corresponding tests are run and then the driver is closed. But, I want the thread-count times of browser to be launched for the very first time, and remaining test cases to be run on the same browsers(same thread whichever is available) instead of closing and launching again.

Could anyone please help?

Upvotes: 1

Views: 331

Answers (1)

Faiz
Faiz

Reputation: 250

Add this below method with the annotation to the test class.

             @BeforeClass  
             public void before_class()  
             {  
                 System.out.println("This method is executed before Class1 and class1 has your above test methods");  
                 //Declare the driver at the class level : public WebDriver driver;
                 driver = new XXXDriver();
                 //now this object will be available to you across all the tests in the class
             }  

Upvotes: 0

Related Questions