maven-selenium

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.

Picture1

Go to “File -> New -> Others” or else we can use File-> New-> Maven Project (if available)

2.) It will show the Dialog like:

Picture2

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.

Picture3

Select “maven-archetype-quickstart” -> Click “Next ” button.

4.) Add Group ID and Artifact ID, see sample screen-shot below:

Picture4

5.) Click Finish to complete the maven creation wizard. And after the wizard is closed, we will be in main Eclipse IDE window.

Picture5

Sample code creation using TestNG:

1.) In the project explorer area, Right click on the Package folder and select the New->Others.

Picture6

2.) It will show the below dialog box

Picture7

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.

Picture8

4.) After creating the TestNG class, it will show the codes with error:

Picture9

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>

&nbsp;

<groupId>Maven1</groupId>

<artifactId>SampleMaven</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

&nbsp;

<name>SampleMaven</name>

<url>http://maven.apache.org</url>

&nbsp;

<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>

&nbsp;

<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>

&nbsp;

<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.

Picture10

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;

&nbsp;

public class Untitled {

private WebDriver driver;

private String baseUrl;

private boolean acceptNextAlert = true;

private StringBuffer verificationErrors = new StringBuffer();

&nbsp;

@BeforeClass(alwaysRun = true)

public void setUp() throws Exception {

driver = new FirefoxDriver();

baseUrl = "https://www.google.co.in/";

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

&nbsp;

@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();

}

&nbsp;

@AfterClass(alwaysRun = true)

public void tearDown() throws Exception {

driver.quit();

String verificationErrorString = verificationErrors.toString();

if (!"".equals(verificationErrorString)) {

fail(verificationErrorString);

}

}

&nbsp;

}

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.

Picture11