In real world you will not be running single test cases. You have to run it as a suite
Imagine the below scenario, you have been asked to write multiple test cases.
- open google.com
- open bing.com
- open yahoo.com
package seleniumWithTestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class OpenGoogle {
@Test
public void openGoogle(){
long startTime = System.currentTimeMillis();
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
driver.quit();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime/1000.0+" seconds");
}
@Test
public void openBing(){
long startTime = System.currentTimeMillis();
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.bing.com");
driver.quit();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime/1000.0+" seconds");
}
@Test
public void openYahoo(){
long startTime = System.currentTimeMillis();
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.yahoo.com");
driver.quit();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime/1000.0+" seconds");
}
}
Time taken to execute the above mentioned three test cases: 16.172 seconds + 23.983 seconds + 77.384 seconds = 117.539 seconds.
More the count of the test cases, more the time taken. But Time complexity is one very critical factor in IT industry. We have to reduce the time taken to execute the test cases, without affecting the functionality. This has become a huge challenge. Let's analyze where can we reduce the time. If you observe, we are opening and closing the browser for each and every test cases, which is a time consuming process. If we make the program to launch only one browser instance and hit all the three URLs, we can save time.
Let's rewrite the above program to run it as a suite
package seleniumWithTestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class OpenGoogle {
WebDriver driver;
long startTime;
long endTime;
long totalTime;
@BeforeSuite
public void launchBrowser(){
startTime = System.currentTimeMillis();
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void openGoogle(){
driver.get("http://www.google.com");
}
@Test
public void openBing(){
driver.get("http://www.bing.com");
}
@Test
public void openYahoo(){
driver.get("http://www.yahoo.com");
}
@AfterSuite
public void closeBrowser(){
driver.quit();
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println(totalTime/1000.0+" seconds");
}
}
Time taken to run this program is 41.302 seconds
PASSED: openBing
PASSED: openGoogle
PASSED: openYahoo
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
1498992248213 Marionette INFO New connections will no longer be accepted
41.302 seconds
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Time taken in 1st program: 117.539 seconds.
Time taken in 2nd program: 41.302 seconds.
Difference: 76.237 seconds
So, for these three simple test cases, we have saved around 76 seonds. Imagine the benefit while running hunderds of test cases.
How we have done this?
TestNG has multiple annotations. BeforeSuite and AfterSuite are one among them. What we have done here is, we have identfied the functionalities that has to be executed before the test cases and after the test cases. So as we discussed earlier, we need not to open and close multiple browsers. Hence we are giving the browser opening logic in BeforeSuite and closing the browser in AfterSuite. By doing so, only one browser is opened and Bing, Google, Yahoo were hit respevtively.