Sunday, July 26, 2020

Part-6: Want to quickly become an Selenium Automation Engineer and build framework right from scratch?


TestNg Configuration and implementation

In Part-5, we modified our simple automation script into a real-time framework pattern wherein we have separate folders for test-cases and helper classes. We also added a few more functionalities to our intended test and executed it further. So now let’s introduce TestNG, the most awaited framework and we will learn the use of pending locators and other framework building stuff. I assure you by the end of this course, you will be able to develop a wide variety of tests.

 

TestNG is a unit testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, in TestNG, refers to ‘Next Generation’, and it is perfectly equipped with next-generation functionalities, like annotation, report generation showing how many test-case passed or failed or skipped, control your test-cases like running only a few test-cases out of many, add priority to test-cases etc. So let’s not add more huge definition which will confuse more and directly work on it. You can find more on its official documentation.

Let’s install the TestNG plugin in our Elcipse. Open Eclipse, click Help->Install New Software


Enter the site URL as ‘https://dl.bintray.com/testng-team/testng-eclipse-release/’  in as mentioned below and hit enter, you should be able to see ‘TestNG’ as mentioned below:


Once you click on that checkbox, the ‘Next’ button will get enabled, click on it.



After clicking Finish, you might get below prompt, click ‘Install anyway’,


You can also check the progress of the installation.


Click ‘Restart now’. This will install TestNG Plugin in your Eclipse.


So just to check whether TestNG is installed, right-click your project and click ‘Properties’, you should be able to see ‘TestNG’


We will create a class which will house all our test-cases, lets name it as ‘TestClass’ under package ‘testSearch’, you know why we selected this package, because this is the package for test-cases.

As this is a TestNG based test-class, we will not use ‘public static void main(String[] args)’ for both compiling and running the class, but we will use TestNG for the same, we will see further how.


TestNg provides various helper annotations which prove very useful in test-case execution, rather team-up as an end-to-end solution for test-case execution driving. Below are some of the annotations:

No.

Tag Annotations

Comment

1.

@BeforeSuite

To be used when you have to load environment variables.

2.

@BeforeTest

To be used when annotated methods will be executed before each test section declared inside TestNG suite.

3.

@BeforeClass

@BeforeClass the annotated method is executed before any of the test methods of a test class.

4.

@BeforeMethod

@BeforeMethod annotated methods are executed before the execution of each test method for a particular class

5.

@AfterMethod

@AfterMethod annotated methods are executed after  the execution of each test method for a particular class

6.

@AfterClass

@AfterClass the annotated method is executed after the execution of every test methods of a test class.

7.

@AfterTest

@AfterTest the annotated method is executed after the execution of every test section declared inside a TestNG suite.

8.

@AfterSuite

To be used when you have to close/unload  environment variables.


Below are some of the helper attributes TestNG uses:

No.

Helper Attributes

Comment

1.

@Test(dependsOnMethods=(“A,B”)

This test-case will execute once A and B are executed.

2.

@Test(enable=false)

This will disable that particular test-case

3.

@Test(timeout=4000)

Used to increase the timeout of that particular test to 4 seconds before failing

4.

@Parameter(“{URL}”)

This is used to parameterize a test-case. Useful for that particular test only where it is mentioned. Here we mention the URL in testing.xml, eg: <parameter name=”URL” value=”www.abc.com”>

@Test

Public void getData(String str)

{

System.out.println(str);

}

5.

@DataProvider

Used to test a test-case with multiple data.

Eg:

@DataProvider

Public Object[][] getData()

{

Object[][] myObject = new Object[2][2];

myObject[0][0]=”email1”;

myObject[0][1]=”val1”;

myObject[1][0]=”email2”;

myObject[1][1]=”val2”;

}

 

@Test(dataProvider=”getData()”)


Don’t worry, we will cover all of the above with an explanation, while some are self-explanatory. 

So let's dive into our eclipse ‘TestClass’ that we created. To declare any test in TestNG we assign an annotation called ‘@Test’, this tells that this is a test method, and we write our test. For now, for better understanding we just convert our available test into TestNG, as below:

Here I am writing my first test, to initiate the browser, so creating a method browserInitiate(), we already required code from the previous example, so we will copy-paste that content from ‘FirstTest’ class. You can also see when we write ‘@Test’ it gave an error because it cannot recognize that tag, though your eclipse has TestNG plugin, but your project needs that TestNG dependency. 

As we are using Maven, we don’t need to do anything, just search that dependency using below:


Don't use any beta version, use a stable version:


Click on the link and copy the content:


And paste it in your pom.xml under <dependencies> tag and save the pom.xml and ‘TestClass’:


Now again go to the class where you are writing your test-case, ie., ‘TestClass’ and again hover over ‘@Test’, you will now find ‘TestNG’ a related suggestion which was not available earlier, so this came because you imported TestNG related dependency in your project


Click on it and the error is gone. Now if you right-click your TestClass->Run As, you will find ‘Run As TestNG test’ and not as ‘Java Application’:


You will find the same output as earlier, but here you will not write class for every test, but in the same class, you will write many tests and control them. Also, you if you see at the bottom in your eclipse console, you will find, it shows Status ‘PASSED’ for our test: ‘browserInitiate’, also it shows descriptive data of failures/skips, for which we have not written any code, but TestNG has provided that data.


Now as we need to plug-and-play with our tests, we need to have testng.xml file which is not available yet, so to get it right click your project-> TestNG->Convert to TestNG:


You will find below details, which are Suite Name, Test name, and class name in which you are doing below test. Click Finish[You can change the details as per your needs also]


Now in your package explorer, you will find testing.xml


Double click that file, click ‘source’ and verify the details that you entered:


Henceforth we will be using TestNG for our further part of our exciting test-case, we will use all the possible annotations in the same simple way, and I guarantee you that, you will master TestNG.

Don’t miss the next important article on TestNG annotation implementations and Cucumber available on Amazon Kindle

-Sanjay Nikalje



How to create any mathematics tables with excel...

  Generate any tables with excel... 1.        Go to excel sheet, in first cell ‘A1’ enter the number whose table you want to generate, for...