Sunday, December 22, 2013

Web driver Scripts

Capture Screen Shots.

public class CaptureScreenshot {
public WebDriver driver;

public void captureScreenShot(String str) throws Exception{
DateFormat dateFormat = new SimpleDateFormat("yyyy_MMM_dd HH_mm_ss");
  Date date = new Date();
String time=dateFormat.format(date);
File f = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f, new File("D:\\Workspace_Weekend_14Dec\\"+str+time+".jpg"));
}
  @Test
  public void f() throws Exception{
 driver=new FirefoxDriver();
 driver.get("http://spicejet.com");
 captureScreenShot("Homepage");
driver.findElement(By.linkText("Book a Flight")).click();
captureScreenShot("BookaFlight");
  }
}

2. Drop down Validation
package testNG;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class DropdownValidation {
public WebDriver driver;
  @Test
  public void f() {
 /*  String s[]=new String[15];
 List<String> s1=new ArrayList<String>();*/
 
 driver.get("http://spicejet.com");
 driver.findElement(By.linkText("Book a Flight")).click();
 String str="ControlGroupSearchView_AvailabilitySearchInputSearchView_DropDownListPassengerType_ADT";
List<WebElement> s=new Select(driver.findElement(By.id(str))).getOptions();
System.out.println(s.size());
for (int i = 0; i < s.size(); i++) {
//System.out.println(s.get(i).getText());
if(s.get(i).getText().equalsIgnoreCase("5 Adults"))
{
System.out.println(s.get(i).getText());
new Select(driver.findElement(By.id(str))).selectByVisibleText(s.get(i).getText());
}
}

  }
  @BeforeTest
  public void beforeTest() {
 System.setProperty("webdriver.chrome.driver", "D:\\Lib\\chromedriver.exe");
 driver=new ChromeDriver();
  }

  @AfterTest
  public void afterTest() {
  }

}

3. Mail Login register

package testNG;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class Mail_Login_Register {
public WebDriver driver;
  @Test
  public void verifyLogin() throws Exception{
driver.get("http://mail.in.com");
driver.findElement(By.id("f_id")).sendKeys("jan30selenium");
driver.findElement(By.id("f_pwd")).sendKeys("selenium");
driver.findElement(By.cssSelector("input.signin")).click();
Thread.sleep(4000);
driver.findElement(By.linkText("Sign out")).click();
  }
@Test //Test Scenario
public void verifyRegister() throws Exception{
driver.get("http://mail.in.com");
driver.findElement(By.cssSelector("input.registernow")).click();
driver.findElement(By.id("fname")).sendKeys("Nagesh");
new Select(driver.findElement(By.id("day"))).selectByVisibleText("10");
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Jun");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1983");
Thread.sleep(1000);
driver.findElement(By.xpath("(//input[@name='radiousername'])[3]")).click();
Thread.sleep(3000);
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys("nag123");
driver.findElement(By.id("altemail")).sendKeys("nag@gmail.com");
driver.findElement(By.id("imageField")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("(//input[@id='gender'])[2]")).click();
}
  @BeforeTest
  public void beforeTest() {
 System.setProperty("webdriver.chrome.driver", "D:\\Lib\\chromedriver.exe");
 driver=new ChromeDriver();
  }

  @AfterTest
  public void afterTest() {
 driver.quit();
  }

}

4. Print All links

package testNG;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class PrintAllLinks {
public WebDriver driver;
  @Test
  public void f() {
/*  String s[]=new String[15];
 List<String> s1=new ArrayList<String>();*/
 driver.get("http://spicejet.com");
 List<WebElement> str=driver.findElements(By.tagName("a"));
 System.out.println(str.size());
 for (int i = 0; i < str.size(); i++) {
System.out.println(str.get(i).getText());//str[i]
}
 
  }
  @BeforeTest
  public void beforeTest() {
 System.setProperty("webdriver.chrome.driver", "D:\\Lib\\chromedriver.exe");
 driver=new ChromeDriver();
  }

  @AfterTest
  public void afterTest() {
  }

}

5. Spice jet Pop ups

package testNG;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class Spicejet_Popup {
public WebDriver driver;
  @Test(description="Verify URL")
  public void test1() {
driver.get("http://spicejet.com");  
  }
  @Test(description = "Verify BookAFlight")
  public void test2(){
driver.findElement(By.linkText("Book a Flight")).click();
  }
  @Test(description ="Verify Oneway search")
  public void test3() throws Exception{
 driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchView_OneWay")).click();
driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1_CTXT")).click();
driver.findElement(By.linkText("Hyderabad (HYD)")).click();
Thread.sleep(1000);
driver.findElement(By.linkText("Bengaluru (BLR)")).click();
Thread.sleep(1000);
driver.findElement(By.linkText("26")).click();
Thread.sleep(1000);
new Select(driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchView_DropDownListPassengerType_ADT"))).selectByVisibleText("2 Adults");
new Select(driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchView_DropDownListPassengerType_CHD"))).selectByVisibleText("2 Children");
driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchView_ButtonSubmit")).click();
Thread.sleep(1000);
  }
  @Test(description ="Verify Confirmation Alert")
  public void test4(){
String str=driver.switchTo().alert().getText();
System.out.println(str);
driver.switchTo().alert().accept();
//driver.switchTo().alert().dismiss();
}
  @Test(description ="Verify popup")
public void test5() throws Exception{
 Thread.sleep(4000);
String windowName=driver.getWindowHandle();
driver.findElement(By.linkText("Currency Converter")).click();
driver.switchTo().window("converter");
new Select(driver.findElement(By.id("CurrencyConverterCurrencyConverterView_DropDownListBaseCurrency"))).selectByVisibleText("US Dollars(USD)");
new Select(driver.findElement(By.id("CurrencyConverterCurrencyConverterView_DropDownListConversionCurrency"))).selectByVisibleText("Indian Rupee(INR)");
driver.findElement(By.id("CurrencyConverterCurrencyConverterView_TextBoxAmount")).sendKeys("100");
Thread.sleep(1000);
//driver.findElement(By.id("ButtonCloseWindow")).click();
driver.close();
driver.switchTo().window(windowName);
new Select(driver.findElement(By.id("AvailabilitySearchInputSelectViewdestinationStation1"))).selectByVisibleText("Bhopal (BHO)");
}
  
  @BeforeTest //Precondition
  public void beforeTest() {
System.setProperty("webdriver.ie.driver", "D:\\Lib\\IEDriverServer.exe");  
 driver=new InternetExplorerDriver();
  }

  @AfterTest
  public void afterTest() {
 driver.quit();
  }

}

6. Split

package testNG;

import org.testng.annotations.Test;

public class Splits {
  @Test
  public void f() {
 /*String s1[]=new String[9];
 int j[]=new int[10];
 Object o[]=new Object[12];*/
 
 String str="seven,six,ten,one,two,three,four,five";
 String[] s=str.split(",");
 System.out.println(s.length);
for (int i = 0; i < s.length; i++) {
// System.out.println(s[i]);
if(s[i].equalsIgnoreCase("three"))
{
System.out.println(s[i]);
}
}
 
 
 
  }
}

7. Validation commands

package testNG;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.Selenium;

public class ValidationCommands {
public WebDriver driver;
public Selenium selenium;
 @Test(enabled=false)
 public void f() throws Exception{
 driver.get("http://spicejet.com");
 driver.findElement(By.linkText("Book a Flight")).click();
driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchView_OneWay")).click();
Thread.sleep(1000);
if(driver.findElement(By.id("custom_date_picker_id_2")).isDisplayed())
{
System.out.println("Available");
}else{
System.out.println("Not available");
}
 
  }
 @Test
 public void verifyUsername(){
 driver.get("http://mail.in.com");
/*if(driver.findElement(By.id("f_id")).isDisplayed()){
System.out.println("Available");
}else{
System.out.println("Not available");
}*/
Assert.assertEquals(selenium.isElementPresent("f_idcv"), true);
Assert.assertEquals(driver.findElement(By.id("f_idcv")).isDisplayed(), true);
if(selenium.isElementPresent("f_iddfg"))
{
 System.out.println("Available");
}else{
 System.out.println("Not available");
}
 }
  @BeforeTest
  public void beforeTest() {
 System.setProperty("webdriver.chrome.driver", "D:\\Lib\\chromedriver.exe");
 driver=new ChromeDriver();
 selenium=new WebDriverBackedSelenium(driver, "http://mail.in.com");
  }
}













No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...