martinixs
martinixs

Reputation: 325

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?

Upvotes: 3

Views: 6774

Answers (2)

Anuragh27crony
Anuragh27crony

Reputation: 2957

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

Upvotes: 4

Andrian Durlestean
Andrian Durlestean

Reputation: 1730

If you want to run all test in one instance, i wrote my own WebDriverFactory so here is some examples for help:

    public static WebDriver getDriver(){
       if (driver == null) {
            return new FireFoxDriver();
        } else {
          return driver;
        }
   }

Now remove AfterClass and add this to your class it will shutdown your browser in the end

static {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    dismissDriver();
                } catch (Exception e) {
                }
            }
        });
}

 public static void dismissDriver() {
        if (driver != null) {
            try {
                driver.quit();
                driver = null;
            } catch (Throwable t) {
            }
        }
    }

Upvotes: 2

Related Questions