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.
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).
- In Eclipse: Help -> Install New Software
- Type the following URL in field Work with: http://download.eclipse.org/technology/m2e/releases
- Click Next etc. to move forward and choose to restart Eclipse when prompted
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
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:
Step 4 :
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