Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Selenium by (7k points)
edited by

I have following conditions

Different base urls for each test class.

Same login credentials for each base url.

Different data sets for each test class.

Problem:

It passes AMSValidation but skips SAPValidation. It says

the firefoxdriver cannot be used after quit was called()

 But while running tests per class, they pass.

My code:

public abstract class Validation {

    // other variables

    public static WebDriver driver = new FirefoxDriver();

    public void doLogin() throws Exception {

        // something

    }

    public void validateDocuments(String portal, String navigation,

            String category, String title, String fileName) throws Exception {

        // driver is used here      

    }

}

public class AMSValidation extends Validation {

    @Parameters("baseUrl")

    @BeforeTest(alwaysRun = true)

    public void setUp(@Optional("https://website.com/ams/") String baseUrl)

            throws Exception {

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get(baseUrl);

        doLogin();

    }

    @DataProvider

    public Object[][] getDataForAMS() throws Exception {

        // return test data for ams

    }

    @Test(dataProvider = "getDataForAMS")

    public void validateAMS(String portal, String navigation, String category,

            String title, String fileName) throws Exception {

        validateDocuments(portal, navigation, category, title, fileName);

    }

    @AfterTest(alwaysRun = true)

    public void tearDown() throws Exception {

        driver.quit();

    }

}

public class SAPValidation extends Validation {

    @Parameters("baseUrl")

    @BeforeTest(alwaysRun = true)

    public void setUp(@Optional("https://website.com/sap/") String baseUrl)

            throws Exception {

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get(baseUrl);

        doLogin();

    }

    @DataProvider

    public Object[][] getDataForSAP() throws Exception {

        // return test data for sap

    }

    @Test(dataProvider = "getDataForSAP")

    public void validateSAP(String portal, String navigation, String category,

            String title, String fileName) throws Exception {

        validateDocuments(portal, navigation, category, title, fileName);

    }

    @AfterTest(alwaysRun = true)

    public void tearDown() throws Exception {

        driver.quit();

    }

}

testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Validation Suite" verbose="2">

    <test name="AMS Test">

        <parameter name="baseUrl" value="https://website.com/ams/" />

        <classes>

            <class name="com.website.tests.AMSValidation" />

        </classes>

    </test>

    <test name="SAP Test">

        <parameter name="baseUrl" value="https://website.com/sap/" />

        <classes>

            <class name="com.website.tests.SAPValidation" />

        </classes>

    </test>

</suite>

Log:

PASSED: validateAMS("portal", "navigation", "category", "title", "filename")

===============================================

    AMS Test

    Tests run: 1, Failures: 0, Skips: 0

===============================================

FAILED CONFIGURATION: @BeforeTest setUp("https://website.com/sap/")

org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.

// rest of stack trace

SKIPPED: validateSAP("portal", "navigation", "category", "title", "filename")

===============================================

    SAP Test

    Tests run: 1, Failures: 0, Skips: 1

    Configuration Failures: 1, Skips: 0

===============================================

===============================================

Validation Suite

Total tests run: 2, Failures: 0, Skips: 1

Configuration Failures: 1, Skips: 0

===============================================

1 Answer

0 votes
by (31.9k points)

Move the driver.quit() call to the aftersuite() method. And move driver initialization to beforesuite() method.

Want to learn Selenium, checkout our Selenium training.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...