I want to know how run multiple files without running the before class() again and again.

E.g.: Consider i am writing test case for GMail

  1. login.java : Which contains complete testing of the login page
  2. inbox.java : Which contains complete testing of the inbox
  3. spam.java : Which contains complete testing of the spam folder page
  4. label.java : Which contains complete testing of create/edit/deleting of labels

Now every file contains a before class which has

  1. calling the browser
  2. input username and password
  3. clicking the submit button and after class which logs out

Using TestNG when I run, every time it will login, test for the particular file and logs out

It needs to run like this: It should login, then test the inbox, spam and label then logs out.

Is it the right way to execute? Or is there a solution for this?

4 Answers 4


You may try this-

public class GmailContentTesting
    public WebDriver driver;
    public String baseUrl ="http://www.gmail.com";

    public void setup()
        /*System.setProperty("webdriver.chrome.driver", "Browsers/chromedriver.exe");
        driver = new ChromeDriver();*/
        driver =new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    public void gmailLogin()
        WebElement EmailTextbox= driver.findElement(By.xpath("//input[contains(@id,'Email')]"));
        EmailTextbox.sendKeys("your email");

        WebElement nextButton =driver.findElement(By.xpath("//input[contains(@id,'next')]"));

        WebElement PasswdTextbox =driver.findElement(By.xpath("//input[contains(@id,'Passwd')]"));
        PasswdTextbox.sendKeys("Your Passwd");

        WebElement SigninButton =driver.findElement(By.xpath("//input[contains(@id,'signIn')]")); 
        public void inboxTesting()
                  Write your code for complete testing of the inbox

        public void spamTesting()
                  Write your code for complete testing of the spam folder page

        public void lableTesting()
                  Write your code for  complete testing of create/edit/deleting of labels

        public void Logout()
                  Write your code for  Gmail Logout.
  • Could you please explain how your code prevents the BeforeTest and AfterTest routines from running before and after each Test routine?
    – Kate Paulk
    Mar 28, 2016 at 11:24

You can try using @BeforeSuite

You have to group your test classes into a suite in the testng.xml and annotate the setup method in one of your test classes.

This link provides a nice tutorial: http://examples.javacodegeeks.com/enterprise-java/testng/testng-beforesuite-example/


You can do it as:

  1. Have a static driver inside the test class; static WebDriver driver = null;
  2. Create a single @BeforeClass, which will initialize the driver to your desired browser, navigate to gmail and login.
  3. Have @Test annotations for all the 4 cases.
  4. Remove/comment out all other tags from those individual classes. Just have them as methods. Then call those methods from inside your @Test methods.
  5. Finally have a @AfterClass to logout.

You can use something like this in your script class ;

public class gmailTest extends BaseTest
    public void loginLogoutTest()

public void inboxTest()
    log.info("Executing inboxTest");
            //Your code here
    log.info("loginToApplication has been Successfully Excecuted");
public void spamTest()
        log.info("Executing spamTest");
            //Your code here
        log.info("spamTest has been Successfully Excecuted");


  1. Now call it where ever required
  2. Here we have only one @test so your gmail login will login one time from @beforetest and logout once from @aftertest.

Hope it help and revert if still more doubts.

  • i am using it in my script and working as expected so can try ,it will work and will reduce the code as we can reuse the codes here Aug 10, 2018 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.