Thursday, 11 January 2018

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

No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...