Thursday, October 10, 2013

important methods in webdriver

Hello Friends,

Hope you have now familiar with Web-driver and and how to automate a web page. Now will see some important methods in webdriver.

#implicit wait - it will wait for 10sec
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

#explicit wait
1. Here cond return true
Type 1:
WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.id("id_of_web_element")));   

WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.className("class_of_element")));

Type 2           
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
@Override
    public WebElement apply(WebDriver d) {
    return driver.findElement(By.id( "myDynamicElement" ));
    }});

Type 3
(new WebDriverWait(driverS, 10)).until(new ExpectedCondition<Boolean>() {
             public Boolean apply(WebDriver d) {
            return d.getTitle().toLowerCase().startsWith("Letsbuy");
            }
            });






#By ID
WebElement element=driver.FindElement(By.id(BuyNowButton));


#By Class Name
List<WebElements> buttons=driver.FindElements(By.className("buttons"));


#By Tag Name
WebElement iframe=driver.FindElement(By.tagName("iframe"));


#By Name
WebElement name=driver.FindElement(By.name("firstName")); 

#By LinkText
<a href="www.google.com/selenium.php">open-selenium</a>
WebElement link=driver.FindElement(By.linkText("open-selenium"));

#By Partial Link Text
<a href="www.google.com/selenium_setup.php">open selenium webdriver setup</a>
WebElement link=driver.FindElement(By.partialLinkText("setup"));

#By CSS   [NEED MORE INFO ???]
-->CSS may vary from browser to browser hence not prefered.
-->browser native support for CSS will be used and if brwoser not supporting CSS then "Sizzle" will be used. eg IE 6,7 and FF 3.0 use Sizzle as search engine.

<div id= "food" ><span class= "dairy" >milk</span><span class= "dairy aged" >cheese</span>
WebElement cheese = driver.findElement(By.cssSelector( "#food span.dairy.aged" ));

#By Xpath       
WebElement element = driver.findElements(By.xpath("//div[contains(text(),'Click Payment History')]"))
WebElement element = driver.findElements(By.xpath("//div[@class=\"articleHeader\"])).get(2);

#To get Hidden field
//input[@type="hidden"][@id="FinalPrice"]/@value
//input[@id="FinalPrice"]@value

#Relative XPath:
<table id="serviceInvocations" width="100%" style="display: block; height: 250px; overflow: auto;">
A: ".//*[@id='serviceInvocations']/tbody/tr[3]/td[1]"  


#Xpath Functions:
//div[@id='summaryTip']//div[@class='xpop-content'][text()='You will see a summary of your transfer as you proceed. ']
driver.findElement(By.xpath(".//*[contains(text(),'Sign out')]")).click();



Few More - Xpath:
1. Verify button visibilty [class='select'] get appended if button is enable
String xpBrands = "//ul[@id='views']/li[@class='selected' and position()=1]/div/p/a";

2. Verify count of similar element
String xpTopScrollLinks = "//div[@id='topscrolllinks']/div/a";
int topScrollLinksCount = driver.findElements(By.xpath(xpTopScrollLinks)).size();

//div[@class='promote_favorite']/div[@id='fav_brand_775321 and @class='favorite faved']



Regards
Sheetal :)

start with webdriver

Hello Friends,

Here we are going to learn how to start automating a simple web page using webdriver 2.0.
I am assuming you have installed webdriver and java.

Here we will read a text file which contains n number of links and fetch title of web page.


//code
package com.test.webdriver;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;


public class Titles{
    WebDriver driver;
    private List<String> links = new ArrayList<String>();
      
    public void initialize(){  
        try {
            BufferedReader br = new BufferedReader(new FileReader("href_file.txt"));
            String str = "";
            while ((str = br.readLine()) != null) {
                links.add(str);
            }
            br.close();
        } catch (FileNotFoundException fnf) {
            System.out.println("File Not Found" + fnf);
        } catch (IOException io) {
            System.out.println("IO Exception: " + io);
        }
    }
      
    public void openUrls(){
        try{
            for(String url: links){
//              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                driver.get(url);
                System.out.println(driver.getCurrentUrl());        
            }     
        }catch(Exception e){
            e.printStackTrace();
        }
    }
   
    public static void main(String args[]) {
        Titles obj=new Titles();
        driver=new FirefoxDriver();
        obj.initialize();
        obj.openUrls();
    }

}


Please note that "href_file.txt" should present at the same level of your project i.e. above your package, else eclipse will not be able to find file.


Regards
Sheetal