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

  1. Open Eclipse IDE

    1. Click on the Help menu

    2. Click on Install New Software

  2. The window will popup

    1. Click on the Add button

    2. In the name input box, type “TestNG”

    3. In the location input box, type url “http://beust.com/eclipse

    4. Click on Add

  3. The window will open with TestNG result

    1. Select TestNG

    2. Click on Next 

    3. Accept the Licence agreement

    4. Click on Finish

  4. If you encounter a Security warning, just click "Install Anyway".

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

  1. Right-click on project > Properties > Java build path

  2. Clicks on Library tab > Add Library

  3. One window popup will display and select TestNG

  4. Click on next & then Finish.

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

  1. The main advantage of properties is that they are outside our source code and you can change them anytime.

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

  1. 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“.

  2. Write the following content in properties file in key: value pair format:
    browser=chrome

url=”www.amazon.com”

How to read config.properties file:

  1. First we have to create an object of Properties class.

Properties prop=new Properties(); // This class is available in java

  1.  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”);

  1.  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);

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

Popular posts from this blog

For Get Dynamic data & USe it into Postman Body parameters

API Automation Testing Script to check Response

Login through Data Driven Testing in selenium

Which dependencies needed in pom.xml while doing automation through selenium?

Download Java(JDK) & Eclipse IDE for selenium

What is data driven testing?

How to extract all the links in selenium?