Maven is a software project management and comprehension tool, It control and manages the plug-in module with POM (Project Object Model) which is basically an XML file format and it contains all plug-in details of the particular project like MVC.
Creating Maven project in eclipse: Note: Following Tools should be installed on your system:
- JAVA (JDK)
- Eclipse
1.) Open eclipse and Create a new project using the following screenshot.
Go to “File -> New -> Others” or else we can use File-> New-> Maven Project (if available)
2.) It will show the Dialog like:
Select “Maven project” ->Click “Next” button,
3.) It will show the next dialogue box to select the Archetype (Archetype is a templating toolkit and It defines the pattern of the project), now we are going to select the basic model, see screenshot below.
Select “maven-archetype-quickstart” -> Click “Next ” button.
4.) Add Group ID and Artifact ID, see sample screen-shot below:
5.) Click Finish to complete the maven creation wizard. And after the wizard is closed, we will be in main Eclipse IDE window.
Sample code creation using TestNG:
1.) In the project explorer area, Right click on the Package folder and select the New->Others.
2.) It will show the below dialog box
Select TestNG -> TestNG Class and Click on “Next” button
3.) Now a new window pop-up, fill in the details of the class name and the methods used.
4.) After creating the TestNG class, it will show the codes with error:
5.) Open the Pom.xml file and delete the entire code inside the file. And now replace it with the code provided below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Maven1</groupId> <artifactId>SampleMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SampleMaven</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <forkCount>0</forkCount> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>LATEST</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>LATEST</version> <scope>test</scope> </dependency> </dependencies> </project>
And the above-mentioned code will create the TestNG, Selenium and Maven Jar files inside the project.
6.) Now re-run the entire process of TestNG. And all the error message will be fixed automatically.
7.) Copy and paste the code which you created in Selenium IDE or you can use the following code to execute.
Sample Code:
package com.example.tests; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.testng.annotations.*; import static org.testng.Assert.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class Untitled { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @BeforeClass(alwaysRun = true) public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "https://www.google.co.in/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testUntitled() throws Exception { driver.get(baseUrl + "/?gws_rd=ssl"); driver.findElement(By.id("lst-ib")).clear(); driver.findElement(By.id("lst-ib")).sendKeys("Software testing introduction"); driver.findElement(By.id("_fZl")).click(); driver.findElement(By.linkText("Introduction to Software Engineering/Testing - Wikibooks, open books ...")).click(); driver.findElement(By.cssSelector("span.toctext")).click(); } @AfterClass(alwaysRun = true) public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } }
8.) After paste, just change the Package and Class names.
9.) And Right click on the Package folder and select the Run As->TestNG, then it will execute the code after TestNG and Maven Jar files installation.
10.) Result screen will show like the below-mentioned screenshot.
Leave a Reply