0

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

3

You may try this-

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

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

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


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


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

        @Test(priority=2)
        public void spamTesting()
        {
                  Write your code for complete testing of the spam folder page
        }


        @Test(priority=3)
        public void lableTesting()
        {
                  Write your code for  complete testing of create/edit/deleting of labels
        }

    @AfterTest
        public void Logout()
        {
                  Write your code for  Gmail Logout.
        }
1
  • 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
2

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/

0

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.
0

You can use something like this in your script class ;

public class gmailTest extends BaseTest
{
    @Test
    public void loginLogoutTest()
    {
        inboxTest();
        spamTest();
    }

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.

1
  • 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 that you have read and understand our privacy policy and code of conduct.

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