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();
}
   
   
}