Skip to main content

Install Maven and use it with TestNG

Steps to use install Maven and use it with TestNG Selenium

For this tutorial, we will use Eclipse (Juno) IDE for Java Developers to set up Selenium WebDriver Project. Additionally, we need add m2eclipse plugin to Eclipse to facilitate the build process and create pom.xml file.
Let's add m2eclipse plugin to Eclipse with following steps:
Step1) In Eclipse IDE, select Help | Install New Software from Eclipse Main Menu.
Step 2) On the Install dialog, select Work with and m2e plugin as shown in the following screenshot:
Step 3)Click on Next button and finish installation.

Configure Eclipse with Maven
With m2e plugin is installed, we now need create Maven project.
Step 1) In Eclipse IDE, create a new project by selecting File | New | Other from Eclipse menu.
Step 2) On the New dialog, select Maven | Maven Project and click Next
Step 3) On the New Maven Project dialog select the Create a simple project and click Next
Step 4) Enter WebdriverTest in Group Id: and Artifact Id: and click finish
Step 5) Eclipse will create WebdriverTest with following structure:
Step 6) Right-click on JRE System Library and select the Properties option from the menu.
On the Properties for JRE System Library dialog box, make sure Workspace default JRE is selected and click OK
Step 7). Select pom.xml from Project Explorer..


pom.xml file will Open in Editor section
Step 8).Add the Selenium and TestNG, JUnit dependencies to pom.xml in the <project> node:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    <dependencies>           
        <dependency>             
             <groupId>junit</groupId>                              
             <artifactId>junit</artifactId>                            
             <version>3.8.1</version>                              
             <scope>test</scope>                               
        </dependency>            
        <dependency>             
            <groupId>org.seleniumhq.selenium</groupId>                             
            <artifactId>selenium-java</artifactId>                             
            <version>2.45.0</version>                              
        </dependency>            
        <dependency>             
            <groupId>org.testng</groupId>                              
            <artifactId>testng</artifactId>                            
            <version>6.8</version>                             
            <scope>test</scope>                                    
       </dependency>             
</dependencies>
Step 9) Create a New TestNG Class. Enter Package name as "example" and "NewTest" in the Name: textbox and click on the Finish button as shown in the following screenshot:
Step 10). Eclipse will create the NewTest class as shown in the following screenshot:
Step 11) Add the following code to the NewTest class:
This code will verify the title of Guru99 Selenium Page
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package example;       
import org.openqa.selenium.By;     
import org.openqa.selenium.WebDriver;      
import org.openqa.selenium.firefox.FirefoxDriver;      
import org.testng.Assert;      
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;  
import org.testng.annotations.AfterTest;       
public class NewTest {     
        private WebDriver driver;      
        @Test              
        public void testEasy() {   
            driver.get("http://www.guru99.com/selenium-tutorial.html"); 
            String title = driver.getTitle();               
            Assert.assertTrue(title.contains("Free Selenium Tutorials"));      
        }  
        @BeforeTest
        public void beforeTest() { 
            driver = new FirefoxDriver(); 
        }      
        @AfterTest
        public void afterTest() {
            driver.quit();         
        }      
}  
Step 12) Right-click on the WebdriverTest and select TestNG | Convert to TestNG.
Eclipse will create testng.xml which says that you need to run only one test with the name NewTest as shown in the following screenshot:
Update the project and make sure that file appears in the tree Package Explorer (right click on the project - Refresh).
Step 13) Now you need to run test through this testng.xml.
So, go to the Run Configurations and create a new launch TestNG, select the project and field Suite astestng.xml and click Run
Make sure that build finished successfully.
Step 14). Additionally, we need to add
  1. maven-compiler-plugin
  2. maven-surefire-plugin
  3. testng.xml
to pom.xml.
The maven-surefire-plugin is used to configure and execute tests. Here plugin is used to configure the testing.xml for TestNG test and generate test reports.
The maven-compiler-plugin is used to help in compiling the code and using the particular JDK version for compilation. Add all dependencies in the following code snippet, to pom.xml in the <plugin> node:
Step 15) To run the tests in the Maven lifecycle, Right-click on the WebdriverTest and select Run As | Maven test. Maven will execute test from the project.
Make sure that build finished successfully.



Maven Learning

1.  Archetype : Used to create Project with same pattern like Hibernate Project, Web project etc.
command : mvn archetype :generate
2. Compile Project  : Go to folder where POM is place
command : mvn compile
3. Compile and Run Test (Unit Test)
command : mvn test
4. How to compile just test resources
command : mvn test-compile
5. How to create jar file using maven
command : mvn package
6. What are POM files
POM Contains :
·         Information about project
·         Project description
·         Versioning
·         Dependencies
7. List objectives provided by Maven
·         Provide Quality Information
·         Transparent Migration to new features
·         Makes build process easy
·         Uniform building system

8. Maven's Order of inheritance
·         Parent POM
·         Project POM
·         Settings
·         CLI Parameters
9. What are two main subdirectory folder of Maven
·         src
·         target
10. What is target directory used?
                House all output of build
11. What is Groupid
                Entity or organization responsible for producing the artifact
12. What is Artifact id
                Name of artifact
13. What is version
                Version number of artifact


Comments

Popular posts from this blog

API Testing With Selenium WebDriver

REST API Testing Framework We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios. Rest Testing Framework Overview The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response. The completed code can be accessed from my  GitHub account   from where you can collect and make modifications based on your requirements. Design We will be having three classes for the framework (in package com.axatrikx.controller ) RestExecutor  : Performs the HTTP operations using Apache Http Client RestResponse  : A javabean class to hold our response values (code, message, headers, body) RestValidator  : Validates the response with the expected values The package  com.axatrikx.test  holds the test scripts. Note: I will be using ‘ json-server ‘ to fake the REST API. Its a real handy tool to r

ExtentReports in Selenium Webdriver

ExtentReports in Selenium Webdriver What is ExtentReport? ExtentReports  is a HTML reporting library for Selenium WebDriver for Java which is extremely easy to use and creates beautiful execution reports. It shows test and step summary, test steps and status in a toggle view for quick analysis Download Download the jar below: Download ExtentReports 1.4 (1623)    Snapshot of Extent report After Executing the Script   Program Steps:  We are going to write three different testcases. Pass Warning Fail TestCase with Pass Result Navigate to http://www.guvi.in Click on Sign-in Enter the credientials Check the URL is correct or not after login   TestCase with Warning Result Verify with the Wrong URL (static String Afterloginfail="http://www.guvi.in/ ")    TestCase with fail Result Click on Menu Select Tech Challenges Verify With wrong URL. Source Code: import  java.io.File; import  java.io.IOException; import   java.sql

How to Compare Two Images in Selenium

How to Compare Two Images in Selenium Some time in testing we have to do image comparision as verification point. So, in this blog I expalin how to compare two images in selenium. I have created one function for compare two images. you can use directly into your framework. first of all you have to import below packages into your code. import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; Now, you can use below function for comparison of two images. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public boolean mCompareImages ( BufferedImage img1 , BufferedImage img2 ) { boolean bCompareImage = true ; try { if ( img1 . getWidth () == img2 . getWidth () && img1 . getHeight () == img2 . getHeight ()) { for ( int i = 0 ; i < img1 . getWidth (); i ++) { for ( int j = 0 ; j < img1 . getHeight (); j ++) { if ( img1 . get