Saturday, 22 September 2018

Creating the Maven with Cucumber project

Creating the Maven with Cucumber project


For creating the Maven with cucumber project, follow the below navigations
Eclipse
                                New file ->
Others ->
Maven->
Maven project

Click on finish


NOTE:
·         Group Id: Unique name of the project across all projects
·         Artifact Id: Name of the jar
·         Version: Version of the jar

Copy dependencies

                Copy all dependencies into project file of pom.xml as mentioned Section 1.1


Create Feature File

A. Create Feature folder and under the folder create. feature file i.e., Test.feature
B. Write the applications Scenarios under. feature file as follows


C. Run the feature file, will generate the cucumber methods as per the business requirements in mentioned,

Feature: login action

  Scenario: Successful login to cyclos  # E:/Partha/Cucumber/MavenwithCucumberProject/Feature/TestCucumber.feature:3
    Given open the Browser
    When User able to access the URL
    And enter the username and password
    Then Applition successful login

1 Scenarios (1 undefined)
4 Steps (4 undefined)
0m0.000s
@Given("^open the Browser$")
public void open_the_Browser() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^User able to access the URL$")
public void user_able_to_access_the_URL() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^enter the username and password$")
public void enter_the_username_and_password() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^Applition successful login$")
public void applition_successful_login() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();

D. Create TestRunner.java class under the TestRunner package and the .java file should contain the code snippet

RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "Feature",
glue = {"StepDefinition"})


Create StepDefinition package and list of .java class as follows
- Create a .java file (HomePage.java) The purpose of this .java file is to store the application URL and make sure that, this class should extends from PageObject in order to preserve the objects
Code Snippet:
public class HomePage extends PageObject{

}


-          Create anther .java file i.e., BuyerStep, purpose of this .java file is to invoke the application URL
Code Snippet:
              HomePage homePage = new HomePage();                                         
   
              @Test//@Step                                                      
              public void openurl() {
                  homePage.open();
              }
}

-Create another .java file where we will write Business logics, and need to copy the cucumber generated methods


-          Copy browser executable files in to the project folder
-          Create. properties file, where we will specify on which browser will invoke the application.
-          Run the Test Runner file where it will execute as per the scenarios (requirements).

Issues facing while configuring Maven with Cucumber

                Unable to build the pom.xml sometimes, to fix this issue please make sure your Eclipse Market place file having the Natural 0.7.6 is installed (used for BDD and ATTD). 

Maven With Cucumber Installation

Cucumber Configuration in Eclipse IDE


Configuration steps:
Open the Eclipse IDE -> Help-> install New software



Click on Add -> Provide Name and Location and provide the cucumber source as follows




Check the Cucumber Eclipse Plugin


Proceed the installation by giving install, YES and OK

Cross checking cucumber installed successful or not, if it’s successfully installed the Cucumber will be listed under preference left side pane as follows

Now Cucumber is configured successfully



Maven With Cucumber


Maven with Cucumber

  What is BDD

ü  Behaviour-driven development focus on the what test on the application.
ü  BDD uses plain English text to explain the exact requirements
ü  BDD uses the language called Gherkins
ü  In the market the BDD can implement by
·         Cucumber (Java),
·         Jbehave (Java),
·         SpecFlow (dot net environment) etc.,

What is Cucumber

ü  Cucumber is a behavior driven development (BDD) approach to automation test scripts to test Desktop, Web and Android Apps.
ü  It often develops and execute acceptance, Functional, Regression and unit tests.
ü  Cucumber makes to understand the eclipse the Gherkin syntax and works like a syntax highlighter.
ü  It highlights main syntax in feature file and it is more readable.
ü  We can run the feature file independently
ü  The official URL is  https://docs.cucumber.io/   -> (cucumber.io/docs/reference)
ü  Required list of.jar files for cucumber 
·         Cobertura-2.1.1
·         Cucumber-core-1.1.2
·         Cucumber-java-1.1.2
·         Cucumber –junit-1.1.2
·         Cucumber–jvm-deps-1.0.3
·         Cucumber-reporting-0.1.0
·         Gherkin2.12.1
·         Junit-4.11
·         Mockito-all-2.0.2

The Complete jar files with 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>MavenwithCucumberProject</groupId>
  <artifactId>MavenwithCucumberProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

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

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

  <dependencies>
  
  
  <!-- 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-java -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>gherkin</artifactId>
    <version>2.12.2</version>
    <scope>provided</scope>
</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-jvm-deps -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-jvm-deps</artifactId>
    <version>1.0.5</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-cucumber -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>1.9.12</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Few keywords, which are used to define in the feature file about the requirement i.e.,
·         Given
·         When
·         Then
And etc.

Friday, 2 February 2018

Field Validation for Alert Box when passing invalid inputs using Selenium

 Field Validation for Alert Box when passing invalid inputs using Selenium



 class name text
{
public static void main(String[] args) {

WebDriver z=new FirefoxDriver();
z.manage().window().maximize();
z.get("");
z.findElement(By.id("FIRSTNAME")).sendKeys("!@#$%^&*");
z.findElement(By.id("Button1")).click(); 

String at=z.switchTo().alert().getText();

System.out.println(at);

if(at.equals("First name Should not contain Special Characters"))

{

System.out.println("pass");

z.switchTo().alert().dismiss();

}
else
System.out.println("false");
z.findElement(By.id("FIRSTNAME")).clear();
z.findElement(By.id("FIRSTNAME")).sendKeys("TESTING");
z.findElement(By.id("Button1")).click(); 

String at1=z.switchTo().alert().getText();

System.out.println(at1);
if(at1.equals("First name Should not contain Special Characters"))
{
System.out.println("pass");
}
else
System.out.println("false");
z.switchTo().alert().dismiss();
}
}

JXL: How to Read the Input in Excel-Read and Write Concept Using Selenium Tool

JXL: 

It is a data Driven Testing to test the Element with Multiple inputs

It support .xls file format

There is two operations in that Read and write concepts


write:                                                                                      Read:

WritableWorkbook---------------classname--------------------Workbook
createWorkbook----------------methodname-----------------getWorkbook
 createSheet-------------------methodname---------------------getSheet
WritableSheet--------------classname--------------------------Sheet
Label---------------classname                                      getcell.getcontents(read input in row and col)
addCell-----------------methodname                            close--->methodname
write----------methodname
close------------methodname


Write Program: 

In the write program to create a Excel,Sheet and pass the Inputs Automatically.

public class write {
public static void main(String[] args) throws IOException, RowsExceededException, WriteException {

WritableWorkbook wb=Workbook.createWorkbook(new File("d:\\driver\\test.xls"));
WritableSheet sh=wb.createSheet("anandh", 0); //("sheetname",sheetindex)
WritableSheet sh1=wb.createSheet("sathish", 1);
Label d=new Label(0,0,"selenium"); //(column,row,"inputs")
Label d1=new Label(1,2,"testing");
Label d2=new Label(0,0,"selenium");
Label d3=new Label(1,2,"testing");
sh.addCell(d);
sh.addCell(d1);
sh1.addCell(d2);
sh1.addCell(d3);
wb.write();
wb.close();
}


Read Program: 

In the Read Program to read the Inputs in Excel,Sheet and in the particular row and column.

public class read {
public static void main(String[] args) throws BiffException, IOException, InterruptedException {
// TODO Auto-generated method stub
Workbook wb=Workbook.getWorkbook(new File("d:\\driver\\data.xls"));
Sheet e=wb.getSheet(0);
for(int i=1;i<=2;i++)
{
WebDriver d=new FirefoxDriver();
d.manage().window().maximize();
d.get("URL");
d.findElement(By.id("Email")).sendKeys(e.getCell(0,i).getContents());
d.findElement(By.id("next")).click();
Thread.sleep(3000);
d.findElement(By.id("Passwd")).sendKeys(e.getCell(1,i).getContents());
d.findElement(By.id("signIn")).click();
d.close();
}
}


Note:  in the Jxl it read the inputs in col,row

e.getCell(0,i).getContents()

0--->column
i--->Row

How to Count No of Countries in Dropdwon box Using Selenium Code

How to Count No of Countries in Dropdwon box Using Selenium Code:


  • To Find Out no of countries in Dropdown box Using getOptions Method is better. 
  • Using Select class to find the Element
  • Using List concept and get the options from Dropdown.

public class noodropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver g=new FirefoxDriver();
g.manage().window().maximize();
g.get(""); 
Select s=new Select(g.findElement(By.id()));
List<WebElement> f=s.getOptions();
System.out.println(“no of links is:”f.size());
for(int i=0;i<f.size();i++)
{
System.out.println(f.get(i).getText());
}
}
}

To Find out no of links in Home Page Using Selenium


 To Find out no of links in Home Page Using Selenium

  • To Count No of Links Using tagName Locator
  • Most of the Links are Developed using the Anchor code

<a href="links">link Name</a>

<a>---------->Anchor Tag.

First We have to collect all the anchor tag in WebElement using tagName Loator and stored in the List Concept(Array-java)

Then Using Size Concept to count the links and print in the console 

FindElement--->For Single Element

FindElements--->To collect Multiple Elements

public class nooflinks {
public static void main(String[] args) {

WebDriver g=new FirefoxDriver();
g.manage().window().maximize();
g.get("");
List<WebElement>f=g.findElements(By.tagName(“a”));
System.out.println(“no of links is:”f.size());
for(int i=0;i<f.size();i++)
{
System.out.println(f.get(i).getText());
}
}
}

Cross Browser Testing Using TestNG Concept in Selenium

CrossBrowser Testing: 

To Test the Application in Different Browsers in Parallel.

Requirements:

TestNg Class

Browser's:Chrome,Firefox,IE

XML Suite File:For passing the parameters to the class.

Annotations:@Parameters,@BeforeMethod,@AfterMethod.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters; import
org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class cross {
WebDriver g;
@Test
public void f() {
g.manage().window().maximize();
g.get("Mention Ur Url");
}
@Parameters("browser")
@BeforeTest
public void beforeTest(String browser) throws Exception
{
if(browser.equalsIgnoreCase("firefox"))
{
g=new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver","d:\\driver1\\chromedriver.exe");
g=new ChromeDriver();
}
else if(browser.equalsIgnoreCase("ie"))
{
System.setProperty("webdriver.ie.driver", "d:\\driver1\\IEDriverServer.exe");
g=new InternetExplorerDriver();
}
else{
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
g.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterTest
public void afterTest() {
g.close();

XML Suite File: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="test.cross">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="test.cross">
</class>
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="IE" />
<classes>
<class name="test.cross">
</class>
</classes>
</test>
</suite> 

Note: When give parallel="tests" it run the browser simulataneously

or When parallel="false" it run the browser one by one.



 

Apache POI-How to Read the Inputs and Print the Output in Excel Using Apache POI in Selenium

Introduction of Apache POI:

It is used to Create,Modify Edit the MS office files Using Java Programs.

It is open source library developed by Apache software foundation

Mainly It contain Two classes

HSSFWorkbook--->Horriable Style Sheet Format

It Support only .xls File Format in the Version(97-2003)

XSSFWorkbook----->XML SpreadSheet Format 


It Support only .xlsx and .xls File Format in the Version(97-2003)&(above 2010 versions) 

In the Below Program How to Read the Input From Excel and Print the Output in Excel

Here I am Creating Two Classes

1)Excel Util

In the inside of class creating Four Methods
a) setexcel--->To Declare a Method for To Open the Excel and Sheet
b)getcell------>To  Declare a Method for read the Inputs in the Particular row and column
c)setcell------->To Declare a Method for Print the Method In the Particular row and column
d)setexcel1 ------->To Declare a Method for To close the Excel and Sheet

2)Inputs  

To Call all the Methods in the Input class using inheritance concept and Test the Elemenets with multiple inputs and print the output in Excel

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Inputs extends ExcelUtil {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        ExcelUtil.setexcel("d:\\test.xlsx","Sheet0");
       
        WebDriver g=new FirefoxDriver();
       
        g.get("http://newtours.demoaut.com/");
       
        for(int i=1;i<=2;i++)
        {
       
        g.findElement(By.name("userName")).sendKeys(ExcelUtil.getcell(i,0));
       
        g.findElement(By.name("password")).sendKeys(ExcelUtil.getcell(i, 1));
       
        g.findElement(By.name("login")).click();
       
        String at=g.getTitle();
       
        if(at.equals("test"))
        {
            ExcelUtil.setcell("pass", i, 2);
        }
        else
        {
            ExcelUtil.setcell("fail", i, 2);
        }
       
        ExcelUtil.setexcel1("d:\\test.xlsx","Sheet0");
        }
       
    }

}


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ExcelUtil {
   

public static XSSFWorkbook wb;

public static XSSFSheet sh;

public static XSSFRow r;

public static XSSFCell c;

public static void setexcel(String path,String Sheetname) throws IOException

{
    FileInputStream fs = new FileInputStream(path);
    // Access the required test data sheet
    wb = new XSSFWorkbook(fs);
    sh = wb.getSheet(Sheetname);
}

public static String getcell(int row,int col)
{
c=sh.getRow(row).getCell(col);
   
    String value = null;
   
    if(c.getCellType() == c.CELL_TYPE_NUMERIC) {
        int i = (int)c.getNumericCellValue();
        value = String.valueOf(i);
    }
        else if(c.getCellType()==c.CELL_TYPE_NUMERIC)
        {
            long d= (long)c.getNumericCellValue();
            value=String.valueOf(d);
        }
         else  {
        value = c.getStringCellValue();
        }
   
    return value;
}

public static void setcell(String result,int rno,int cno)
{
     r  = sh.getRow(rno);
     c = r.getCell(cno, r.RETURN_BLANK_AS_NULL);
                if (c == null) {
                    c = r.createCell(cno);
                    c.setCellValue(result);
                    } else {
                        c.setCellValue(result);
}
}

public static void setexcel1(String paths,String sheets) throws

{
    FileOutputStream fileOut = new FileOutputStream(paths);
    wb.write(fileOut);
    fileOut.flush();
  fileOut.close();
}
   
   
}