Execute Selenium code through Maven and TestNG

Last updated on Nov 24 2021
Swaminathan M

Table of Contents

Execute Selenium code through Maven and TestNG

• We will write one sample test suit , with the assistance of @Test, @BeforeTest, and @AfterTest TestNG annotations.
• First, we’ll launch the Google Chrome
• Then, navigate to the tecklearn website.
• And get the present page URL and title of the web site with the assistance of getTitle(), getCurrentURL()

Note:

getTitle(): This method is employed to capture the title of the page.
getCurrentURL(): it’s wont to capture the present URL of the page.
For executing the above test script, we’ll create one package within the MavenProject.
• To create a package within the MavenProject, we’ll right-click on the src/test/java → New → Package

execute 1
new package

• After that, we’ll provide the name of the package as “testpackage”, and click on on the Finish button as we will see within the below screenshot:

execute 2
testpackage

• After creating a package, we’ll create one class.
To create a category in MavenProject, follow the below process:
• Right-click on the newly created package, and choose New then select Class from the given list as we will see within the below screenshot:
testpackage → New → Class

execute 3
Right-click

• And, provide the name of the category as “MavenTest1” and click on on the Finish

execute 4
MavenTest1

After creating the package and sophistication , we’ll start writing the code.
According to our code requirement, we’ll be adding multiple dependencies.
After adding the dependencies, our pom.xml file appear as if this:

1.
2.
3. junit
4. junit
5. 3.8.1
6. test
7.
8.
9. org.testng
10. testng
11. 6.8
12. compile
13.
14.
15. com.google.inject
16. guice
17. 4.1.0
18. no_aop
19.
20.
21. org.seleniumhq.selenium
22. selenium-java
23. 3.141.59
24.
25.
26. org.seleniumhq.selenium
27. selenium-chrome-driver
28. 2.50.0
29.
30.
31. com.google.guava
32. guava
33. 22.0
34.
35.
36. com.google.api-client
37. google-api-client-appengine
38. 1.23.0
39.
40.
41. com.google.guava
42. guava-jdk5
43.
44.
45.
46.
47. com.google.code.gson
48. gson
49. 2.2.4
50.
51.

And, here the sample code:

1. package testpackage;
2. import java.util.concurrent.TimeUnit;
3. import org.openqa.selenium.WebDriver;
4. import org.openqa.selenium.chrome.ChromeDriver;
5. import org.testng.annotations.AfterTest;
6. import org.testng.annotations.BeforeTest;
7. import org.testng.annotations.Test;
8. public class MavenTest1 {
9. public String baseUrl = "https://www.tecklearn.com/";
10. String driverPath = "C://chromedriver_win321//chromedriver.exe";
11. public WebDriver driver ;
12. @Test
13. public void test() {
14. // set the system property for Chrome driver
15. System.setProperty("webdriver.chrome.driver", driverPath);
16. // Create driver object for CHROME browser
17. driver = new ChromeDriver();
18. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
19. driver.manage().window().maximize();
20. driver.get(baseUrl);
21. // get the present URL of the page
22. String URL= driver.getCurrentUrl();
23. System.out.print(URL);
24. //get the title of the page
25. String title = driver.getTitle();
26. System.out.println(title);
27. }
28. @BeforeTest
29. public void beforeTest() {
30. System.out.println("before test");
31. }
32. @AfterTest
33. public void afterTest() {
34. driver.quit();
35. System.out.println("after test");
36. }
37. }

Execute the code

We can run above code in multiple ways:
• Run through TestNG
• Run through Maven
• Run through instruction

Run through TestNG

If we run the above code with the TestNG, we’ll follow the below process:
• Right-click on the java file, and choose Run As → 1 TestNG Test as we will see within the below image:

execute 5
Run

• After running the code successfully, it’ll give the below output:

execute 6
output

• And it’ll launch the browser within the console window.

Run Through Maven

To run an equivalent code through Maven, follow the below steps:
First, we’d like to convert the MavenTest1.java file into the TestNG File, for this follow the below process:
• Right-click on the java file and choose TestNG then select Convert to TestNG options within the given list.

execute 7
execute

• Once we select the Convert to TestNG options, it’ll open the below window, and then , we’ll click on the Finish

execute 8
Convert

• And, the converted TestNG file will appear as if this:

execute 9
TestNG

After that, we’ll run the testng.xml file, so for this, we’d like to feature the Maven Plugins within the pom.xml files.
So, we’ll add the three different plugins, which are as follows:
Maven compiler plugin
Maven surefire plugin
Maven source plugin

Note:

The Maven compiler plugin is employed to compile the ASCII text file of a Maven project. Maven test command will hook up with the web and download all the dependency jar into the .M2 folder local repository then compile the whole selenium ASCII text file as we will see within the below image:

execute 10
MVN

The Maven surefire plugin is employed once we need to run the unit tests of the appliance .
The Maven source plugin is employed to create the jars files that were having the .java source files.
After adding all the plugins, our pom.xml appear as if this:

1.
2.
3.
4. org.apache.maven.plugins
5. maven-compiler-plugin
6. 3.1
7.
8. <source>>

So, this brings us to the end of blog. This Tecklearn ‘Execute Selenium code through Maven and TestNG’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Execute Selenium code through Maven and TestNG"

Leave a Message

Your email address will not be published. Required fields are marked *