Wednesday, May 27, 2020

Selenium With Cucumber Using Maven Complete Tutorial

Cucumber tool is based on the Behavior Driven Development framework that acts as the bridge between the following people:
  1. Software Engineer and Business Analyst.
  2. Manual Tester and Automation Tester.
  3. Manual Tester and Developers.
Cucumber is an open source tool that supports Behavior Driven Development (BDD) framework.
It provides the facility to write tests in a human readable language called Gherkin.

Prerequisites To Set Up Cucumber

In order to install Cucumber on your system, you would need some basic installations on your system:
  1. Set up JDK on your system (JDK 1.8 or the latest version)
  2. Install Eclipse (Eclipse OXYGEN or the latest version)
  3. Install Cucumber plugin:
    • In Eclipse, go to Help → Install new software
    • On the Available Software popup, enter the URL “ http://cucumber.github.com/cucumber-eclipse/update-site ” in the Work with field.
    • You will see “Cucumber Eclipse Plugin” displayed in the filter; select the checkbox and click Next, and you will navigate to the Install Details popup. Click Next to proceed further.
    • Accept the license in the Review License pop-up and click Finish.

Why Maven?

Maven is a automation build tool and is widely used for Java projects. It is mainly used in managing dependencies through pom.xml.
















Maven


Environment Setup

Java JDK setUp:
  1. Download Java jdk: https://www.oracle.com/java/technologies/javase-jdk13-downloads.html
click next next and install.
  1. go to environmental variable and new : new- JAVA-HOME and paste and copy paste directory C:\Program Files\Java\jre1.8.0_241
  2. go to the path and  new paste jdk path: C:\Program Files\Java\jre1.8.0_241\bin
  3. go to cmd and type java -version
Maven:
Maven, POM file
maven is a build management tool and it contains pom file and it has dependency. which hit web.
  1. download maven apche.: https://maven.apache.org/download.cgi
  2. download binary zip archieve : https://maven.apache.org/download.cgi
  3. extract. go to c drive and write Dev_programs and copy paste apache-maven inside folder
  4. go to environmental variable and type M2_HOME and location of bin folder
  5. go to env new and M2 and type location of bin
  6. go to path and %M2%
  7. check type mvn -v
Eclipse download:
  1. go to eclipse and download it.
  2. maven install on eclipse ...go to  windows-> preference and maven installation and select maven apache where we drag and drop apache maven
  3. windows pereference --> install jre add --> standad vmp and give java jdk there..
  4. eclipse set up linkfor step 3:  Dmaven.multiModuleProjectDirectory=M2_HOME
  5. go to java build and add variable and select M2_repo
  6. eclipse pom setup is 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>

<groupId>com.webdriveruniversity</groupId>
<artifactId>CucumberFramework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<!--<executable>C:\Program Files\Java\jdk1.8.0_121\bin\javac.exe</executable> -->
<executable>${env.JAVA_HOME}\bin\javac.exe</executable>
</configuration>
</plugin>
</plugins>

</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

TestNG framework:


  1. go to marketplace and search testNG and install TestNG for eclipse and check all checkbox
  2. check eclipse is installed or not.. go to new other option and see testng on any one package
Cucumber add Eclipse:
  1. got to eclipse marketplace  and add cucumber "natural" and no need jbehavior ..AND Restart
  2. then go to market place and add Eclipse cucumber kit:https://cucumber.io/cucumber-eclipse/update-site
visual aid enhancement tool for eclipse:
  • go to eclipse market place  and search eclipse color theme, darkest dark theme,
  • go to eclipse preference and change color text
chrome driver and chrome browser setup:


Introduction:
Maven: is a build management tool. maven project file has pom file. download relevant resource from web. pom has a dependency. Jenkins execute a build.

How to create maven project:
New --> maven project -->  location select -->group id ..com.manojlearn
execution jdk
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<!--<executable>C:\Program Files\Java\jdk1.8.0_121\bin\javac.exe</executable> -->
<executable>${env.JAVA_HOME}\bin\javac.exe</executable>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
POM
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<!--<executable>C:\Program Files\Java\jdk1.8.0_121\bin\javac.exe</executable> -->
<executable>${env.JAVA_HOME}\bin\javac.exe</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<testErrorIgnore>false</testErrorIgnore>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</reporting>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-html -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>

<!-- Extent Reports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.0.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.26-incubating</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>


  • maven apache plugin
  • j unit
  • seleniu manager webdriver
  • selenium  java
Maven commands you must know:
  • right click --> import  --> rootdirectory select and press finish
  • update maven project
  • project clean


Gerkin

feature file

Creating first feature:
groupId: com.stakeflow
ArtifactId: CucumberFramework
Package: CucumberFramework
Login.feature

Feature: Login into account
Existing stackoverflow user should be able to login into account using correct credentials
Scenario: Login into account with correct details
Given User navigates to stackoverflow website
And User clicks on the login button on homepage
And User enters a valid username
And User enters a valid password
When User clicks on the login button
Then User should be taken to the successful login page


What are step definition classes:

Firebug and Fire path:
  1. download firefox: firefox release download on google: https://ftp.mozilla.org/pub/firefox/releases/
  2. 48.0b9/ download this version : https://ftp.mozilla.org/pub/firefox/releases/48.0b9/
  3. download : https://ftp.mozilla.org/pub/firefox/releases/48.0b9/win64/en-GB/
Creating  first step definition class:
  • create login class
  • highlight feature file and run configuration and select cucumber feature then double click on new configurationa and give name login go to feature path and browse on actual feature file and dobul click and run....then you can see execution of feature file.
  • copy syntex and paste that code.
  • remove throw and save it and write code.
...........................
Examples will be updated soon.

No comments:

Post a Comment