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:
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
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:
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
- maven-compiler-plugin
- maven-surefire-plugin
- 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
Post a Comment