1

In my TestNG framework, I want to execute @AfterTest method to quit a Browser session by calling a method from customer Util class. I am getting an error as "Parameter 'Webdriver driver' is required by @Configuration on method TearDown but has not been marked @Optional or defined" After reading that error, I declared a parameter to AfterTest Method like below.

@AfterTest

public void TearDown(WebDriver driver){
    Util.tearDown(driver);

    }

Here is a teardown method from Util package

    public static void tearDown(WebDriver driver) {

    driver.quit();
    if (driver.toString().equals(null)) {
        logger.info(driver.toString() + " is closed ");
    }
}

Am I missing something? could you plz suggest how to fix this? thanks :)

Adding class file here

@Listeners({com.customerItestListener.CustomItestListener.class})

public class TC_CreateOnloadProposal {

WebDriver driver;

@Test

@Parameters({ "Url", "BemsID", "TransferType", "WrkTrnsfrTtle", "purpse" })

public void run(String Url, String BemsID, String TransferType, String WrkTrnsfrTtle, String purpse) {
    driver = BrowserFactory.getInstance("firefox", Url).getDriver();
    PageObjCreateExpressProposal pg_loc = PageFactory.initElements(driver, PageObjCreateExpressProposal.class);
    pg_loc.checkForElement(driver, pg_loc.Txt_Login, "set", BemsID);
    pg_loc.checkForElement(driver, pg_loc.Img_Logon, "click");
    pg_loc.checkForElement(driver, pg_loc.Lnk_onload, "click");
    pg_loc.checkForElement(driver, pg_loc.Img_CrtOrSelctPrpsl, "click");
}

@AfterTest

public void TearDown(){
    driver.quit();
}

`

nandesh kalyankar
  • 302
  • 1
  • 5
  • 23
  • I've made the below changes to AfterTest method- no luck `@AfterTest @parameters({"Webdriver driver"}) public void TearDown(WebDriver driver){ Util.tearDown(driver); }` – nandesh kalyankar Aug 17 '16 at 09:11
  • Where have you initialized the driver object? Is it in the Util class? If so remove the passing of the driver to the method. If the driver is in initialized the Test class then make it a instance variable and pass it to Util. – Grasshopper Aug 17 '16 at 09:19
  • `@AfterTest public void TearDown(){ Util.tearDown(driver); }` should work if you declared driver before – kotoj Aug 17 '16 at 09:21
  • This error you are getting is because you are not passing `driver` to your `TearDown` method. – Harish Aug 17 '16 at 09:35
  • @Grasshopper I've initialized webdriver in my class & passing as an argument to a AfterTest method. Regardless of passing an argument, I can't get to work out with `@AfterTest` method even with simple codeas below @AfterTest public void TearDown(){ driver.quit(); } – nandesh kalyankar Aug 17 '16 at 10:01
  • What is the exception you getting now? Can you add your test class to your question sow e can have a look? – Grasshopper Aug 17 '16 at 10:05
  • @Grasshopper I've added to the code to question section.I can't get to run `@AfterTest` method – nandesh kalyankar Aug 17 '16 at 10:33
  • Are you getting any exception? Can you log or print a message in the aftertest method that it is getting called. – Grasshopper Aug 17 '16 at 10:40
  • @nandeshkalyankar, As you have initialized the `driver` in the test class, there should be no problem in using it in `@AfterTest`. Why are you trying to quit the driver in `Utils` class as you are doing the same in `@AfterTest` method in test class. – k.s. Karthik Aug 17 '16 at 11:01

2 Answers2

0

First of all if you want to run your tearDown() method after every @Test method, you should use @AfterMethod instead of @AfterTest. See my answer for this question: Difference between BeforeClass and BeforeTest in TestNG

The other problem is that you want to use your Util class where the WebDriver is not initialized. The easiest way to include the @AfterTest or @AfterMethod methods in the same class where your test cases are, or if you want to store it in a separate class then create an abstract class and extend your Test Class with it. Example:

AbstractTest class

public class AbstractTest {
    protected Webdriver driver;

    @BeforeTest
    public void setUp() {
        // init your webdriver
        this.driver = ...;
    }

    @AfterTest
    public void tearDown() {
        this.driver.quit();
    }
}

Your Test Class

public class MyTests extends AbstractTest {
    @Test
    public void myCoolTest() {
        this.driver.get(...);
        // etc...
    }
}

Hope it helps.

Community
  • 1
  • 1
peetya
  • 3,578
  • 2
  • 16
  • 30
-1

The reason @AfterTest method didn't execute is because I didn't include it in xml file. I am running this test As Run as TestNG xml.

Here are the changes I made to xml file:

<?xml version="1.0" encoding="UTF-8"?>
<suite name ="ExpressModule">
<test name = "Express module Test">
    <parameter name="Url" value="http:XXXX"/>
    <parameter name="BemsID" value="95188"/>
    <parameter name="TransferType" value="Onload"/>
    <parameter name="WrkTrnsfrTtle" value="Onload"/>
    <parameter name="purpse" value="No Purpose"/>       
        <classes>
            <class name="com.wtms.ExpressProposalCreation.TC_CreateOnloadProposal"/>
            <methods>
            <include name="run"/>
            <include name="TearDown"/>
            </methods>
            </classes>
    </test>
</suite>
Laurel
  • 5,965
  • 14
  • 31
  • 57
nandesh kalyankar
  • 302
  • 1
  • 5
  • 23