In our recent sessions, we reviewed essential WebDriver instructions and their frequent uses. Strategies to identify UI elements for inclusion in testing scripts were also tackled. As a result, we managed to develop our initial WebDriver Automation Test Script.
This session focuses on unveiling TestNG, its capabilities, and its applications.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
TestNG is an innovative framework built to offer value to both coders and testers. TestNG shares common features with JUnit, a popular tool in the Java community for its impact on code quality, but also includes some advanced functionalities.
Learn more here: JUnit Tutorial and how to use it in Selenium scripts
Although JUnit is simple and easy to use, it does have limitations. This is why TestNG was developed. TestNG, an open-source software engineered by Cedric Beust, offers numerous benefits and operates under the Apache Software License.
One key requirement for implementing TestNG with WebDriver is efficient and effective test outcomes formatting. Sharing these reports with important decision-makers offers an overview of the product or software’s status, tackling WebDriver’s drawback of test report generation. TestNG also features a built-in exception handling system that inhibits the application from unexpectedly stopping.
Both TestNG and JUnit belong to the same family of unit frameworks, but TestNG is an enhanced version with a broader use in present testing.
Here’s what you’ll learn
Exploring TestNG’s Features
- Annotation support
- Parameterization management
- No requirement for creating test suites and they allow an advanced execution process
- Data Driven Testing via data providers
- Setting up execution priorities for testing methods
- Supports a thread-safe environment when running multiple threads
- Seamless integration with various tools and plugins like Ant, Maven, and Eclipse
- Effective Report Generation using ReportNG
Comparing TestNG and JUnit
TestNG boasts of several benefits over JUnit including:
- Enhanced and user-friendly annotations
- Setting up the execution flow
- Simultaneous execution of test scripts
- Capability to set test case dependencies
Both JUnit and TestNG utilize the “@” symbol for their annotations feature.
Now, let’s proceed with the installation and implementation of TestNG.
Cloud Server for Selenium Test Automation
#1) LambdaTest
LambdaTest, a cloud Selenium Grid, perfectly complements the TestNG framework. It enables parallel testing on over 2000 actual browsers and OS combinations, eliminating the need for maintaining an in-house Selenium Grid.
LambdaTest furnishes various logs including command logs, network logs, unprocessed Selenium logs, metadata, and even complete video recordings of your Selenium testing scripts for debugging.
LambdaTest also easily integrates with numerous CI/CD tools like Jenkins, Travis CI, CircleCI, and more for continuous testing. Additionally, it offers integration with various project management tools for unhindered bug tracking and logging.
Installing TestNG in Eclipse
To download and install TestNG on Eclipse, follow these easy steps:
Step 1: Open Eclipse IDE and choose the Help section in the menu. From the dropdown, choose the “Eclipse Marketplace” option.
Step 2: Enter “TestNG” in the search textbox on the Eclipse Marketplace then tap on the “Go” button.
Step 3: From the search results, tap on the “Install” button to install TestNG.
Step 4: Confirm the installation and wait for it to finish.
Step 5: Restart Eclipse for the changes to take effect.
Step 6: To verify the TestNG installation, click on “Preferences” from the “Window” option found in the menu bar.
Building a TestNG Sample Project
Now we create a TestNG project using the Eclipse IDE.
Step 1: Look for the File option in the menu and select New -> Java Project.
Step 2: Key in “DemoTestNG” as the project name and click on “Next”. To make the Java project, click on “Finish”.
Step 3: Configure the TestNG library by right-clicking on the project, selecting “Build Path,” and clicking on “Add library.” Next, choose TestNG and press the “Next” and “Finish” buttons to add the library to your project.
Now, the TestNG library has been added to your Java project, and the necessary libraries can be seen in the package explorer.
Add the downloaded Selenium libraries and jars to the project’s build path following the instructions from the previous guide.
Setting Up a New TestNG Class
With the basic requirements for building a test script through TestNG now established, let’s proceed with creating a sample script.
Step 1: Right-click on the “src” package in the project and click on New -> Other.
Step 2: Expand the TestNG option then select the “TestNG class” option. Click the “Next” button.
Step 3: Fill in the necessary fields, such as the source folder, package name, and TestNG class name. Click the “Finish” button to create the TestNG class.
You can now insert your actual test program into the newly formed TestNG class. Use the same program we used previously.
Next, we will insert the actual test code to set the base for the TestNG test script. We will use the same code we used in the previous session:
package TestNG; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class DemoTestNG { public WebDriver driver = new FirefoxDriver(); String appUrl = "https://accounts.google.com"; @Test public void gmailLogin() { driver.get("https://gmail.com"); driver.manage().window().maximize(); String expectedTitle = "Sign in - Google Accounts"; String actualTitle = driver.getTitle(); Assert.assertEquals(expectedTitle,actualTitle); WebElement username = driver.findElement(By.id("Email")); username.clear(); username.sendKeys("TestSelenium"); WebElement password = driver.findElement(By.id("Passwd")); password.clear(); password.sendKeys("password123"); WebElement SignInButton = driver.findElement(By.id("signIn")); SignInButton.click(); driver.close(); } }
Unveiling the Code with Similarity to TestNG:
1) @Test – @Test is a TestNG annotation. It tells us that the method with an @Test annotation points to a testing method. To use different TestNG annotations, incorporate the package “import org.testng.annotations.*”.
2) TestNG scripts don’t need a main() method. The program runs based on the annotations.
3) The Assert class allows comparisons of expected and actual values. Various checks use the Assert class. To use different assertions, we import “import org.testng.Assert”.
TestNG Script Execution
Following below is how you can run a TestNG testing script:
=> Right-click anywhere inside the class in the editor or package explorer, select “Run As” and select “TestNG Test”.
Results of TestNG are shown in two windows:
- Console Window
- TestNG Result Window
Refer to the above screenshot to check the result windows.
Generating HTML Reports
TestNG allows the creation of HTML reports that are readable by end-users for testing executions. The reports generated by TestNG can be viewed by any browser and using the built-in browser support in Eclipse.
Procedure to create HTML reports:
Step 1: Execute the TestNG class. Refresh the project by right-clicking on it and choosing “Refresh”.
Step 2: Inside the “test-output” folder, open the “emailable-report.html” file with the Eclipse browser to display the HTML report.
Step 3: You will now see the HTML report displayed within the Eclipse environment.
Assigning Priority in TestNG
Snippet of Code:
package TestNG; import org.testng.annotations.*; public class SettingPriority { @Test(priority=0) public void method1() { } @Test(priority=1) public void method2() { } @Test(priority=2) public void method3() { } }
Code Comprehension
If a testing script consists of multiple testing methods, you can assign the execution priority and sequence utilizing the TestNG annotation “@Test” and the “priority” parameter.
In the snippet of code above, every method is annotated with “@Test” and assigned priorities of 0, 1, and 2. The testing methods will be executed in this order: method1, method2, method3.
Understanding TestNG Annotations
Among annotations provided by TestNG, these are some examples:
Annotation | Definition |
---|---|
@Test | Indicates that the method is a test method |
@BeforeSuite | Executes before the tests in the suite |
@AfterSuite | Runs only after the tests in the suite |
@BeforeTest | Executes before any test method in the class |
@AfterTest | Executes after any test method in the class |
@BeforeClass | Executes before the first test method in the class |
@AfterClass | Runs only after the last method in a class |
@BeforeMethod | Executes before every test method in the class |
@AfterMethod | Runs only after every test method in a class |
@BeforeGroups | Executes before the first test method of a group |
@AfterGroups | Runs only after the last test method of a group |
Note: Numerous amounts of these annotations are also available in JUnit 3 and JUnit 4.
Wrapping Up
During this session, we delved into TestNG, a testing framework built with Java. We traversed its installation, script generation, and key features. We also explored the various annotations provided by TestNG and ran our first TestNG testing script injected by various annotations and assertions.
Key takeaways:
- TestNG, designed for coders and testers, is a significant testing framework.
- TestNG gives functionalities that are better than JUnit.
- TestNG is freely available and easily installed in Eclipse through Eclipse Marketplace.
- Setting up a new TestNG project is possible, and configuring the build path can be done with the help of the TestNG library.
- Setting up a new TestNG class that contains test methods injected with “@Test”.
- Run the TestNG script and display the outcomes in the TestNG Result Window.
- Generating HTML reports for testing executions is possible with TestNG.
- Assign execution priorities for testing settings using the “priority” parameter through TestNG.
- TestNG provides various annotations that you can use to personalize the testing execution.
In the upcoming session, we will discuss dropdown handling strategies. We will also go over WebDriver’s Select class and its dropdown choosing methods.
While waiting for the next session, you can begin creating your own WebDriver scripts using the TestNG framework.
Start creating advanced scripts and new concepts by incorporating additional annotations and assertions into your TestNG classes and running them in the TestNG environment. Display and analyze HTML reports created by TestNG.