Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable in @BeforeTest and @AfterClass (TestNG) across separate classes?

I am writing some selenium automated UI tests for my company using Java and the TestNG framework. I am defining the driver in a Base class, and I want to actually initialize the driver in an @BeforeTest and quit it in a @AfterTest method. What is the Java way to do that, assuming they are in different classes? I know how to make it work in the same class, but not over separate classes. Here is my Base.java file:

public class Base {

        public static WebDriver driver = null;
        public WebDriver getDriver() {
            driver = new ChromeDriver();
            return driver;
        }
}

Now, I want to have a separate Setup class and a separate Teardown class. If I was going to define all of this in the same @Test, I would do it this way:

@Test
public void testOne() {

    Base b = new Base();
    WebDriver driver = b.getDriver();

    // Do test-y things here. 

    driver.quit();
}

How would I set this up? Trying to learn the right way to do this, and not hack something together. I can also provide more information if needed. Thanks!

like image 607
kroe761 Avatar asked Feb 01 '17 19:02

kroe761


People also ask

What is the Use of alwaysRun attribute in TestNG?

alwaysRun : This is used when we want to make sure a method always runs even if the parameters on which the method depends, fails. If set to true, this test method will always run. Eg: @Test(alwaysRun = true) dataProvider: TestNG dataProvider is used to provide any data for parameterization.

What is the difference between @BeforeClass and @BeforeTest in TestNG?

@BeforeTest method executes only once before the first @Test method. @BeforeClass executes before each class. If there are separate @BeforeTest and @BeforeClass methods in different classes, then all the @BeforeTest methods will execute first but @BeforeClass methods will be executing as per the respective classes.

What is the use of @BeforeTest annotation?

@BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case. @AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG. xml file.

What is before class in TestNG?

@BeforeClass annotated method will be run before the first test method in the current class is invoked. For before methods (BeforeClass, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.


1 Answers

Use inheritance.

public class TestBase {

    protected WebDriver driver;

    @BeforeClass
    public void setUp(){
        System.out.println("I am in setUp method.");

        //WebDriver instantiation etc.
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @AfterClass
    public void tearDown(){
        System.out.println("I am in tearDown method.");

        //You can clean up after tests.
        driver.close();
    }
}

And then inheritance can be used. Pay attention to the extends keyword:

public class ParticularTest extends TestBase {

   @Test
   public void testMethod() {
       System.out.println("I am in testMethod.");

       //Your driver from TestBase is accessible here.
       //Your assertions come here.
   }
}

Later on you can just execute ParticularTest.java. Output:

I am in setUp method.
I am in testMethod.
I am in tearDown method.
like image 56
Grzegorz Górkiewicz Avatar answered Sep 22 '22 05:09

Grzegorz Górkiewicz