Sunday 24 May 2015

Selenium Framework: 1 (Using Test Case scripts, Generic Library's, Test Data)

Scenario: Login to actiTIME
                Create a customer
                And verify the customer name
                Expected result: Customer should be created
Scenario: Login to actiTIME
                Create a project for the customer
                And verify the project name
                Expected result: Project should be created for the customer.
===================================================================
ClassName: ProjectAndCustomerTest.java

package com.actitime.custandprojtest;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.actitime.genericlib.Excellib;

public class ProjectAndCustomerTest {
Excellib cell=new Excellib();
@Test
public void createCustomerTest() throws EncryptedDocumentException, InvalidFormatException, IOException
{
String expVal="HDFC";
String username=cell.getExcelData("Sheet1", 1, 2);
String password=cell.getExcelData("Sheet1", 1, 3);
String customername=cell.getExcelData("Sheet1", 1, 4);
WebDriver driver=new FirefoxDriver();
   driver.get("http://hunter/login.do");
   driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
   driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(password);
   driver.findElement(By.id("loginButton")).click();
 
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//div[text()='Tasks']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//a[contains(text(),'Projects & Customers')]")).click();
   driver.findElement(By.xpath("//input[@value='Create New Customer']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys(customername);
   driver.findElement(By.xpath("//input[@value='Create Customer']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   String actVal = driver.findElement(By.xpath("//a[text()='HDFC']")).getText();
 
   Assert.assertEquals(actVal, expVal,"Not Found");
   cell.setExcelData("Sheet1", 1, 5, "PASS");
   driver.quit();
 
}

@Test
public void createProjectTest() throws EncryptedDocumentException, InvalidFormatException, IOException
{
String username=cell.getExcelData("Sheet1", 2, 2);
String password=cell.getExcelData("Sheet1", 2, 3);
String projname=cell.getExcelData("Sheet1", 2, 6);
String expVal="Banking";
WebDriver driver=new FirefoxDriver();
   driver.get("http://hunter/login.do");
   driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
   driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(password);
   driver.findElement(By.id("loginButton")).click();
 
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//div[text()='Tasks']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//a[contains(text(),'Projects & Customers')]")).click();
   driver.findElement(By.xpath("//td[table[tbody[tr[td[a[text()='HDFC']]]]]]/following-sibling::td/a[text()='add project']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys(projname);
   driver.findElement(By.xpath("//input[@value='Create Project']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   String actVal = driver.findElement(By.xpath("//a[text()='Banking']")).getText();
   Assert.assertEquals(actVal, expVal, "Not found");
   cell.setExcelData("Sheet1", 2, 5, "PASS");
   driver.quit();
}
}
========================================================================
Excel Library

package com.actitime.genericlib;

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

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Excellib {
String filename="C:\\Users\\NHCE\\Desktop\\MyInput.xlsx";
public String getExcelData(String sheetName,int rowNum,int cellNum) throws EncryptedDocumentException, InvalidFormatException, IOException
{
FileInputStream fis = new FileInputStream(filename);
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet(sheetName);
Row rw = sh.getRow(rowNum);
String data = rw.getCell(cellNum).getStringCellValue();
return data;
}
@SuppressWarnings("static-access")
public void setExcelData(String sheetName, int rowNum, int cellNum, String cellValue) throws EncryptedDocumentException, InvalidFormatException, IOException
{
FileInputStream fis = new FileInputStream(filename);
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet(sheetName);
Row rw = sh.getRow(rowNum);
Cell cel = rw.createCell(cellNum);
cel.setCellType(cel.CELL_TYPE_STRING);
FileOutputStream fos = new FileOutputStream(filename);
cel.setCellValue(cellValue);
wb.write(fos);
}

}

Friday 22 May 2015

Apache POI:: Using the excel sheet for storing the values and using POI accessing the excel sheet and fetching the values.

package TestNG;

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

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.testng.annotations.Test;

public class AcceptInputFromPoiTest {
  @Test
  public void startExec() throws EncryptedDocumentException, InvalidFormatException, IOException {
 //Step 1: Giving the path to the workbook
 FileInputStream fis = new FileInputStream("C:\\Users\\NHCE\\Desktop\\MyInput.xlsx");

 //Step 2: Opening the workbook
 Workbook wb = WorkbookFactory.create(fis);

 //Step 3: Opening the respective sheet
 Sheet sh = wb.getSheet("Sheet1");

 //Step 4 : Finding the row
 Row rw = sh.getRow(1);

 //Step 5 : Finding the respective cell

 String userName = rw.getCell(2).getStringCellValue();
 String passWord = rw.getCell(3).getStringCellValue();
 String Cust = rw.getCell(4).getStringCellValue();

 System.out.println(userName+" "+passWord+" "+Cust);

  }
}

Monday 18 May 2015

Scenario: login to gmail, click on compose, attach a file using autoIT + WebDriver

package WebdriverDemo2;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

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

public class GmailAttachXpath {

public static void main(String[] args) throws InterruptedException, IOException {
WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://accounts.google.com/");

driver.findElement(By.id("Email")).sendKeys("manju.naidu13@gmail.com");

driver.findElement(By.id("Passwd")).sendKeys("7411677660");

driver.findElement(By.id("signIn")).click();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Runtime.getRuntime().exec("C:\\Users\\NHCE\\Desktop\\autoIT\\upload.exe");

driver.findElement(By.xpath("//div[@class='a1 aaA aMZ']")).click();

Thread.sleep(4000);

Runtime.getRuntime().exec("C:\\Users\\NHCE\\Desktop\\autoIT\\upload.exe");


//Runtime.getRuntime().exec("cmd /c start C:\\Users\\NHCE\\Desktop\\upload.exe");




}

}


========================================
AutoIT script:

WinWaitActive("File Upload")
Send("C:\Users\NHCE\Desktop\images.jpg")
ControlClick("File Upload","","Button1")

Friday 15 May 2015

Working with google auto suggest and select an option from the auto suggested links.

package Assignments;

import java.util.List;

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


public class CapturingAllAutoSuggested {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
String exp = "wipro wiki";

driver.findElement(By.id("lst-ib")).sendKeys("wipro");

List<WebElement> lst = driver.findElements(By.xpath("//div[div[div[div[div[input[@id='lst-ib']]]]]]/following-sibling::div/div/div/div/ul/li"));

for(WebElement option : lst)
{
String s = option.getText();
System.out.println(s);

if(s.equals(exp))
{
System.out.println("Trying to select: "+exp);
                        option.click();
                        break;
}


}


}

}

Wednesday 13 May 2015

Handling SSL on InternetExplorer

package Assignments;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class SSLIePopupHandle {

public static void main(String[] args) {


System.setProperty("webdriver.ie.driver", "C:\\Users\\NHCE\\Desktop\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://ess5-white.echo360.com:8443/ess");

driver.navigate().to("javascript:document.getElementById('overridelink').click()");



}

}

Selenium script to enable add-on on firefox browser.!

File file = new File("C:\\Users\\NHCE\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\eayvhft0.default\\extensions\\firebug@software.joehewitt.com.xpi");
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
profile.setPreference("extensions.firebug.currentVersion", "2.0.9");

WebDriver driver = new FirefoxDriver(profile);

WAP to enter "wipro" in google search editbox and capture all the auto suggested list and display it on console.

package Assignments;

import java.util.List;

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

public class CapturingAllAutoSuggested {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");

driver.findElement(By.id("lst-ib")).sendKeys("wipro");

List<WebElement> lst = driver.findElements(By.xpath("//div[div[div[div[div[input[@id='lst-ib']]]]]]/following-sibling::div/div/div/div/ul/li"));

for(int i=0;i<lst.size();i++)
{
String s = lst.get(i).getText();
System.out.println(s);
}


}

}

Monday 11 May 2015

Truely amazing!! "How to hadle multiple frames in webDriver"

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


public class FrameExampleDemo {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//pass the driver control to frame-1
driver.switchTo().frame("classFrame");

//perform operation on frame-1
driver.findElement(By.linkText("com.thoughtworks.selenium")).click();

//pass the driver control to top-window(Parent)
driver.switchTo().defaultContent();

//pass the driver control to frame-2
driver.switchTo().frame("packageFrame");

//perform the operation on frame-2
driver.findElement(By.linkText("AddWebStorage")).click();



}

}

Saturday 9 May 2015

ActiTIME scenario--> Create customer, create a project, create the task for the respective project and verify the same.

package Assignments;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Assignment {

public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
   String expected="sai";


   driver.get("http://hunter/login.do");
   driver.findElement(By.xpath("//input[@name='username']")).sendKeys("admin");
   driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys("manager");
   driver.findElement(By.id("loginButton")).click();
   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
   driver.findElement(By.xpath("//div[contains(text(),'Tasks')]")).click();
 
   driver.findElement(By.xpath("//a[text()='Projects & Customers']")).click();
   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

   driver.findElement(By.xpath("//input[@value='Create New Customer']")).click();
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys("sai");
 
   driver.findElement(By.xpath("//input[@value='Create Customer']")).click();
   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

   driver.findElement(By.xpath("//input[@value='Create New Project']")).click();
   Select sel=new Select(driver.findElement(By.xpath("//select[@name='customerId']")));
   List<WebElement> web=sel.getOptions();
   for(int i=0;i<web.size();i++){
    //String actual=web.get(i).getText();
    if(web.get(i).getText().equals(expected)){
    sel.selectByVisibleText(web.get(i).getText());
   
    }
   }
 
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys("tiger");
 
   driver.findElement(By.xpath("//input[@value='Create Project']")).click();
   driver.findElement(By.xpath("//td[table[tbody[tr[td[a[text()='tiger']]]]]]/following-sibling::td/a")).click();
 
   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

   driver.findElement(By.xpath("//input[@name='task[0].name']")).sendKeys("manual testing on login");
   driver.findElement(By.xpath("//input[@name='task[1].name']")).sendKeys("SQL testing");
   driver.findElement(By.xpath("//input[@name='task[2].name']")).sendKeys("automation testing");
   driver.findElement(By.xpath("//input[@value='Create Tasks']")).click();

   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//a[text()='Open Tasks']")).click();
 
   driver.findElement(By.xpath("//td[@id='cpSelector.cpButton.contentsContainer']")).click();
   driver.findElement(By.xpath("//label[span[text()='sai']]/preceding-sibling::input")).click();
   driver.findElement(By.xpath("//input[@id='tasksFilterSubmitButton']")).click();
 
   String very = driver.findElement(By.linkText("automation testing")).getText();

           if(act.equals(very)
                   {
                                System.out.println("PASS!");
                   }else{
                                System.out.println("FAIL!");
                   }
 
  driver.findElement(By.xpath("//a[text()='Logout']")).click();
  driver.quit();
 
 



}

}

Thursday 7 May 2015

Calender handling in "makemytrip.com"..

package Assignments;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MakeMyTrip {

public static void main(String[] args) throws InterruptedException {
java.util.Scanner scn = new java.util.Scanner(System.in);
System.out.println("Enter the date");
int date = scn.nextInt();

System.out.println("Enter the month");
String month = scn.next();

System.out.println("Enter return date");
int date1 = scn.nextInt();

System.out.println("Enter return month");
String month1 = scn.next();


WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
int count = 0, count1 =0;
Actions act = new Actions(driver);

WebElement fromele = driver.findElement(By.id("from_typeahead1"));
fromele.click();
fromele.sendKeys("ban");
act.sendKeys(Keys.ENTER);

driver.findElement(By.id("to_typeahead1")).sendKeys("hyd");
act.sendKeys(Keys.ENTER);

Thread.sleep(2000);

driver.findElement(By.xpath("//span[text()='DEPARTURE']")).click();

String datePicker = "//div[div[div[span[text()='"+month+"']]]]//a[text()='"+date+"']";

while(count<=12)
{
try
{
driver.findElement(By.xpath(datePicker)).click();
break;
}
catch(Exception e)
{
driver.findElement(By.xpath("//a[@title='Next']")).click();
Thread.sleep(2000);
count++;
}

}

driver.findElement(By.xpath("//span[text()='RETURN']")).click();

String retdatePicker = "//div[div[div[span[text()='"+month1+"']]]]//a[text()='"+date1+"']";

while(count1<=12)
{
try
{
driver.findElement(By.xpath(retdatePicker)).click();
break;
}
catch(Exception e)
{
driver.findElement(By.xpath("//span[text()='Next']")).click();
Thread.sleep(2000);
count1++;
}

}


}

}

Wednesday 6 May 2015

Working indetail with "optgroup"!! Awesome work.!

package WebdriverDemo2;



import java.util.List;


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

public class Naukri {


public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
String exp = "Chandigarh";

driver.get("file:///C:/Users/NHCE/Desktop/Sample.html");

WebElement wele = driver.findElement(By.xpath("//optgroup[@label='------Punjab------']"));

List<WebElement> lst = wele.findElements(By.tagName("option"));

for(WebElement allopt : lst)
{
if(allopt.getText().equals(exp))
{
allopt.click();
}
}
}
}

Tuesday 5 May 2015

My Fav::: Handing multiple windows in irctc.co.in [Combination of both actions and window handling] Expected result of this script is ccopying the text and displaying it on the console.

package Assignments;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class IrctcMultiWin {

public static void main(String[] args) {
//Step1

WebDriver driver = new FirefoxDriver();
driver.get("https://irctc.co.in");

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

//step2

Actions act = new Actions(driver);

WebElement tele = driver.findElement(By.xpath("//span[contains(text(),'Tourism')]"));

act.moveToElement(tele).perform();


//step3


driver.findElement(By.xpath("//a[text()='Tourist Train']")).click();

Set<String> set1 = driver.getWindowHandles();

//step4

Iterator<String> it = set1.iterator();
String parentId = it.next();
String childId = it.next();

//step5

driver.switchTo().window(childId);

//step6

driver.findElement(By.xpath("//h1[contains(text(),'Bharat Darshan')]/following-sibling::a[text()='Know More']")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Set<String> set2 = driver.getWindowHandles();

//step7

Iterator<String> it1 = set2.iterator();
parentId = it1.next();
childId = it1.next();
String subChildId = it1.next();

//step8

driver.switchTo().window(subChildId);

//step9

String webTxt = driver.findElement(By.xpath("//p[contains(text(),'Hall')]")).getText();

System.out.println(webTxt);

driver.close();

//step10

driver.switchTo().window(childId);
driver.close();

//step11

driver.switchTo().window(parentId);
driver.findElement(By.id("usernameId")).sendKeys("sample@irctc.co.in");
driver.findElement(By.className("loginPassword")).sendKeys("hdhdyhd");

}

}

Saturday 2 May 2015

Functional testing on gmail login page using automation.

package Assignments;

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

public class FuncTestOnGmail {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://gmail.com");

String Expcase1 = "Enter your email address";
String Expcase2 = "Enter your password";
String Expcase3 = "The email or password you entered is incorrect";

//Case 1

driver.findElement(By.id("Passwd")).sendKeys("gsgsgs");
driver.findElement(By.id("signIn")).click();
String Actcase1 = driver.findElement(By.xpath("//span[contains(text(),'Enter your email address.')]")).getText();
if(Actcase1.contains(Expcase1))
{
System.out.println("Case 1 PASS!!");
}
else
{
System.out.println("Case 1 FAIL!!");
}
driver.get("http://gmail.com");
Thread.sleep(4000);


//Case 2

driver.findElement(By.id("Email")).sendKeys("duhudhe");;
driver.findElement(By.id("signIn")).click();
String Actcase2 = driver.findElement(By.xpath("//span[contains(text(),'Enter your password')]")).getText();
if(Actcase2.contains(Expcase2))
{
System.out.println("Case 2 PASS!!");
}
else
{
System.out.println("Case 2 FAIL!!");
}
driver.get("http://gmail.com");
Thread.sleep(4000);


//Case 3

driver.findElement(By.id("Email")).sendKeys("ydgygdycgd");
driver.findElement(By.id("Passwd")).sendKeys("hxcgdygy");
driver.findElement(By.id("signIn")).click();
String Actcase3 = driver.findElement(By.xpath("//span[contains(text(),'The email or password you entered is incorrect')]")).getText();
if(Actcase3.contains(Expcase3))
{
System.out.println("Case 3 PASS!!");
}
else
{
System.out.println("Case 3 FAIL!!");
}

driver.quit();

}

}


Selenium script to add a book to the cart in flipkart.

package Assignments;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class FlipKartMouseOver {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.get("http://flipkart.com");
String expRes = "Oxford English Mini Dictionary";

//Finding the respective tab
WebElement wbele = driver.findElement(By.xpath("//span[contains(text(),'Books & Media')]"));

//creating an object for actions class
Actions act = new Actions(driver);
act.moveToElement(wbele).perform();

//navigating to entrance exam section
driver.findElement(By.xpath("//a[contains(text(),'Entrance Exam')]")).click();

Thread.sleep(4000);

//finding a book with name "oxford eng mini dic"
driver.findElement(By.xpath("//a[contains(text(),'Oxford English Mini Dictionary (English) 7th Edition')]")).click();

//adding the book to cart
driver.findElement(By.xpath("//div[@class='shop-section']/div/div/div/div/div/div/form/input[@value='Add to Cart']")).click();

Thread.sleep(4000);

//navigating to cart to verify the book is added or not
driver.findElement(By.xpath("//span[contains(text(),'Cart')]")).click();

Thread.sleep(4000);

String actRes = driver.findElement(By.xpath("//span[contains(text(),'Oxford English Mini Dictionary (English) 7th')]")).getText();

if(actRes.contains(expRes))
{
System.out.println("Test case ---- PASS!!");
}
else
{
System.out.println("Test case ---- FAIL");
}

}

}