Rahil Kumar
Rahil Kumar

Reputation: 428

TestNG annotation Issue in Selenium JAVA

I have written basic TESTNG java selenium script for learning. IN below code after execution of test1 method I am expecting closing of browser as @AfterTest will run. But after Test1 method Test2 method is running . Browser is closing after execution of both @test method. Can anyone help me how to close browser after each run of @test method

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;

public class Sample {

    public String baseUrl = "https://www.facebook.com/";
    public WebDriver driver = new FirefoxDriver();
    @BeforeTest
    public void beforeTest() 
      {
      driver.get(baseUrl);      
      }
    @Test
    public void test1() 
    {
      driver.get(baseUrl);
      String expectedTitle = "Facebook - Log In or Sign Up";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
    }
    @Test
    public void test2() 
    {
      driver.get(baseUrl);
      String expectedTitle = "Facebook - Log In or Sign Up";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
    }
  @AfterTest
  public void afterTest()
  {
  driver.close();
  }

}

Upvotes: 1

Views: 1091

Answers (3)

 kumar
kumar

Reputation: 81

I have got an approach to your query. That is you can write two annotations there are @BeforeMethod and @AfterMethod. I@ Before Method you can write code to launch application and in @AfterMethod you can write query to quit the browser. When test execution starts first before test annotation will be executed after @test1 method will be executed and after @AfterMethod will be executed that mena sbrowser is closed. Next 2nd time @AfterMethod will be executed and @test2 will be executed and @AfterMethod will be executed.

Upvotes: -1

Tim Hallbeck
Tim Hallbeck

Reputation: 651

This article has a nice side by side table of the tag differences between testng and junit, and some examples of each at the bottom.

https://www.guru99.com/junit-vs-testng.html

Also - In general, you should have each test be entirely independent of every other test. That means you need to have each test create its own browser / webdriver instance. It's standard to put the driver creation in its own method, and have it return a webdriver to each test. Similarly, at the end you'll call something that has driver.quit() that you pass your webdriver to.

The reason for this is that eventually you'll get into multithreading / simultaneous execution, where you'll set the number of threads on a test by test basis.

    @Test( invocationCount = 20, threadPoolSize = 5 )

Better to prepare for this early by creating helper methods for creation & cleanup early on.

Upvotes: 0

juherr
juherr

Reputation: 5740

Be careful, TestNG is not JUnit.

JUnit @BeforeTest is translated by @BeforeMethod in TestNG world.

@BeforeTest in TestNG is before a group of test methods.

See http://testng.org/doc/documentation-main.html#annotations You can have a look on Difference between BeforeClass and BeforeTest in TestNG too.

Upvotes: 2

Related Questions