Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium and TestNG all test in one browser

I have abstract class, where initialize webdriver. All classes with the implementation of the tests are inherited from him. I want to reduce the time of test, open a browser for all tests.

class AbstractClassCase {
    public static WebDriver driver;

    @BeforeClass    
    @Parameters({"webDriver", "applicationHost", "applicationPort", "driverHost", "driverPort",  "username", "password"})
    public void setUp(
        String webDriverIdentifier,
        String applicationHost,
        @Optional String applicationPort,
        @Optional String driverHost,
        @Optional String driverPort,
        @Optional String username,
        @Optional String password){      

        driver = new FirefoxDriver();
        driver.get("localhost");
        login(username, password)

    }

    @AfterСlass
    public void tearDown() {   
        driver.quite();
    }     

}

public class TestButton extends AbstractClassCase {
    @Test
    public void testClickButtonNo() {
        WebElement button = driver.findElement(By.id("button-no"));
        button.click();
        WebElement status = driver.findElement(By.id("button-status"));
        Assert.assertEqual("Cancel", status.getText());
   }
}

and other test class in the same spirit.

How can I reconfigure this class, so that the browser opened once?

like image 758
martinixs Avatar asked Jan 27 '26 23:01

martinixs


1 Answers

If you want to Open Browser at the start once and run all the methods and then close it.

There are 2 steps i would recommend to follow.

Step 1: Include browser invocation code in methods with @BeforeSuite && @AfterSuite for closing the browser/driversession. This makes sure these tests are run once before and after test suite.

protected static WebDriver Browser_Session;

@BeforeSuite
public void beforeSuite() {
    Browser_Session=new FirefoxDriver();
    Browser_Session.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}

@AfterSuite
public void afterSuite() {
    Browser_Session.quit();
}

Step2 : Open testng.xml and include all such classes (test methods) under a single Suite, Thus making sure the browser (via Selenium) is invoked first and then rest all methods are run in the same browser. enter image description here

Here "Class Init" contains Browser Initiating code and ClassA & ClassB are subclassess of Init.

Hope this helps

like image 70
Anuragh27crony Avatar answered Jan 30 '26 11:01

Anuragh27crony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!