Sunday, November 17, 2013

Selenium Web Driver Scripts

Selenium Web Driver Scripts:


1.  Find No. of Rows and columns in a Web Table.



package FAQs;

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 Totalrows {

public static void main(String[] args) throws Exception
{   
//Create an instance for webdriver

WebDriver driver = new FirefoxDriver();
//Open the URL
driver.get("http://www.google.co.in");
//Maximize the browser
driver.manage().window().maximize();

try 
{

// Finding No.of Rows in a web table

@SuppressWarnings("rawtypes")
List rows = driver.findElements(By.tagName("tr"));
System.out.println("There are "+ rows.size() + "  Rows in a google page"); 

catch (Exception e) 
{
System.out.println("error");
}

 try
 {
// Finding No.of Columns in a web table
 
 @SuppressWarnings("rawtypes")
List Colms = driver.findElements(By.tagName("td"));
System.out.println("There are "+ Colms.size() + "  Columns in a google page"); 

catch (Exception e) 
{
System.out.println("error");
}
 }

}



2. How to Upload file Script.



package FAQs;

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

public class FileUpload {

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


  String url ="http://www.2shared.com/";
  System.setProperty("webdriver.ie.driver", "C:\\Selenium Automation\\Ieserver\\IEDriverServer.exe");
 // WebDriver driver = new FirefoxDriver();
  WebDriver driver = new InternetExplorerDriver();
  driver.get(url);
  Thread.sleep(3000);
             
  String filepath = "C:\\Users\\home\\Desktop\\test.txt";
  
  driver.findElement(By.name("fff")).sendKeys(filepath);
  System.out.println("Path of the file Entered Successfully");
  Thread.sleep(3000);
  // Click on UPload button
  driver.findElement(By.xpath("//*[@id='overall']/tbody/tr[2]/td/form/input[3]")).click();
  
}

}


3. Context Click on Menu.



package FAQs;


import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class ContextClick
{

public static <E> void main(String[] args) throws InterruptedException 
{
  String url ="http://www.amazon.in/";
  System.setProperty("webdriver.ie.driver", "C:\\Selenium Automation\\Ieserver\\IEDriverServer.exe");
 // WebDriver driver = new FirefoxDriver();
  WebDriver driver = new InternetExplorerDriver();
  driver.get(url);
  Thread.sleep(3000);

  driver.findElement(By.name("field-keywords")).sendKeys("laptops");
  Thread.sleep(3000);
  driver.findElement(By.name("field-keywords")).sendKeys(Keys.ARROW_DOWN);
  Thread.sleep(3000);
  driver.findElement(By.name("field-keywords")).sendKeys(Keys.ARROW_DOWN);
  Thread.sleep(2000);
  driver.findElement(By.name("field-keywords")).sendKeys(Keys.ARROW_DOWN);
  Thread.sleep(2000);
  driver.findElement(By.name("field-keywords")).sendKeys(Keys.ENTER);
  Thread.sleep(2000);
  WebElement parentEle = driver.findElement(By.xpath("//*[@id='ref_1318473031']/li[2]/a/img"));
  
  Actions Act = new Actions(driver);
  Act.contextClick(parentEle).build().perform();
  Thread.sleep(2000);
  Act.sendKeys(Keys.ARROW_RIGHT).build().perform();
  Thread.sleep(2000);
  Act.sendKeys(Keys.ARROW_DOWN).build().perform();
  Thread.sleep(2000);
  Act.sendKeys(Keys.ARROW_DOWN).build().perform();
  Thread.sleep(2000);
  Act.sendKeys(Keys.ENTER).build().perform();
  
  int siz = driver.findElements(By.xpath("//*[@id='sort']/option")).size();
        System.out.println(siz);

       
       Thread.sleep(2000);
       // Handling multiple browsers
       @SuppressWarnings("unchecked")
Set<E> Windowhandle = (Set<E>) driver.getWindowHandles();
       Iterator<E> it = Windowhandle.iterator();
       
       String Parentwin = (String) it.next();
       String Childwin = (String) it.next();
       Thread.sleep(2000);
      
        
        Thread.sleep(2000);
       driver.switchTo().window(Childwin);
       Thread.sleep(2000);
       driver.manage().window().maximize();
       Thread.sleep(2000);
       WebElement element = driver.findElement(By.xpath("//*[@id='sort']"));
       
        Select dd= new Select(element);
        dd.selectByIndex(2);
       // dd.selectByValue("3");
      // dd.selectByVisibleText("xyz");
        Thread.sleep(2000);
      // driver.close();
       driver.switchTo().window(Parentwin);
       
}

}

4. How to Create an Actions in Web Driver ( Mouse Over to an Element in a Web Page)


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;
import org.openqa.selenium.interactions.Actions;


public class FlipKart {


public static void main(String[] args) throws InterruptedException {
String url ="http://www.flipkart.com/";
WebDriver driver = new FirefoxDriver();
driver.get(url);
// Mouse Over to an Element in a Web Page
           Actions Electronics = new Actions(driver);
           Electronics.moveToElement(driver.findElement(By.linkText("ELECTRONICS"))).build().perform();
           Thread.sleep(3000);
           driver.findElement(By.linkText("Samsung")).click();
}
}


5. How to take Screen shot in Web driver


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.ScreenshotListener;


public class ScreenShot
 {

public static void main(String[] args) throws IOException 
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

// Now you can do whatever you need to do with it, for example copy somewhere

FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
}

}


6.  How to Find No. of Elements in a Drop down & List of Elements to Print from Drop down.


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;
import org.openqa.selenium.interactions.Actions;


public class OptionsCoount {

public static void main(String[] args) throws InterruptedException {
 String url ="http://www.flipkart.com/";
  WebDriver driver1 = new FirefoxDriver();
  driver1.get(url);
          Actions Electronics = new Actions(driver1);
          Electronics.moveToElement(driver1.findElement(By.linkText("ELECTRONICS"))).build().perform();
          Thread.sleep(3000);
          driver1.findElement(By.linkText("Samsung")).click();
          
          Thread.sleep(3000);
          
          // Finding No.of Items in a drop down
          
          List<WebElement> Count = driver1.findElements(By.xpath("//*[@id='sort-dropdown']/option"));
           System.out.println("No. of elements in a drop down " +Count.size());
           Thread.sleep(3000);
           
           // Finding List Items in a drop down
           
          List <WebElement> allSuggestions = driver1.findElements(By.xpath("//*[@id='sort-dropdown']"));      
          for (WebElement suggestion : allSuggestions)
          {
         
          System.out.println("List of elements in a drop down with names " +suggestion.getText());
          }
}

}


1 comment:

  1. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
    "

    ReplyDelete

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...