seleniumselenium-webdrivertestng

How to run TestNG tests one-by-one


I have a test suite and I want to run tests form XML file one-by-one. But when I'm running it, it runs all TCs at the same time (I have 3 open browsers at the same time).

My XML:

    <suite name="TestingSuite" preserve-order="true" parallel="false" verbose="10">
      <test name="Test1">
         <classes>
            <class name="guiAndFunctianal.LoginFail" />
            <class name="guiAndFunctianal.LoginAsManager" />
            <class name="guiAndFunctianal.CreateUserTest" />        
          </classes>
      </test>
    </suite>  

My TCs looks like this:

    public class LoginFail extends AbstractTest{ 
    @ BeforeTest 
    public void openBrowser() {
     openBrowserFireFoxAllTcs();
     } 
    @ Test
     public void main (){   
    }
    @ AfterTest 
    public void quit() { 
    driver.quit(); } 

AbstractTest

    public class AbstractTest {
     WebDriver driver = new FirefoxDriver();
     WebDriverWait wait = new WebDriverWait(driver, 10);
     public void openBrowserFireFoxAllTcs() { 
    driver.get("some URL"); 
    } 

Solution

  • I assume you want sequential execution. Open a browser>finish testing>close browser and then repeat the same for other browsers. If that's case I would do the following:

    TestNG.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" junit="false" parallel="false" configfailurepolicy="skip" thread-count="5" skipfailedinvocationcounts="false" data-provider-thread-count="10" group-by-instances="false" preserve-order="true" allow-return-values="false">
       <test name="firefox" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
          <parameter name="browser" value="firefox" />
          <classes>
             <class name="com.github.tests.GitHubHomePageTests" />
          </classes>
       </test>
       <test name="ie" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
          <parameter name="browser" value="ie" />
          <classes>
             <class name="com.github.tests.GitHubHomePageTests" />
          </classes>
       </test>
       <test name="chrome" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
          <parameter name="browser" value="chrome" />
          <classes>
             <class name="com.github.tests.GitHubHomePageTests" />
          </classes>
       </test>
    </suite>
    

    How I instantiate the drivers:

    package com.github.tests;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.annotations.*;
    public class BaseTest {
        public WebDriver driver;
        String url = "https://github.com/";
    
        @Parameters("browser")
        @BeforeClass
        public void SetUpTests(String browser) {
            if (browser.equalsIgnoreCase("firefox")) {
                driver = new FirefoxDriver();
            } else if (browser.equalsIgnoreCase("ie")) {
                System.setProperty("webdriver.ie.driver", ".\\drivers\\IEDriverServer.exe");
                driver = new InternetExplorerDriver();
            } else if (browser.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
                driver = new ChromeDriver();
            }
            //Navigate to url
            driver.navigate().to(url);
            //Maximize the browser window
            driver.manage().window().maximize();
        }
        @AfterClass
        public void CleanUpDriver() throws Exception {
            // Quit current driver instance.
            try {
                driver.quit();
            } catch (Exception ex) {
                throw ex;
            }
        }
    

    How the test run

    package com.github.tests;
    import com.github.pageobjects.GitHubLandingPage;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    public class GitHubHomePageTests extends BaseTest {@Test
        public void ExploreLinkTest() {
            String explorePageTitle = new GitHubLandingPage(driver)
                .clickGitHubExplorePage()
                .getGitHubExplorerPageTitle().trim();
            System.out.println(explorePageTitle);
            Assert.assertEquals(explorePageTitle, "Explore GitHub");
        }
    }
    

    A global Github repo is available here