What is the use of TestNG Library and confing.properties?
What is TestNG Framework?
TestNG is a Java testing framework for complex integrated Testing. it is designed to simplify all our testing requirements such as functional testing, regression, end-to-end testing.

Installation of TestNG
Open Eclipse IDE
Click on the Help menu
Click on Install New Software
The window will popup
Click on the Add button
In the name input box, type “TestNG”
In the location input box, type url “http://beust.com/eclipse”
Click on Add
The window will open with TestNG result
Select TestNG
Click on Next
Accept the Licence agreement
Click on Finish
If you encounter a Security warning, just click "Install Anyway".
Wait for the installation to finish. When Eclipse prompts you for a restart, click "Restart now."
Use of TestNG library
Annotations are easier to use and understand.Test cases can be grouped more easily.
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@BeforeTest: The annotated method will be run after the BeforeSuite method
@BeforeMethod: The annotated method will be run after BeforeSuite & BeforeMethod
@Test: The annotation method will be executed after Above three Methods.
@AfterMethod: The annotation method will be executed before AfterTest & AfterSuite method
@AfterTest: The annotation method will be executed before AfterSuite method
@AfterSuite: The annotation method will be run after the all annotation methods.
TestNG is capable of generating HTML-based reports.
make priorities which test case should be executed first.
The testing framework can be easily integrated with tools like Maven, Jenkins, etc.
Add TestNG in Project
Right-click on project > Properties > Java build path
Clicks on Library tab > Add Library
One window popup will display and select TestNG
Click on next & then Finish.
Click on Apply & Close & you will show the TestNG Library in project
Properties File
Properties file is a text file wherein data is stored in the form of key-value pairs.
Use of Properties File
The main advantage of properties is that they are outside our source code and you can change them anytime.
Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key), and the other storing the value.
How to create config.properties file
Open eclipse. Right click on the project, select New→ file→ give file name as “config.properties”→ Finish. We cannot write java code in this. It is a simple text file. Make sure to use extensions as “properties” and not “property“.
Write the following content in properties file in key: value pair format:
browser=chrome
url=”www.amazon.com”
How to read config.properties file:
First we have to create an object of Properties class.
Properties prop=new Properties(); // This class is available in java
Create object of FileInputStream and give property file location as fileInputStream parameter (which property file is to be read)
FileInputStream ip= new FileInputStream(“location of property file”);
Now we have to load the property file. Use properties object to load property file
prop.load(“fileInputStream object”)
Final code should look something like this:
Properties prop=new Properties();
FileInputStream ip= new FileInputStream(“home/username/PrjtName/src/config.properties”);
prop.load(ip);
Now, once the config file is loaded, we need to read the properties of the config file. Properties object gives us a .getProperty method which takes the key of the property as a parameter and returns the value of the matched key from the .properties file.
System.out.println(prop.getProperty(“name”));
How to open a Chrome browser using TestNG in Selenium?
sitedata.properties
browser=chrome
url=”www.amazon.in”
Newproject.java
public class NewClass {
public static WebDriver driver = null;
public static Properties pr=new Properties();
@BeforeTest
public static void BaseInit() throws InterruptedException, IOException {
FileInputStream fi = new FileInputStream(System.getProperty("user.dir")+"//src//Properties//sitedata.properties");
pr.load(fi);
if(pr.getProperty("browser").equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "D://eclipse//libs//chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(5000);
}
}
@Test(priority=0, description="Load URL")
public void GetUrl() throws InterruptedException{
Thread.sleep(5000);
driver.get(pr.getProperty("url"));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterTest
public void closeBrowser() {
driver.close();
}
}
Comments
Post a Comment