Wednesday, February 26, 2014

Opening Firefox,Chrome and IE browsers- in a single run

package seleniumExamples;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Open3Browsers {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
//open firefox browser
       
       // System.setProperty("webdriver.Firefox.driver", ""))
        WebDriver driver = new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver", "C:\\Users\\Public\\Jars\\ChromeDriver.exe");
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\sreekanth\\Desktop\\Technical\\Selenium\\Selenium training\\chromedriver_win32\\chromedriver.exe");
        //System.setProperty("webdriver.chrome.driver","C:\\ChromeDriver.exe");
        WebDriver driverchrome = new ChromeDriver();
       
        //IE driver is found in the link: "http://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_x64_2.38.0.zip"
        System.setProperty("webdriver.ie.driver", "C:\\Users\\sreekanth\\Desktop\\Technical\\Selenium\\Selenium training\\IEDriverServer_Win32_2.38.0\\IEDriverServer.exe");
        //System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
        WebDriver driverIe = new InternetExplorerDriver();
    }

   

}

Validating an image--- with the xpath of an image.

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class ImagevalidationinFirefox {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
WebDriver FF1; // Create WebDriver element
FF1= new FirefoxDriver(); //Create Firefox Webelement
FF1.get("http://www.homedepot.com"); //Open the homedepot.com website
//validating the homedepot logo it is an image.
if(FF1.findElement(By.xpath("//img[@src='http://www.homedepot.com/static/theme/v3/images/headerFooter/hd-logo.png']")).isDisplayed())
    System.out.println("image is displayed");
//The else statement never executes
else
    System.out.println("No, image is not dispalyed");
        //http://www.homedepot.com/static/theme/v3/images/headerFooter/hd-logo.png
    }
}

Opening website in firefox, Searching for word 'trimmer'( in the field) and click button

//here i would like to open "homedepot" website, search for trimmer and
//also i would like to know what all items available in website, such as categories..by hitting links.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebElement;

public class Homedepotwebsitetesting {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new FirefoxDriver();
      //WebDriver driver = FirefoxDriver();
        driver.get("http://homedepot.com");
       
        WebElement element1= driver.findElement(By.xpath("//input[@id='searchFocus']")); //finding element- field
        element1.sendKeys("trimmer"); //entering word 'trimmer' on the webelement- search field
       
        driver.findElement(By.xpath("//button[@id='searchButton']")).click(); //find searchbutton and click i
        //WebElement element2 = driver.findElement(By.xpath("//button[@id='searchButton']")); //find searchbutton and click it
       //element2.click();
       
    }

}

Opening a weib site through Chrome driver


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumwebdriverExwithChrome {

    public static void main(String[] args) {
       
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");//C:\\chromedriver.exe -- is the path of drivers on my machine
        //System.setProperty("webdriver.chrome.driver", "C:\\Users\sreekanth\Desktop\Technical\Selenium\Selenium training\chromedriver_win32\chromedriver.exe");
//firefox does not need driver to be downloaded by chrome need drivers to be downloaded from seleniumhq.com site.
WebDriver driver = new ChromeDriver();
/*
driver.get("http://www.google.com"); //it is able to open google website
//driver.get("www.google.com"); //Issue: it is not opening google site
//driver.close(); // it is able to close browser
System.out.println("The web page title is: " + driver.getTitle());
final String title = driver.getTitle();
System.out.println(title); */
    }

}

Validating text on the Webelement(Field) on the web page

import org.openqa.selenium.By; //.By - here meanse importing By methond from selenium package

import org.openqa.selenium.WebElement;//.WebElement - importing WebElement method from selenium package
//For accessing webelements on web page
import org.openqa.selenium.WebDriver;//.WebDriver - importing WebDriver element from selenium package
//WebDriver is used to access drivers like Firefox driver, chrome .....
import org.openqa.selenium.firefox.*;
//Expecially for firefoxdriver
public class TextValidationinthewebpage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
WebDriver driver1 = new FirefoxDriver();//open firefox
driver1.get("http://homedepot.com");//open the website

//WebElement element1= driver.findElement(By.xpath("//input[@id='searchFocus']"));
//element1.sendKeys("trimmer");
WebElement element2 = driver1.findElement(By.xpath("//label[@id='lblSearch']")); //looking for SearchAll field
//element2.click();
String s= element2.getText(); //extracting default text of searchall field
System.out.println(s);
if(s.equals("What can we help you find?")) //string comparison
    //if(s.equalsIgnoreCase("What can we help you find.?")) //this will compare by ignoring case
System.out.println("test case pass");
else
System.out.println("test case fail");



//if (s=="What can we help you find?") then

    }

}