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

}

}