Skip to main content

First Selenium program using Maven+Java


This is basic example to use Webdriver, Maven with Java.


We need Tools:


1.Eclipse - IDE to Build our applications

Download and install Eclipse. I choose Eclipse IDE 
you can choose IDE of you choice.


2.Maven - We need maven to get all our dependencies automatically, which also allows users to reuse same jars across multiple projects


Download and install maven.
Eclipse does not have integrated Maven support out of the box. To add the support, I am going to use Maven Integration (m2e).



Steps to create program


Step 1:  First create a new Maven project. 

In Eclipse: File -> New -> Other  -> Maven -> Maven project


  • Click: Next, Next, Next
  • Type in field Group Id: com.demo.yourCompanyName
  • Type in field Artifact Id: yourProjectName
  • Click: Finish


Step 2 Edit pom.xml

Eclipse should detect the pom.xml changes automatically, build the project and download required dependencies.

After the build is finished, there should be selenium*.jar files under Maven Dependencies under the Project Explorer panel.

POM.XML


<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>com.demo.yourCompanyName</groupId>
 <artifactId>Demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>
  <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-server</artifactId>
   <version>3.4.0</version>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>         


Step 3:

Create a new test class
Right-click over package com.yourcompany.yourclientprojectname 

under src/test/java and select New -> Class

Type in ClassName: OpenFirefox


Java Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenFirefox {

 public static void main(String[] args)

 {

  System.out.println("Start");

  System.setProperty("webdriver.gecko.driver", "Path to gekco driver\\geckodriver.exe");

  WebDriver driver = new FirefoxDriver();

  driver.get("https://www.google.co.in/");

  System.out.println("End");
 }
}

Step 4 :

Right click on class RunAs -->java application.

Your firefox browser will open with given URL "https://www.google.co.in/"

Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Failure to transfer https://repo.maven.apache.org/maven2 was cached in the local repository

Error : Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled. pom.xml /LogReaderServer line 1 Maven Configuration Problem Solution : Remove all your failed downloads: For windows: cd %userprofile%\.m2\repository for /r %i in (*.lastUpdated) do del %i