Showing posts with label WebDriver. Show all posts
Showing posts with label WebDriver. Show all posts

Friday, October 5, 2012

Selenium RC vs WebDriver

Selenium RC (Selenium 1):
Selenium uses JavaScript to automate web pages. This lets it interact very tightly with web content, and was one of the first automation tools to support Ajax and other heavily dynamic pages. However, this also means Selenium runs inside the JavaScript sandbox. This means you need to run the Selenium-RC server to get around the same-origin policy, which can sometimes cause issues with browser setup.

WebDriver (Selenium 2):

WebDriver on the other hand uses native automation from each language. While this means it takes longer to support new browsers/languages, it does offer a much closer ‘feel’ to the browser. If you’re happy with WebDriver, stick with it, it’s the future. There are limitations and bugs right now, but if they’re not stopping you, go for it.

Selenium Benefits over WebDriver
  • Supports many browsers and many languages, WebDriver needs native implementations for each new languagte/browser combo.
  • Very mature and complete API
  • Currently (Sept 2010) supports JavaScript alerts and confirms better
Benefits of WebDriver Compared to Selenium
  • Native automation faster and a little less prone to error and browser configuration
  • Does not Requires Selenium-RC Server to be running
  • Access to headlessHTMLUnit can allow really fast tests
  • Great API

Saturday, September 15, 2012

How to take screenshot of a page using webdriver?

How to take screenshot of a page using webdriver?

 Here is a sample code to get the screenshot of the current browser window using webdriver ...

The file name with the specific path should mentioned. Either it should be dynamic or we can hard-code the file name ( every time we run this code, the file will be overriden).


    public static void snapshot() throws IOException{
       
        File fi = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(fi, new File("file name with the path should be mentioned here... "));
    }

It is very simple and we call this method when ever we need (Pass or Fail cases) to take the screenshots of the pages.

Hope this is helpful.. :)

How to maximize window size of current window using webdriver?

How to maximize window size of current window  using webdriver?

Generally when we start the browsers, it will open based on its previous state. Some times it is customized to certain size (width and height) manually. So in order to make the browser opens in a maximized window, we need to use Toolkit class.

Maximize window option will allows the user to see the complete application under test.

Here is the snippet to achieve the maximized window using Webdriver...

Here driver is WebDriver object for Firefox or IE driver.

WebDriver driver = new Firefoxdriver();

public static void maximizeWindow(){
        Toolkit t = Toolkit.getDefaultToolkit();
        org.openqa.selenium.Dimension screenResolution = new org.openqa.selenium.Dimension((int)t.getScreenSize().getWidth(), (int)t.getScreenSize().getHeight());
        driver.manage().window().setSize(screenResolution);
   
    }

Saturday, September 1, 2012

How to drag and drop elements using webdriver?

How to drag and drop elements using webdriver?
 
If we want to drag and drop from one element to other element.

Below is the small snippet code....

WebDriver driver  = new FirefoxDriver();

driver.get("URL");

Actions builder = new Actions(driver);

builder.clickAndHold().moveToElement(ele).release().build().perform();

how to mouse hover on any element using webdriver

how to mouse hover on any element using webdriver?

Mouse over using WebDriver little different. And also the given solution may not work with the latest versions of FF 12+. We need to wait for the latest version of the selenium jar.


//driver = new InternetExplorerDriver();
// driver = new FirefoxDriver();
driver.get("URL");


Using Actions:

        Actions builder = new Actions(driver);
        WebElement ele = driver.findElement(By.xpath("Specify xpath"));
        builder.moveToElement(ele).build().perform();
        WebElement ele2 = driver.findElement(By.xpath("Specify xpath"));
        ele2.click();


Friday, August 31, 2012

How do we configure proxy to our browser?

If we need to use a proxy. How do we configure that?

Proxy configuration is done via the org.openqa.selenium.Proxy class like so:

Proxy settings need to set, when an application runs under proxy. And also we can set
the Desired Capabilities for any given browser. 

Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://youdomain/config");
// We use firefox as an example here.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// You could use any webdriver implementation here
WebDriver driver = new FirefoxDriver(capabilities);
 

Thursday, July 26, 2012

selenium screenshots using webdriver

Taking Screenshots with Selenium WebDriver

Take a screenshot with Selenium WebDriver

Testers do take screenshots while testing, its important in many ways. So can you take screenshots using Selenium WebDriver? The answer is yes. Below you can find few good links that show you how to take screenshots with Selenium WebDriver:

1. This StackOverflow link has an example from Java, Python, Ruby, Jython [Link]

2. Taking screenshots with Selenium WebDriver (Selenium Client Driver for C#) [Link]

3. Taking screenshot of flash object using Selenium with Webdriver [Link]

4. Taking a Screenshot with Selenium Remote Webdriver - Java, Csharp, Python, Ruby [link]

5. If capture screenshot functionality with remote webdriver implementation throws Class Cast Exception [java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.TakesScreenshot], then take a look here.

6. How to take an OS level screen capture using ruby selenium webdriver [Link]

7. How to Take Screenshots during Selenium Test Execution [Link]

Wednesday, July 25, 2012

How to locate Web Elements or UI Elements in a page

First of all inorder to locate the web elements we need to open or fetch (open) a web page. We can do this
by
 driver.get("http://www.google.com");


Web Elements:

Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings expose a “Find Element” and “Find Elements” method. The first returns a WebElement object otherwise it throws an exception. The latter returns a list of WebElements, it can return an empty list if no DOM elements match the query.
The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.

By ID:
Ex:
< span=""> id="coolestWidgetEvah">... <>
Using Java:  
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
 



By Class Name:
Ex:
< span=""> class="cheese">Cheddar <>
< span=""> class="cheese">Gouda <>

Using Java:
List<WebElement> cheeses = driver.findElements(By.className("cheese")); 

By Tag Name:

The DOM Tag Name of the element.
Ex

How to handle popups in WebDriver

public boolean testNewWindow(){
 String currentHandle = driver.getWindowHandle();
 Set handles = driver.getWindowHandles();
 handles.remove(currentHandle);
 if (handles.size() > 0) {
 try{
 driver.switchTo().window(handles.iterator().next());
 return true;
 }
 catch(Exception e){
 System.out.println(e.getMessage());
 return false;
 }
 }
 System.out.println("Did not find window");
 return false;
 }

WebDriver

WebDriver:

WebDriver is a selenium 2.0 and it has all cool features like

1. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
2. Selenium-WebDriver, drives the browser directly using the browser’s built in support for automation.
3. No need to start the server.
4. Works with higher versions of firefox.
5. Android and iPhone based testing.
6. We can do object extraction in Bulk.

WebDriver and Selenium Server:

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server.. more