MainMenu

Home Java Overview Maven Tutorials

Monday, 7 November 2022

Cucumber Tutorial with JUNIT

Hello Friends,
In this article , we will work with Selenium BDD (Behaviour driven development) using Cucumber.
So we will learn the cucumber with below topics :-

1). How to Configure/Install Cucumber in eclipse

2). How to create maven project & change it to cucumber project

3). Dependencies required to configure the cucumber

4). Create a feature file

5). Create step definition file

6). Create runner file

7). Run cucumber project



How to install the Cucumber plugin in Eclipse: please follow the below path in eclipse

Help -->> Eclipse Market Place --> Search "Cucumber" in search box --> Click on Install
as shown in below image


OR Help -->> Install New Software --> Click on Add button --> http://cucumber.github.io/cucumber-eclipse/update-site -->Save




Now Create a Maven Project


Now configure the POM.xml file == >Add Cucumber, Junit dependency , ==>> add plugins


<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>way2testingCucumber</groupId>
<artifactId>way2testingCucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>7.14.1</cucumber.version>
<surefire.version>3.2.2</surefire.version>
<maven.complier.version>3.11.0</maven.complier.version>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.complier.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version></plugin>
</plugins></pluginManagement>
</build>
</project>



Create a Resource folder and feature file inside it




Create a package "StepDefinition" and created a class and write all steps definitions


Create a runner package and a runner class and add Cucumber Options

package Runner;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "D:\\Software\\Selenium\\01_Cucumber_Framework_2024\\WorkSpace\\way2testingCucumber\\Resource",
glue = {"Step_definitions"},
plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/report.json"},
tags="@Regression",
monochrome =true )
public class TestRunner {
}





Now run the cucumber test either via JUNIT or by Maven



SetUp Cucumber Maven JUnit from scratch


Create project with Cucumber Maven JUnit from scratch




Thursday, 11 August 2022

Basic Rest Assured Methods

Hello Friends, In this article we will cover all the possible Rest Assured methods :-

Monday, 16 May 2022

JavascriptExecutor usage in selenium

In this article we will see how to perform multiple operations like scroll, click, getText, sendText using java script executor

If a web page is highly dynamic in nature it have complex Javascript functions or Ajax function then sometimes simple click using selenium like
driver.findElement(By.xpath("")).click(); do not work OR many functions like getText(), sendKeys() etc do not work
So here we can use JavascriptExecutorInterface and its various mathods to perform such operations.

How to perform Scroll by using java script executor

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(X,Y)", "");
//here X & Y are value in pixal

Example :- JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");


public static void scrollxandy(WebDriver driver, int x, int y)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy("+x+","+y+")", "");
}


How to perform Scroll upto a webElement by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


How to perform Scroll upto upto bottom of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");



public static void scrolluptobottom(WebDriver driver)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}


How to perform Scroll upto upto Top of WebPage by using java script executor


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, 0);");


WebElement element = driver.findElement(By.tagName("header"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);


JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0, -1000000000)");


How to perform Action click by using java script executor


WebElement element = driver.findElement(By.xpath(""));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", we);


How to perform Action SendKeys/SendText by using java script executor


public static void sendTextusingJavascriptExecutor(WebDriver driver, WebElement we, String text)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value=arguments[1];", we);
}



How to perform Action getText by using java script executor


public static String getTextUsingJavascriptExecutor(WebDriver driver, WebElement element)
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
String text = (String) jse.executeScript("return arguments[0].value", element);
return text;
}




How to Highlight a webElement by using java script executor


public static void highlight(WebDriver driver, WebElement element) throws Exception
{
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
Thread.sleep(100);
jse.executeScript("argument[0].setAtrribute('style', '');", element);
Thread.sleep(100);
jse.executeScript("arguments[0].setAttribute('style', 'background:yellow; border : 2px solid red;');", element);
}




Sunday, 1 May 2022

How to Press Multiple Keys in Selenium


How to press mulitple keys in selenium

Example :- Press control + t key :



driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 't');

OR

driver.findElement(By.xpath("")).sendKeys(Keys.chord(Keys.CONTROL, "T"));

------------------------------------
How to press Enter keys in selenium
WebElement textbox = driver.findElement(By.id("idOfElement"));

textbox.sendKeys(Keys.ENTER);

How to Press thress keys simultaneously in selenium


driver.findElement(By.xpath("")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ALT, Keys.DELETE));

How to Validate input/character is number or string in Java

Validate Input String is number or not



Hello Friends,

This article will describe how can we check whether a data is number or String in java.



Algorithm :-
1). First Break the string into characterArray. 2) Now to check for number use Integer.Parseint() 3) Use Character.isDigit() to check whether character is number or not.


Example :-
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class PojorunExample
{
static boolean isnumber(String x)
{
boolean flag = false;
try
{
int num = Integer.parseInt(x);
flag = true;
}
catch(Exception e)
{
}
return flag;
}
@Test
public void g()
{
String x = "Chandan";
String y = "chandan30";
if(isnumber(x))
{
System.out.println("its a number");
}
else
{
System.out.println("its not a number");
}
char[] ch = y.toCharArray();
for(int i = 0; i<ch.length; i++)
{
if(Character.isDigit(ch[i]))
{
System.out.println(ch[i] + " " + "this is number");
}
else
{
System.out.println(ch[i] + " " + "this is letter");
}
}
}
}
Output :-
its not a number
c this is letter
h this is letter
a this is letter
n this is letter
d this is letter
a this is letter
n this is letter
3 this is number
0 this is number

Saturday, 16 April 2022

POJO Class in Java

POJO Class in Java with example



Hello Friends,

This article will describe the concept of pojo class in java.



POJO Class in Java

:-


POJO stands for :-
PLAIN OLD JAVA OBJECT

Pojo is an ordinary object and pojo is not bound by any special restriction.
Also pojo file does not require any special classpath.
It increases the readability and re-usability of a java program.
History :-
In 2000, The term POJO was introduced by Martin Fowler ( An American software developer). it is available in Java from the EJB 3.0 by sun microsystem.

Rule :-
1) Class must be public
2) The object in the POJO Class can have any access modifies such as private, public, protected. But, all instance variables should be private for improved security of the project
3) Must have public default constructor
4) can have args constructor
5) Every property/field/variable must have publc getter and setter method
6) A POJO class should not extend predefined classes.
7) It should not implement prespecified interfaces.
8) It should not have any prespecified annotation.

Pros of Pojo classes :-
1) It increases the readability and re-usability of a java program.
2) POjo is easy to write and Understand



Example of POJO Class
public class Pojoexample
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String Name)
{
this.name = Name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}


-------------------------------------------------------------------
public class pojoextend extends Pojoexample
{
}
-------------------------------------------------------------------
public class PojorunExample
{ public static void main(String args[])
{ // Pojoexample pojoobject = new Pojoexample();
pojoextend pojoobject = new pojoextend();
pojoobject.setName("Chandan Singh");
pojoobject.setAge(30);
System.out.println(pojoobject.getName());
System.out.println(pojoobject.getAge());
}
}
Output :- Chandan Singh
30

-------------------------------------------------------------------
POJO Class Example by using Arguments Constructor
public class Pojoexample
{
private String name;
private int age;
Pojoexample(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}



POJO Vs JAVA Beans


POJO Java Beans
Pojo does not have special restrictions other than those forced by java languageJava Beans is a special POJO which have some restrictions.
It doesn't provide much control on membersIt provide complete control on members
Pojo can implement the serializable interface, but it is not mandatory The Bean class should implement the Serializable interface
Pojo class can be accessed by using their namesThe Bean class can only be accessed by using the getters and setters.
Fields may have any of the access modifiers such as public, private, protechted. Fields can only have private access.
In POJO, it may or may not have no-ar constructorIt must have a no-arg constructor
It is used when you don’t want to give restriction on your members and give user complete access of your entity It is used when you want to provide user your entity but only some part of your entity.


Interview Question

Q1). Can we extend POJO class?
Answer :- Yes, one class can extend pojo class.

Q2). What are three rule of POJO class?
Answer :- Rules are :- .
1) A POJO class should not extend predefined classes.
2) It should not implement prespecified interfaces.
3) It should not have any prespecified annotation.




Monday, 21 February 2022

Loose Coupling and Tight Coupling in Java with example

Loose Coupling and Tight Coupling in Java with example



Hello Friends,

This article will describe the concept of tight and loose coupling in java.



Coupling :- Degree of dependency of Class A on Class B.
How often do changes in one class force changes in another class.


Tight Coupling :-
Two classes often change together If class A knows more than it should about the way in which class B was implemented, then A and B are tightly coupled.


Loose Coupling :-
Reducing the dependencies of a class that uses the different classes directly.
If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled.


Example of Tight coupling
class A1
{
A1(int x , int y)
{
System.out.println("sum is " + (x+y));
}
void sam()
{
System.out.println("i am method of class A");
}
}
class B extends A1
{
B(int x, int y)
//have to add this constructor other wise while creating the object , compile time error will appear. {
super(x, y);
}
void sam()
{
System.out.println("i am method of class B");
}
}
public class Coupling
{
public static void main(String args[])
{
B obj = new B(7, 8);
obj.sam();
}
}


Example of loose coupling
interface mycoup
{
void sample();
}
class A1 implements mycoup
{
A1(int x , int y)
{
System.out.println("sum is " + (x+y));
}
@Override
public void sample()
{
System.out.println("i am method of class A");
}
}
class B implements mycoup
{
public void sample()
{
System.out.println("i am method of class B");
}
}
public class Coupling
{
public static void main(String args[])
{
B obj = new B();
obj.sample();
A1 obj1 = new A1(3, 7);
obj1.sample();
}
}

Output:-
i am method of class B
sum is 10
i am method of class A

Saturday, 12 February 2022

Upload Operation in Selenium testing the post

How to upload the file using selenium

Click Here to navigate on practice page



Hello Friends, In this tutorial we will learn how to perform upload operation in selenium.
We can perform the upload operation in selenium by two ways :
1). First by SendKeys() method.
2). Second by using the robot class.
First Method :
Precondition : Element should be input and type should be "file", as shown below :



Now we just need to locate the element and just use the method sendKeys() and in sendKeys() method we will pass the path of that file.
Syntax :-
driver.findElement(By.xpath("path of element")).sendKeys("path on file");

Here is sample code :-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h()
{
//Remember element should be input and type should be "file"
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).sendKeys("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
}
}





Second Method : By using Robot Class:-
What is Robot class ?
"Basically robot class will simulate the Keyboard and mouse actions."
Steps :-
1). First Copy the file path.
2).Paste the file path in popup window
3).Press Enter button.

by robot class we can perform CTRL + C operation by below methods as :-
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

by robot class we can perform CTRL + V operation as below :-
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);



by robot class we can press enter key as below :-
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Here is sample code :-

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h() throws AWTException
{
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).click();
Robot robot = new Robot();
robot.setAutoDelay(2000);
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}