{{announcement.body}}
{{announcement.title}}

Selenium WebDriver and TestNG: Find Perfect Match for Automation Testing

DZone 's Guide to

Selenium WebDriver and TestNG: Find Perfect Match for Automation Testing

The manual testing process has been replaced by automated testing. Selenium automation testing increases the effectiveness and efficiency of the testers,

· Performance Zone ·
Free Resource

Me looking for the perfect match for automation testing.

The manual testing process has been replaced by automated testing during recent years. Selenium automation testing increases the effectiveness and efficiency of the testers and allows them to leverage various benefits at the same time.

It has gained massive popularity among developers, as it is an open-source tool it provides enormous support to the developers. Many organizations are adopting this favorite testing tool as it provides more accuracy and efficiency to them. 

You may also like: Discover the Top Tips and Tricks of the Selenium WebDriver

One of the significant aspects which are responsible for the popularity of these tools is its cost. Nowadays, most of the businesses are trying to cut most of their operational costs so that they can increase their profit margin, in such circumstances, selenium can prove to the best option for them as it is costlier when compared to other testing tools. Even businesses can find the perfect match by exploring and learning how Selenium Webdriver and TestNG can enhance their process and help them go the long run. 

What Selenium Webdriver and TestNG Really Mean? 

The future of selenium is quite promising as it provides various opportunities for the business. Every organization is striving to offer an excellent experience to its users all the time. They are looking forward to optimizing the look, feel, and performance of their developed apps and selenium testing is perfect for reaching all the goals. Even taking the support of selenium Webdriver and TestNG is just like adding a cherry to the favorite dessert. 

Selenium Webdriver

It is one of the advance web automation frameworks which allows users to execute their tests. They can easily execute tests against different web browsers. Selenium WebDriver facilitates users to use a programming language for creating test scripts that might not be possible while they are using in Selenium IDE. It enables them to use various conditional operations such as switches cases, if-then-else, and many more. 

Selenium Webdriver also allows users to perform looping as per their coding and testing requirements. It provides support for various programming languages like Java, PHP, .Net, Perl, Python, and Ruby. Besides this, users get various benefits when they use Selenium Webdriver, they are: 

  • Help to bagged a higher ROI.
  • Easy access, users can even access it remotely through any device. 
  • Reduction of flaws.
  • It helps to find bugs and flaws at the initial stage.
  • It makes the process more reliable and tests dependable for users.
  • Enables testing in volumes.

Explore the Code to Know How Users Implement Selenium Webdriver

Java




xxxxxxxxxx
1
26


1
import java.util.ArrayList;
2
import java.util.List;
3
import org.testng.TestNG;
4
 
5
public class RunTestNG {
6
 
7
public static void main(String[] args) {
8
 
9
// Create object of TestNG Class
10
TestNG runner=new TestNG();
11
 
12
// Create a list of String 
13
List<String> suitefiles=new ArrayList<String>();
14
 
15
// Add xml file which you have to execute
16
suitefiles.add("C:\\Users\\Documents\\Blog6 March\\dummy 16 june\\testng.xml");
17
 
18
// now set xml file for execution
19
runner.setTestSuites(suitefiles);
20
 
21
// finally execute the runner using run method
22
runner.run();
23
} 
24
}



Source: automation.com

TestNG Framework: 

It is an automation testing framework that is widely used by testers to test the developed product. NG is referred to as "Next Generation", this automation testing framework is inspired by JUnit and NUnit, and it uses the annotations (@). It is an open-source test framework for Java. (Source)

TestNG framework's advanced and useful features make it robust and more useful than others. It is portrayed by Cedric Beust and is frequently used by coders and testers for creating a test case for their project. They can easily use their multiple annotations and multiple features. It also offers various benefits to the users who prefer it, some of the benefits are as follows: 

  • Logs generation.
  • Easy use of annotations.  
  • Enables users to group and prioritize test cases. 
  • Allow parallel testing. 
  • Helps to product execution HTML reports.  
  • Data parameterization.

TestNG framework can be integrated with various tools as per users' requirements, users can use tools such as Jenkins, Maven, Jenkins, and many others to test their app. Using Annotations of TestNG framework makes it easy for coders and tester to understand the overall process.

They can use annotations like @BeforeTest, @AfterTest, @BeforeMethod, @AfterMethod, and many others. There is a lack of a native mechanism in Selenium WebDriver, which makes it quite hard for users when it comes to reporting generation. But while using the TestNG framework, users can easily generate the report that also in a readable format, have a look to know how the report actually look: 

TestNG simplifies the way of coding for the tester, they need not need to apply the static main method in their tests. Instead of that, they can simply use a sequence of annotations to perform various actions in the test, users don't require to make the method static, which makes the testing process much easier to understand for the users. Have a look to know how to use annotations instead of the static methods.  

Handling Uncaught exceptions has become much easier with the TestNG framework as it handles it automatically. Testers can handle it without terminating the test prematurely, such exceptions can be termed as failed steps.

Users can even create a group while using TestNG, which lesser their coding comparatively and make their work much more manageable. But while creating the group with TestNG, they have to consider some of the aspects and codes which is listed below.

Syntax For Creating Groups in TestNG:

Syntax

     @Test(groups={"groupname"}) 

Example of implementing single and multiple groups:

After completing the grouping process, users can easily exclude and include testng.xml as per their requirements. 

<include>: Helps testng.xml to know which group actually needs to be executed.

<exclude>: Helps testng.xml to know which group actually needs to be Skip during execution.

Thus if any of the users execute their test cases with grouping, then they have to make changes (modification) to their testng.xml file. They have to explicitly the file after specifying the group to it. View code to create and modify the complete program using group test cases in selenium. 

Explore the Code to Create Group Test:

Sample Program

Java
x
19
 
1
package testing demo;
2
 
3
import org.testng.annotations.Test;
4
 
5
public class TestGroupDemo {
6
 
7
    
8
    @Test(groups={"Smoke"})
9
    public void login(){
10
        
11
        System.out.println("Login done");
12
        System.out.println("Smoke Scenario passed");
13
    }
14
    
15
    @Test(groups={"Regression"})
16
    public void register(){
17
        System.out.println("Registration done");
18
    } 
19
}


View the Complete Code to Modify Testng.xml to Use Group Test Cases in Selenium.

XML
xxxxxxxxxx
1
19
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3
<suite name="Suite" parallel="none">
4
  <test name="Test">
5
    <groups>
6
     
7
       <run>
8
            
9
            <include name="Smoke" />
10
            <exclude name="Regression"/>
11
             
12
       </run>
13
     
14
    </groups>
15
    <classes>
16
      <class name="testngDemo.TestGroupDemo"/>
17
    </classes>
18
  </test> <!-- Test -->
19
</suite> <!-- Suite -->


In the above-mentioned XML code, you can explore that a new tag called group is added to it. And under group tag, two different tags exclude and include is mentioned. Users can also mention the group name as per their convenience and requirement and can include and exclude the same in the needed file. 

Selenium Webdriver and TestNG: Explore How You Can Use It Parallelly While Execution

When users have to test the application, then it becomes essential for them to test the particular app on different browsers. If they do not do so, then it becomes difficult for them to know whether the app supports the particular browser or not. Moreover, testers can test their effectiveness with the help of Selenium WebDriver and TestNG. 

If the tester or developer wants to execute numerous scripts, then it becomes quite challenging for them as it is one of the time-consuming tasks. However, if they want to complete it quickly, then they can use Parallel Execution, which is one of the critical concepts of selenium. In this concept, they can use Selenium WebDriver and TestNG partially and can complete the execution process. 

TestNG facilitates users to execute test cases/scripts in parallel. Furthermore, by using the Parallel Execution concept, they can easily reduce the execution time, as all the tests are executed on different browsers at the same time as they have already declared it in the testng.xml file. Explore the below code to know how it can run on multiple browsers at the same time (that is parallel).

Example of Parallel Execution With Selenium WebDriver and TestNG

Java
xxxxxxxxxx
1
66
 
1
package com.exploration.scripts;
2
import java.util.concurrent.TimeUnit;
3
import org.openqa.selenium.By;
4
import org.openqa.selenium.WebDriver;
5
import org.openqa.selenium.WebDriverException;
6
import org.openqa.selenium.chrome.ChromeDriver;
7
import org.openqa.selenium.firefox.FirefoxDriver;
8
import org.openqa.selenium.ie.InternetExplorerDriver;
9
import org.testng.annotations.BeforeTest;
10
import org.testng.annotations.Parameters;
11
import org.testng.annotations.Test;
12
public class AddingToWishList
13
{  
14
    private WebDriver driver;
15
      String url = "http://sites.ecanarys.com/Nopcommercesite";
16
      @Parameters ({"browser"})
17
      @BeforeTest
18
       public void preCondition(String browser)
19
      {
20
              try
21
             {
22
                    if(browser.equalsIgnoreCase("Firefox"))
23
                    {
24
                          driver = new FirefoxDriver();
25
                    }
26
                     if(browser.equalsIgnoreCase("Chrome"))
27
                    {
28
                          //Location of the chromedriver file
29
                          System.setProperty("webdriver.chrome.driver","D:/ exefiles/chromedriver.exe");
30
                          driver = new ChromeDriver();
31
                    }
32
                     if(browser.equalsIgnoreCase("IE"))
33
                    {
34
                          //Location of the IEDriverServer.exe file stored in your machine              
35
System.setProperty("webdriver.ie.driver","D:/ IEDriverServer.exe");
36
                          driver = new InternetExplorerDriver();
37
                    }                   
38
             }
39
              catch (WebDriverException e)
40
             {
41
                    System.out.println("Browser not found" +e.getMessage());
42
             }
43
             driver.get(url);
44
             driver.manage().window().maximize();
45
             driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
46
      }
47
      @Test
48
       public void addToCart() throws InterruptedException
49
      {
50
             //Clicking on Cement link 
51
             driver.findElement(By.partialLinkText("Cement")).click();
52
             WebElement ele = driver.findElement(By.cssSelector("a[href='/Nopcommerce Site/maha-shakthi-ppc']"));
53
             Actions action = new Actions(driver);
54
             action.moveToElement(element).click().perform();
55
             //Add to Wishlist button
56
             driver.findElement(By.id("add-to-wishlist-button-1465")).click();
57
             //Clicking on the wishlist button on pop up
58
             driver.findElement(By.xpath("//input[@class='button-1 productAddedToCartWindowCheckout']")).click();
59
             //Getting the count 
60
             String whishlistCount = driver.findElement(By.xpath("//span[@class='wishlist-qty']")).getText();
61
             //Printing the Count 
62
             System.out.println("Wishlist count: " +whishlistCount );
63
             driver.quit();     
64
   }
65
}
66
 
          


(Source)

Explore the Reason Which Justifies Why Selenium Is a Perfect Match for You

Most of the entrepreneurs are running their business online and aiming to reach more and more customers in no time. Thus to satisfy their aim, it becomes essential for them to advertise their business effectively with the help of ppc agency and even pay attention to app development.

By developing the app, they can ease and clear the way for customers to reach them and make a purchase from them. However, what if the app is not working effectively on targeted user's devices? Thus businesses have to make sure that the app which they developed works perfectly on each device and browser. And when it comes to testing the app, then the app tester and testing framework grab the spotlight. 

Businesses take the support of testers to test the developed app and make it a perfect one for the use of customers. Most of the testers these days are using selenium to test the developed app as it offers various advantages to them. They leverage numerous benefits, especially when they choose selenium as their first preference. Explore some of the benefits to know how it benefits testes and other users who use it to test the developed app on various browsers. 

Open Source

Selenium is the most popular open-source software that was released under the Apache 2.0 license. As the framework is an open-source, more and more users are downloading and using it. The free nature of selenium has cut down the testing costs to a great extent. (Source)

Web Browser and OS Support

When it comes to the app, then it becomes essential that it runs excellently on all the browsers and OS. Doing so app owners can keep their app visitors engaged with the app and can convey them to make a purchase from the same.

Furthermore, for doing so, testers need tools that help them to improve app performance on mostly used browsers. They can improve app performance on various browsers such as Firefox, Chrome, and many more. Even they can use the framework to enhance app performance on various operating systems like Windows, OS X, Linux, and many others.

Summing It Up

TestNG and Selenium Webdriver enhance the capability of Selenium tests and make it easier for users to understand various issues and generate a report for the same. It provides flexibility and great strength to the users who want to test the web app in no time and want to generate a report that others can understand easily. 

Even the users can add new functionalities to the testing framework and test script as per their requirements. Apart from offering flexibility to customize the framework and test script, it also allows users to make the necessary modifications within it. Thus it is quite sure that Selenium Webdriver and TestNG can be considered as the future of testing.


Further Reading

How to Write Your First Script in Selenium WebDriver

Selenium WebDriver for Cross-Browser Testing, Part 1

5 Courses to Learn Selenium WebDriver for Automation Testing in Java

Topics:
automation testing ,webdriver ,testng ,testng framework ,testng framework in selenium ,testng tutorial ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}