Selenium Tutorials and Materials

Friday, 23 October 2015


Over time, projects tend to accumulate large numbers of tests. As the total number of tests increases, it becomes harder to make changes to the codebase --- a single "simple" change may cause numerous tests to fail, even though the application still works properly. Sometimes these problems are unavoidable, but when they do occur you want to be up and running again as quickly as possible. The following design patterns and strategies have been used before with WebDriver to help making tests easier to write and maintain. They may help you too.
  • DomainDrivenDesign: Express your tests in the language of the end-user of the app.
  • PageObjects: A simple abstraction of the UI of your web app.
  • LoadableComponent: Modeling PageObjects as components.
  • BotStyleTests: Using a command-based approach to automating tests, rather than the object-based approach that PageObjects encourage
  • AcceptanceTests: Use coarse-grained UI tests to help structure development work.
  • RegressionTests: Collect the actions of multiple AcceptanceTests into one place for ease of maintenance.
If your favourite pattern has been left out, please leave a comment on this page, and we'll try and include it. Also, please remember that these are just suggestions: in order to use WebDriver you don't need to follow these patterns and strategies, and it's likely that not all of them will be useful to you. Just use the ones that you like!


Thursday, 22 October 2015




Here i am going to describe how to download and install selenium IDE open source testing tool step by step process.

Seleniu IDE download and install step by step process 
·                     Open Mozilla Firefox Browser.
·                     Type URL : http://seleniumhq.org/download/ in your browser.Selenium IDE download page will get open then click on latestDownload version link (Here is 1.3.0) as shown in image bellow. click on that link.
·          
            


·                      
·                      
·                      When you click on latest Selenium IDE Download version link, Firefox will show one popup saying do you want to allow Mozilla Firefox to install selenium IDE or not. Click on Allow button as shown in image bellow.
·                    

·                     When you click on allow button, Firefox will automatically install Selenium IDE software. after completion ofSelenium IDE installation, it will show one pop up saying Selenium IDE installation completed. you need to restart your browser to use selenium IDE. Click on Restart Now button as show in image bellow.
·                      

·                     When you click on Restart Now button, Firefox will restart automatically. now click on Tools menu list displayed in top. you can see there Selenium IDE as shown in image bellow.
·                       

·                     Click on Selenium IDE as shown in image above. It will launch Selenium IDE software window as shown in image bellow. 
·                    
 

So these are the steps to download and install Selenium IDE. Now you can use it to record and play your web application test. Comment here if you face any problem in download and install selenium IDE. I will guide you personally. See my next post to know how to use selenium IDE

Selenium Tutorial Table of Contents, Next Chapter, Previous Chapter

Decide if you are going to write your Selenium tests in a high-level language like Java, Ruby, Python, C#, and PHP. If  your coding skills need the help of a record/playback tool to write test scripts then skip to Tutorial 5.

Writing test scripts in a high level language is easy. I recommend you learn the concepts behind Selenium PageObjects first. PageObjects is an object oriented library for building easily maintainable tests. It separates test code into a Model, View, Controller pattern. Read a blog and watch a screencast on PageObjects. Other object oriented test scripting solutions include: GEB, Fitness.

A very simple Selenium test looks like this in Java:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class temp script extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://localhost:8080/", "*iexplore");
    }
    public void testTemp script() throws Exception {
        selenium.open("/BrewBizWeb/");
        selenium.click("link=Start The BrewBiz Example");
        selenium.waitForPageToLoad("30000");
        selenium.type("name=id", "bert");
        selenium.type("name=Password", "biz");
        selenium.click("name=dologin");
        selenium.waitForPageToLoad("30000");
    }
}

The super constructor setUp tells the Selenium client library to connect to the Selenium RC service to use Microsoft Internet Explorer (*iexplore) and the base URL of http://localhost:8080/. All subsequent Selenium commands will be relative to the base URL. For example, selenium.open("/BrewBizWeb/") commands Selenium RC to tell IE to open http://localhost:8080/BrewBizWeb/.

Once you write this Java class, write an Ant script to compile the code and package it into a Java Archive Resource (JAR) file. Most Selenium users will run their Selenium tests from the Ant script itself. That makes it easy to integrate the test with a Continuous Integration environment as described in Tutorial 12.

There are great tutorials on the SeleniumHQ.org documentation site to explain Selenium test script authoring in more depth.


Selenium has many pitfalls. For example, many Selenium tutorials offer instruction to use XPath expressions to locate elements in a Web page. Do not use XPath if you can avoid it. The Microsoft Internet Explorer browsers do not feature a native XPath expression evaluator. One of my Selenium tests takes 3 minutes to run in Firefox and takes 30 minutes to run in IE depending on the XPath expressions in the Selenium script. The Selenium Load Testing screencast explains the problem and solution.

Wednesday, 21 October 2015

Automation using Selenium
Selenium Tutorial
Selenium Documentation

Once you've completed the GettingStarted tutorial, you may want to know more. This is the right place to look!
Which Implementation of WebDriver Should I Use?
WebDriver is the name of the key interface against which tests should be written, but there are several implementations. These are:
Name of driver
Available on which OS?
Class to instantiate
All
org.openqa.selenium.htmlunit.HtmlUnitDriver
All
org.openqa.selenium.firefox.FirefoxDriver
Windows
org.openqa.selenium.ie.InternetExplorerDriver
All
org.openqa.selenium.chrome.ChromeDriver
All
com.opera.core.systems.OperaDriver
You can find out more information about each of these by following the links in the table. Which you use depends on what you want to do. For sheer speed, the HtmlUnitDriver is great, but it's not graphical, which means that you can't watch what's happening. As a developer, you may be comfortable with this, but sometimes it's good to be able to test using a real browser, especially when you're showing a demo of your application (or running the tests) for an audience. Often, this idea is referred to as "safety", and it falls into two parts. Firstly, there's "actual safety", which refers to whether or not the tests works as they should. This can be measured and quantified. Secondly, there's "perceived safety", which refers to whether or not an observer believes the tests work as they should. This varies from person to person, and will depend on their familiarity with the application under test, WebDriver and your testing framework.
To support higher "perceived safety", you may wish to choose a driver such as the FirefoxDriver. This has the added advantage that this driver actually renders content to a screen, and so can be used to detect information such as the position of an element on a page, or the CSS properties that apply to it. However, this additional flexibility comes at the cost of slower overall speed. By writing your tests against the WebDriver interface, it is possible to pick the most appropriate driver for a given test.
To keep things simple, let's start with the HtmlUnitDriver:
WebDriver driver = new HtmlUnitDriver();
Navigating
The first thing you'll want to do with WebDriver is navigate to a page. The normal way to do this is by calling "get":
driver.get("http://www.google.com");
WebDriver will wait until the page has fully loaded (that is, the "onload" event has fired) before returning control to your test or script.
Interacting With the Page
Just being able to go to places isn't terribly useful. What we'd really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways of finding elements. For example, given an element defined as:
<input type="text" name="passwd" id="passwd-id" />
we could use any of:
WebElement element;
element
= driver.findElement(By.id("passwd-id"));
element
= driver.findElement(By.name("passwd"));
element
= driver.findElement(By.xpath("//input[@id='passwd-id']"));
You can also look for a link by its text, but be careful! The text must be an exact match! You should also be careful when usingXpathInWebDriver. If there's more than one element that matches the query, then only the first will be returned. If nothing can be found, aNoSuchElementException will be thrown.
WebDriver has an "Object-based" API; we represent all types of elements using the same interface: WebElement. This means that although you may see a lot of possible methods you could invoke when you hit your IDE's auto-complete key combination, not all of them will make sense or be valid. Don't worry! WebDriver will attempt to do the Right Thing, so if you call a method that makes no sense ("setSelected()" on a "meta" tag, for example) an exception will be thrown.
So, we've got an element. What can we do with it? First of all, you may want to enter some text into a text field:
element.sendKeys("some text");
You can simulate pressing the arrow keys by using the "Keys" class:
element.sendKeys(" and some", Keys.ARROW_DOWN);
It is possible to call sendKeys on any element, which makes it possible to test keyboard shortcuts such as those used on GMail. A side-effect of this is that typing something into a text field won't automatically clear it. Instead, what you type will be appended to what's already there. You can easily clear the contents of a text field or textarea:
element.clear();
Filling In Forms
We've already seen how to enter text into a textarea or text field, but what about the other elements? You can "toggle" the state of checkboxes, and you can use "setSelected" to set something like an OPTION tag selected. Dealing with SELECT tags isn't too bad:
WebElement select = driver.findElement(By.xpath("//select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
   
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
    option
.click();
}
This will find the first "SELECT" element on the page, and cycle through each of it's OPTIONs in turn, printing out their values, and selecting each in turn. As you can see, this isn't the most efficient way of dealing with SELECT elements. WebDriver's support classes come with one called "Select", which provides useful methods for interacting with these.
Once you've finished filling out the form, you probably want to submit it. One way to do this would be to find the "submit" button and click it:
driver.findElement(By.id("submit")).click();  // Assume the button has the ID "submit" :)
Alternatively, WebDriver has the convenience method "submit" on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn't in a form, then the "NoSuchElementException" will be thrown:
element.submit();
Drag And Drop
You can use drag and drop, either moving an element by a certain amount, or on to another element:
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();
Moving Between Windows and Frames
It's rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the "switchTo" method:
driver.switchTo().window("windowName");
All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window's name? Take a look at the javascript or link that opened it:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
Alternatively, you can pass a "window handle" to the "switchTo().window()" method. Knowing this, it's possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
  driver
.switchTo().window(handle);
}
You can also swing from frame to frame (or into iframes):
driver.switchTo().frame("frameName");
It's possible to access subframes by chaining switchTo() calls, and you can specify the frame by its index too. That is:
driver.switchTo().frame("frameName")
     
.switchTo().frame(0)
     
.switchTo().frame("child");
would go to the frame named "child" of the first subframe of the frame called "frameName". All frames are evaluated as if from currently switched to frame. To get back to the top level, call:
driver.switchTo().defaultContent();
Navigation: History and Location
Earlier, we covered navigating to a page using the "get" command (driver.get("http://www.example.com")) As you've seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it's simply a synonym to:
driver.navigate().to("http://www.example.com");
To reiterate: "navigate().to()" and "get()" do exactly the same thing. One's just a lot easier to type than the other!
The "navigate" interface also exposes the ability to move backwards and forwards in your browser's history:
driver.navigate().forward();
driver
.navigate().back();
Please be aware that this functionality depends entirely on the underlying browser. It's just possible that something unexpected may happen when you call these methods if you're used to the behaviour of one browser over another.
Cookies
Before we leave these next steps, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:
// Go to the correct domain
driver
.get("http://www.example.com");

// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver
.manage().addCookie(cookie);

// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
   
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}