TestNG: How to Run Multiple Test Suites in Selenium
- Create a new project in eclipse.
- Create two packages in the projects (name them as com.suite1 and com.suite2)
- Create a class in each package (name them as Flipkart.java and Snapdeal.java) and copy the below code in respective classes.
- Create a new file in your project and name it as testng.
TestNG is a testing framework. It also generates testing reports. Maven is a software project management and comprehension tool. It manages all dependencies and different flows for building a project.
How to run testng.xml from maven
- Write TestNG Tests. Below are two tests which we have already written in two previous tutorials. RegularExpressionGroupTest.java.
- Write TestNG Suite. Let's see the testng suite files testng. xml and suites-test-testng.
- Add testng suites in maven. Add below pom.
- TestNG maven example. Now execute above pom.
Using the Page Object Model pattern in Selenium + TestNG tests
- Create Page Objects representing pages of a web application that we want to test.
- Create methods for these Page Objects that represent actions we want to perform within the pages that they represent.
- Create tests that perform these actions in the required order and performs checks that make up the test scenario.
Create Maven Project in Eclipse IDE:f) Create package >> Create Class >> Add test cases. g) Run the tests or Press Ctrl+F11. h) Create Test Suite XML file. Right click on the project >> TestNG >> Convert to TestNG >> Change xml file name on Location field (Default testng.
To run the test, follow these steps:
- Right-click the MyFirstTestNGProject folder.
- Select Run as > TestNG Test: The results of the test will be shown in the console window and in the TestNG results window:
The first step in establishing our Java project is the creation of a Maven Project Object Model (POM) file. This is an XML document that defines how our code will be built, what additional dependencies it has access to, and how tests are run.
TestNG is an automation testing framework in which NG stands for "Next Generation". TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy. Using TestNG, you can generate a proper report, and you can easily come to know how many test cases are passed, failed, and skipped.
Maven is used to define project structure, dependencies, build, and test management. Using pom. xml(Maven) you can configure dependencies needed for building testing and running code. Maven automatically downloads the necessary files from the repository while building the project.
- Step 1: Convert a Selenium Project into Maven Project.
- Step 2: Remove Errors from the Project.
- Step 3: Remove Associated Libraries from the Project Build Path.
- Step 4: Add Dependencies to the Maven Repository.
- Step 5: Add JDK to the Project Build Path.
- Step 6: Add Maven Dependencies to the Project Build Path.
Here are the steps to install Maven through command line in this Selenium Maven tutorial:
- Step 1: Download & Install Java.
- Step 2: Set Up Java Environment Variable.
- Step 3: Download Maven and Set Up Maven Environment Variable.
- Step 4: Updating the Path Variable.
- Step 5: Testing the Maven Installation.
To
use Selenium with
Jenkins you need to configure
Jenkins with
Selenium.
Follow the below steps:
- Go to Jenkins dashboard.
- Click on manage Jenkins.
- Click on configure Jenkins.
- Click on JDK installation – In JDK name section enter the name, under Java Home section – give your java path.
In the build section you'll find an option for adding your
pom.
xml file. Add the path of your
pom.
The complete process to install Jenkins can be summarized in five steps:
- Install Java Version 8.
- Install Apache Tomcat Version 9.
- Download Jenkins war File.
- Deploy Jenkins war File.
- Install required plugins.
Cucumber is an open source tool that supports Behavior Driven Development (BDD) framework. It provides the facility to write tests in a human readable language called Gherkin. The Selenium-Cucumber framework supports programming languages such as Perl, PHP, Python, . NET, Java, etc.
Create TestNG Project In Eclipse
- Step 1: Navigate to File > New > Java Project.
- Step 2: Give your project a name for example, 'LambdaTestNG' and click on Next.
- Step 3: On the next screen, you will see the Java settings for your new project.
- Step 4: Choose TestNG from the list of libraries and click Next.
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.
xml through the command prompt:
- Open the command prompt on your system.
- Go to the Test Java project folder where the new testng. xml is created.
- Type the following line. java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml.
Right-click the pom. xml file and select Run As Maven build. Enter "clean install" as Goal. You have to enter the goals manually.
The main advantage of using this is we can execute multiple testng. xml files. You can execute the project now by right clicking on project, Run As –> Maven Test (another way to execute test case is from command prompt).
How to verify if TestNG is installed successfully
- Verify TestNG is installed at show view window. Navigate to menu Window > Show View > Other. Expand Java folder in the Show View window.
- Verify if TestNG is installed successfully by creating a new class. Right click on package explorer view in Eclipse IDE.
The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats: Plain text files (*. txt) XML files (*.
In the parent POM, the main difference between the <dependencies> and <dependencyManagement> is this: Artifacts specified in the <dependencyManagement> section, will only be included in the child module if they were also specified in the <dependencies> section of the child module itself.
Via the Maven index, you can search for dependencies, select them and add them to your pom file. To download the index, select Windows > Preferences > Maven and enable the Download repository index updates on startup option. After changing this setting, restart Eclipse. This triggers the download of the Maven index.
Dependency management is a mechanism to centralize the dependency information. In a multi-module project, you can specify in a parent project all the artifact version and it will be inherited by the child projects. Below we will see an example where there are two POMs which extend the same parent.
The <scope> element can take 6 values: compile, provided, runtime, test, system and import. This scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks. compile. This is the default scope, used if none is specified.
The Local RepositoryThe local repository of Maven is a folder location on the developer's machine, where all the project artifacts are stored locally. When maven build is executed, Maven automatically downloads all the dependency jars into the local repository. Usually this folder is named . m2.
Maven pom. xml file with additional elements
| Element | Description |
|---|
| url | defines url of the project. |
| dependencies | defines dependencies for this project. |
| dependency | defines a dependency. It is used inside dependencies. |
| scope | defines scope for this maven project. It can be compile, provided, runtime, test and system. |
1. 2. Plugins defined in your buildsection plugins tag will be executed during the build of your project. There are many plugins that do something with your build. For example the maven-compiler-plugin which allows you to set the Java version for your project.
Select the project that Maven support will be added to from the Project Explorer. Right-click on the project to bring up the context menu. From the context menu navigate to Configure → Convert to Maven Project. Click on the Convert to Maven Project menu option.
artifactId is the name of the jar without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar you have to take the name of the jar as it's distributed. eg. maven , commons-math.