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

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...