Showing posts with label Webdriver. Show all posts
Showing posts with label Webdriver. Show all posts

Friday, 29 June 2012

Selenium WebDriver Installation


WebDriver is a clean, fast framework for automated testing of webapps. Selenium WebDriver is not tied to any particular test framework.


Google Opensource blog says: WebDriver takes a different approach to solve the same problem as Selenium. Rather than being a JavaScript application running within the browser, it uses whichever mechanism is most appropriate to control the browser. For Firefox, this means that WebDriver is implemented as an extension. For IE, WebDriver makes use of IE's Automation controls. By changing the mechanism used to control the browser, we can circumvent the restrictions placed on the browser by the JavaScript security model. In those cases where automation through the browser isn't enough, WebDriver can make use of facilities offered by the Operating System. For example, on Windows we simulate typing at the OS level, which means we are more closely modeling how the user interacts with the browser, and that we can type into "file" input elements.

For in-depth knowledge on Selenium WebDriver, see here.

Below are few links to help you in the process of Selenium WebDriver installation:
1. WebDriver for Chrome [Link]
2. Setting Up a Selenium-WebDriver Project [Link]
3. Installing Selenium Webdriver on Ubuntu 11 [Link]
4. Installing Selenium Webdriver (selenium-dotnet-2.20.0.zip) [Link]
5. Setting Up a Selenium-WebDriver Project [Link]
6. Selenium Webdriver for Python installation [Link 1] [Link 2]
7. Python - Getting Started With Selenium WebDriver on Ubuntu/Debian [Link]
8. Get started with Selenium 2 [Link]
10. Selenium WebDriver AndroidDriver [Link]

Selenium - Uploading a File

If you are trying to upload a file using Selenium and are stuck then below links may help you. Below are 10 links to help you upload a file using Selenium automation testing tool.

1.
Selenium RC with Junit framework - upload a file using attachFile() method - [Link]

2.
Selenium - Uploading Files in Remote WebDriver - [Link]

3.
Selenium - Upload files on browsers running over remote machines [Link]

4.
Selenium: Upload file in Google Chrome - [Link]

5.
How to deal with file uploading in test automation using selenium or webdriver - [Link]

6.
Webdriver: File Upload - [Link]

7.
How to upload a file using Selenium in Java - [Link]

8.
How to upload a file in selenium - AutoIT - [Link]

9.
Selenium RC: How to Upload and Submit Files Using Selenium and AutoIt - [Link]

10.
Selenium Upload/Download file handling - [Link]

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]

org.openqa.selenium.WebDriverException: Permission denied for to get property Window.frameElement

This summary is not available. Please click here to view the post.

Tuesday, 19 June 2012

Runing test script in multiple browsers using WebDriver

we can run test script in different browsers using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox chrome.





package com.web;
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.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {


private static WebDriver driver = null;

@Test
@Parameters( {"BROWSER"})
public void testread(String BROWSER)throws Exception{

System.out.println("Browser: " + BROWSER);

if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
driver.navigate().to("http://www.yahoo.com");
Thread.sleep(10000);

WebElement search= driver.findElement(By.name("p"));
search.sendKeys("automation blog by niraj");
search.submit();

Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
driver.close();

}
}


In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml



































How to select value in drop down using Webdriver

How to select values in drop down using Webdriver

This post will take you through the way you can select the value in a drop down. Webdriver does not have straight forward command "SELECT" which Selenium does.
In below example i will open home page of eBay India and search ipod in Consumer Electronic category. This page has a category drop down in which i will select 
Consumer Electronics



package com.web;
import java.io.File;
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.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DropDownSelection {
private static WebDriver driver=null;
@BeforeTest
@org.testng.annotations.Parameters({"BROWSER"})
public void setup(String BROWSER){
if(BROWSER.equals("FF")){
File profileDir = new File("C:\\nir");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPreference("general.useragent.override", "same user agent string as above");
driver= new FirefoxDriver(profile);
System.out.println("Broser Selected " + BROWSER);
}else if(BROWSER.equals("IE")){
System.out.println("Broser Selected " + BROWSER);
driver = new InternetExplorerDriver();
}else if(BROWSER.equals("HU")){
System.out.println("Broser Selected " + BROWSER);
driver =new HtmlUnitDriver();

} else
{
System.out.println("Broser Selected " + BROWSER);
driver = new ChromeDriver();
}

}
@Test
public void testDropDownSelection() throws InterruptedException{

driver.get("http://www.ebay.in");
WebElement search = driver.findElement(By.name("_nkw"));
search.sendKeys("ipod");

List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
for (WebElement option : dd) {
System.out.println(option.getText());
if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
option.click();
break;
}
}
driver.findElement(By.xpath("//input[@value='Search']")).submit();
Thread.sleep(5000);
driver.getPageSource().contains("results found");
driver.quit();
}

}




TestNG.xml 
Using TestNG suite we will run the test case.



xml version="1.0" encoding="UTF-8"?>






<parameter name="BROWSER" value="FF" />










If above code does not work with you configuration just try another way given below.



List options = driver.findElements(By.tagName("option"));
for (WebElement option : options) {
if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
option.click();
}
}



Running test case in multiple browsers parallely in WebDriver

we can run test script in different browsers parallely using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox and chrome parallely.





package com.web;
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.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelRunning {

private WebDriver driver=null;
@BeforeTest
@Parameters ({"BROWSER"})
public void setup(String BROWSER){
System.out.println("Browser: " + BROWSER);

if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
}

@Test
public void testParallel()throws Exception{

driver.get("http://www.google.com");
Thread.sleep(5000);
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"));
driver.findElement(By.xpath("//a[contains(@href,'automationtricks.blogspot.com')]")).click();
Thread.sleep(15000);
Assert.assertTrue(driver.getPageSource().contains("working as Associate Manager @CSC"));
driver.quit();

}
}



In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml






































Saturday, 16 June 2012

Testing GWT Apps with Selenium or WebDriver

Good functional testing is one of the most difficult tasks for web application developers and their teams. It is a challenge to develop tests that are cheap to maintain and yet provide good test coverage, which helps reduce QA costs and increase quality.

Both Selenium and WebDriver (which is essentially now the successor to Selenium) provide a good way to functionally test web applications in multiple target environments without manual work. In the past, web UIs were built using the page navigation to allow users to submit forms, etc. These days, more and more web applications use Ajax and therefore act and look a lot more like desktop applications. However, this poses problems for testing – Selenium and WebDriver are designed to work with user interations resulting in page navigation and don’t play well with AJAX apps out of the box.

GWT-based applications in particular have this problem, but there are some ways I’ve found to develop useful and effective tests. GWT also poses other issues in regards to simulating user input and locating DOM elements, and I discuss those below. Note that my code examples use Groovy to make them concise, but they can be pretty easily converted to Java code.

Problem 1: Handling Asynchronous Changes

One issue that developers face pretty quickly when testing applications based on GWT is detecting and waiting for a response to user interaction. For example, a user may click a button which results in an AJAX call which would either succeed and close a window or, alternatively, show an error message. What we need is a way to block until we see the expected changes, with a timeout so we can fail if we don’t see the expected changes.

Solution: Use WebDriverWait

The easiest way to do this is by taking advantage of the WebDriverWait (or Selenium’s Wait). This allows you to wait on a condition and proceed when it evaluates to true. Below I use Groovy code for the conciseness of using closures, but the same can be done in Java, though with a bit more code due to the need for anonymous classes.
def waitForCondition(Closure closure) {
int timeout = 20
WebDriverWait w = new WebDriverWait(driver, timeout)
w.until({
closure() // wait until this closure evaluates to true
} as ExpectedCondition)
}

def waitForElement(By finder) {
waitForCondition {
driver.findElements(finder).size() > 0;
}
}

def waitForElementRemoval(By finder) {
waitForCondition {
driver.findElements(finder).size() == 0;
}
}

// now some sample test code

submitButton.click() // submit a form

// wait for the expected error summary to show up
waitForElement(By.xpath("//div[@class='error-summary']"))
// maybe some more verification here to check the expected errors

// ... correct error and resubmit

submitButton.click()
waitForElementRemoval(By.xpath("//div[@class='error-summary']"))
waitForElementRemoval(By.id("windowId"))
As you can see from the example, your code can focus on the actual test logic while handling the asynchronous nature of GWT applications seamlessly.

Problem 2: Locating Elements when you have little control over DOM

In web applications that use templating (JSPs, Velocity, JSF, etc.), you have good control and easy visibility into the DOM structure that your pages will have. With GWT, this isn’t always the case. Often, you’re dealing with nested elements that you can’t control at a fine level.

With WebDriver and Selenium, you can target elements using a few methods, but the most useful are by DOM element ID and XPath. How can we leverage these to get maintainable tests that don’t break with minor layout changes?

Solution: Use XPath combined with IDs to limit scope

In my experience, to develop functional GWT tests in WebDriver, you should use somewhat loose XPath as your primary means of locating elements, and supplement it by scoping these calls by DOM ID, where applicable.

In particular, use IDs at top level elements like windows or tabs that are unique in your application and won’t exist more than once in a page. These can help scope your XPath expressions, which can look for window or form titles, field labels, etc.

Here are some examples to get you going. Note that we use // and * in our XPath to keep our expressions flexible so that layout changes do not break our tests unless they are major.
By byUserName = By.xpath("//*[@id='userTab']//*[text()='User Name']/..//input")
WebElement userNameField = webDriver.findElement(byUserName)
userNameField.sendKeys("my new user")

// maybe a user click and then wait for the window to disappear
By submitLocator = By.xpath("//*[@id='userTab']//input[@type='submit']")
WebElement submit = webDriver.findElement(submitLocator)
submit.click()

// use our helper method from Problem 1
waitForElementRemoval By.id("userTab")
Problem 3: Normal element interaction methods don’t work!

GWT and derivatives (Vaadin, GXT, etc.) often are doing some magic behind the scenes as far as managing the state of the DOM goes. To the developer, this means you’re not always dealing with plain or