0
votes

Hello all I have a TestNG test suite which executes tests sequentially. But before it starts running tests. It opens the required number of browsers and then run test one after other . I want to change this behaviour to open one browser, run test/s and close the browser. Following from that open another browser, run test/s and close and so on. Is this possible?

I use TestNG, JAVA on IntelliJ. Sample test suite:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" parallel="false" preserve-order="true" thread-count="1">
    <test name="something1" group-by-instances="true">
        <classes>
            <class name="com.this.is.First"/>
        </classes>
    </test>
    <test name="something2" group-by-instances="true">
        <classes>
            <class name="com.this.is.Second"/>
        </classes>
    </test>
    <test name="something3" group-by-instances="true">
        <classes>
            <class name="com.this.is.Third"/>
        </classes>
</suite>

All the test inherit Main class which has @BeforeClass and @AfterClass methods where I instantiate the browser.

Each Class can have multiple tests or sometimes only one.

Main class as requested:

public class MainBeforeAfter
{
  protected WebDriver driver;
  protected String testUrl = "someurl";

  public MainBeforeAfter()
  {
    if (System.getProperty("webdriver.chrome.driver") == null)
    {
      System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe");
    }
    if (System.getProperty("test.url") != null)
    {
      testUrl = System.getProperty("test.url");
    }
    System.out.println(System.getProperty("webdriver.chrome.driver"));
    ChromeOptions options = new ChromeOptions();
    options.addArguments("headless");
    options.addArguments("window-size=1200x600");
    driver = new ChromeDriver(options);
    //driver = new ChromeDriver();

  }
  @BeforeClass public void beforeClass() throws InterruptedException
  {
    driver.get(testUrl);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    enterLogin();
  }
  protected void enterLogin()
  {
    driver.findElement(By.name("username")).sendKeys("user");
    driver.findElement(By.name("password")).sendKeys("password");
    driver.findElement(By.name("password")).sendKeys(Keys.ENTER);

  }
  @AfterClass public void afterClass() throws InterruptedException
  {
    driver.close();

    try
    {
      driver.quit();
    }
    catch (Exception e)
    {
      System.out.println("Unable to close browser after login header exception caught: " + e);
    }

  }
}
3
provide main class code, it will helps to find issuemurali selenium
please find the main class code above. ThanksPashN
you can use singleton class as per my provided answermurali selenium

3 Answers

1
votes

Try like this:

This should do the trick, just inside tag name following classes

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" preserve-order="true">
    <test name="something1">
        <classes>
            <class name="com.this.is.First"/>
            <class name="com.this.is.Second"/>
            <class name="com.this.is.Third"/>
        </classes>
</suite>

hope it helps,

0
votes

You have to use singleton here, try like this

  public class MainBeforeAfter {

         public static WebDriver driver;
         public static  String testUrl = "http://www.urlhere.com";

    public static WebDriver getInstance()
    {
      if (System.getProperty("webdriver.chrome.driver") == null)
      {
        System.setProperty("webdriver.chrome.driver", "E:\\gita_workspace\\GitaProject\\drivers\\chromedriver.exe");
      }
      if (System.getProperty("test.url") != null)
      {
        testUrl = System.getProperty("test.url");
      }
      System.out.println(System.getProperty("webdriver.chrome.driver"));
      ChromeOptions options = new ChromeOptions();
      //options.addArguments("headless");
      options.addArguments("window-size=1200x600");
      return driver = new ChromeDriver(options);

    }
    @BeforeClass 
    public void beforeClass() throws InterruptedException
    {
     driver = MainBeforeAfter.getInstance();
      driver.get(testUrl);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.manage().window().maximize();
     // enterLogin();
    }
    protected void enterLogin()
    {
      driver.findElement(By.name("username")).sendKeys("user");
      driver.findElement(By.name("password")).sendKeys("password");
      driver.findElement(By.name("password")).sendKeys(Keys.ENTER);

    }
    @AfterClass 
    public void afterClass() throws InterruptedException
    {
      driver.close();

      try
      {
        driver.quit();
      }
      catch (Exception e)
      {
        System.out.println("Unable to close browser after login header exception caught: " + e);
      }

    }

   }
0
votes

just add

@BeforeMethod

annotation on getInstance() method and modify it as:

   @BeforeMethod
   public static WebDriver getInstance() {
    if (System.getProperty("webdriver.chrome.driver") == null) {
        System.setProperty("webdriver.chrome.driver", "E:\\gita_workspace\\GitaProject\\drivers\\chromedriver.exe");
    }
    if (System.getProperty("test.url") != null) {
        testUrl = System.getProperty("test.url");
    }
    System.out.println(System.getProperty("webdriver.chrome.driver"));
    ChromeOptions options = new ChromeOptions();
    // options.addArguments("headless");
    options.addArguments("window-size=1200x600");
    driver = new ChromeDriver(options);
    driver.get(testUrl);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

In @BeforeClass do other things if you want(in this case there is no need ) and then add in class below method

@AfterMethod
public void tearDown() {
driver.quit();
}

It will open browser at one time then after execution completion close the browser. This will happen for all test cases sequentially. Driver will be created each time and will be destroyed each time when test execution is complete. I do not know if it is good approach but it will solve your problem:)

Please do let me know if that worked for you.