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


}

}