Monday, 2 July 2012

Selenium Trouble Shooting Tips


Introduction

Selenium Automation testing is an emerging field that draws maximum benefits with minimum effort. The benefit of automation testing is its ability to increase the efficiency of resources, increase test coverage, and increase the quality and reliability of the software.
Selenium is an open source tool for web application testing. Selenium tests run directly in a browser, just like real users do. It runs in Internet Explorer, Mozilla Firefox on Windows, Linux, and Macintosh, Safari on the Mac.
Selenium testing suite is highly flexible. There are multiple ways to add functionality to Selenium framework to customize test automation. As compared to other test automation tools, it is Selenium’s strongest characteristic. Selenium Remote Control support for multiple programming and scripting languages allows the test automation engineer to build any logic they need into their automated testing and to use a preferred programming or scripting language of one’s choice.
Also, the Selenium testing suite is an open source project where code can be modified and enhancements can be submitted for contribution.
Components of Selenium:
  • Selenium IDE - Plugin to Firefox to record and play test in firefox and also export tests in different languages. The most appealing format is the html test case
  • Selenium RC- Allows playing of exported test in different platform/OS
  • Selenium Grid - Allows to control lots of selenium machines

Assumptions

  • Selenium is already installed and classpath variables are set .
  • The examples explained with the Selenium is used with java and junit.

Trouble Shooting Tips

To Start Selenium Server using Batch File :

  • Selenium server can be started automatically with out manually typing the start command in the command prompt.
java -jar <Path where selenium-server jar is located>/selenium-server.jar -interactive -multiWindow -firefoxProfileTemplate "<profiles location>"

  • The above command can be saved in .bat file and place in the folder where the selenium project is running
  • Use the below command in the test case setup() method, to run the selenium server automatically
Runtime.getRuntime().exec("cmd /c start SeleniumStart.BAT");
Note: Use sleep method in try- catch block so that allowing the Test case to wait for some time until the selenium server starts.
Or else, User gets error like “selenium server is not started yet”

try{Thread.sleep(10000);}catch (InterruptedException ie){System.out.println(ie.getMessage());}selenium.start();}


Establish Database Connection Using Selenium and JUnit:

  • Write a java class which establishes JDBC connection
    java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());conn=java.sql.DriverManager.getConnection("jdbc:oracle:thin:@:PORT:SID","username","password");
Write sql queries (to retieve values from database,updating queries) other class or in the same java class

  • try{conn = db.dbconnection();if (conn == null){}Statement stmt = conn.createStatement();stmt.executeQuery("select xxx from yy where zz=’’);ResultSet rs = stmt.getResultSet();while (rs.next()){setItemid(rs.getInt("xxx"));}rs.close();stmt.close();}catch (Exception e){e.printStackTrace();}return xxx;
  • When recorded, the command is as follows:
selenium.type("name",”abc”);
  • Now, the command can be reframed as below:
selenium.type("name",db.name);where ‘db’= is the object created for the java class to refer database ‘name’= variable used to store the query result

To Set Firefox profile Template

Before setting Firefox profile Template, User must know the Fire fox profile location in his/her local machine
  • Click on Start on Menu bar and Go to Run
  • Type “%APPDATA% in the field
  • Navigate up to Mozilla-> Firefox->Profiles
  • The folder which is named with .default is the Firefox profile for that machine

This profile varies from machine to machine
Note: Since Profiles is in the Hidden folder,the above process should be done to know the firefox profile location
  • Set this profile location in the selenium server start up command.
    java -jar where selenium-server jar is located>/selenium-server.jar -interactive -multiWindow -firefoxProfileTemplate ""

Session ID should not be null

  • When the Error display is like below:
Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet?
The reasons for the above error could be the following:
  • The selenium server is not started
  • The selenium server port is not given correctly
selenium = new DefaultSelenium("localhost",port,"*firefox",BaseURL);

Timeout error

  • Default selenium time out is 30000ms
  • But,sometime while running the testcase most of us gets “Time out Error”
  • To overcome this problem:
Increase the time in selenium.waitForPageToLoad() to 60000 (1 minute) or more.
  • Selenium.waitForPageToLoad(“60000”)
  • Try Thread.sleep(10000) method
  • Try selenium.setTimeout("250000") method

Frame with name “xxx” does not Exist

  • While recording with Selenium IDE, the program records as
selenium.selectFrame(("xxx");
selenium.waitForPageToLoad("30000");
But, it throws an error at the time of execution like “Frame with name “xxx” does not Exist”
  • The problem can be overcome by changing the statement like below
selenium.waitForFrameToLoad("xxx", "30000");

Proxy settings in Mozilla Firefox for Different Environment

To set the proxy settings in Mozilla Firefox, Click on Tools -> Options -> Advanced -> Network-> Settings
image
  • Click on settings.
image
  • While accessing webpages over internet then set Proxy to “Manual Proxy configuration”
  • While accessing the internal webpages then set proxy to “No proxy”
  • While accessing the Webpages with secured connection,then set proxy as “Automatic Proxy Configuration URL”

Handling PopUps

The following code will use the selectWindow, windowFocus and close functions to work on a pop up/dialog.
selenium.selectWindow("window id");
selenium.windowFocus();
selenium.type("username", "user name");
selenium.type("password", "password");
selenium.click("logingButton");
selenium.waitForPageToLoad("30000");
selenium.close();
Note: To get the window id, For loop is to be written until it writtens the required popup window id.

Selenium IDE is not working

If the recording is not working - then go to firefox and click on "Tools->Add on -> Extensions->Selenium IDE "  click Disabled
  • First disable it and then enable it and restart the browser.
  • Sometimes clicking on Selenium IDEs “Options->HTML Format” helps.

To Set Default time out in Selenium IDE

  • Default Time out while recording the test is 30000ms
selenium.waitForPageToLoad("30000");
  • The default time can be changed either to less or more like below:
  • Go to “Tools->Selenium IDE”
image
  • Click on Options -> Options
clip_image002
  • Change the value in the column “Default timeout value of recorded command”

To set Dynamically Changing ID

  • If you are testing with page where the id dynamically changes - then You may need to use lots of xpath in your test .
  • In that case install firebug and use "Inspect element" by right clicking on the element -> Copy XPath by right click on bottom-> Add "/" in front of value you got in your clipboard-> paste this as a "target" in Selenium IDE for your "clickAndWait" command.
  • You can check the validility by - click on find button in Selenium IDE to ensure that your XPath is correct(assuming that page is open)

Using "waitForCondition"

  • When you need to check if the page is loaded before continuing with the test
  • When you need to check if some some request has changed data on the page (for instance, if you're testing an auto-complete functionality)
  • This method uses two parameters - Javascript condition and timeout period.
  • Most of the time you will use "selenium" object to see if some condition is true. This object is Javascript equivalent of DefaultSelenium in Java. So, to use this command, you would write something like:
Selenium sel = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.asdf.com");sel.start();sel.open("http://www.asdf.com");sel.waitForCondition("selenium.isElementPresent('link=Home')", "5000");sel.click("link=Home");

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]

Selenium Element Locator Examples

An element Locator tells Selenium which HTML element a command refers to. The format of a locator is:

locatorType=argument 

Below are some of the awesome links to help you understand Selenium element locator in a better way:

1. Selenium Tutorial : Locators [Link]

2. Locating page elements using WebDriver [Link]

3. How to locate elements in Selenium IDE? [Link]

4. A Quick Introduction to CSS Locators in Selenium [Link]

5. Selenium Element Locators [Link]

6. Selenium Tips: Start improving your locators [Link]

7. Selenium Tips: CSS Selectors in Selenium Demystified [Link]

8. Selenium: Is it possible to use the regexp in selenium locators [Link]

9. How to use regex in selenium locators [Link]

10. Selenium IDE with an XPath Locator [Link]

11. When to use which element locator, element locator use in selenium Xpath,CSS, DOM [Link]

Selenium WebDriver Page Objects


The Page Object pattern represents the screens of your web app as a series of objects. There is a PageFactory in the support package that provides support for this pattern, and helps to remove some boiler-plate code from your Page Objects at the same time.


Below you can find few awesome links on Page Objects in order to understand these in depth:

1. Page Objects in Selenium 2.0
In this post the author looks at the difference between test specification and test implementation and how Selenium achieves this with page objects. [Link]

2. Using Page Objects with Selenium and Web Driver 2.0. [Link]

3. Good explanation of Page Objects, Implementation Notes and an example. [Link]

4. Selenium Page Object Pattern
This post makes you understand how to apply the Page Object design pattern. [Link]

5. Automated Testing Using Page Objects and WebDriver. [Link]

6. Selenium WebDriver: Page Object Pattern and PageFactory. [Link]
This post explains Page Object Pattern, main advantages of Page Object Pattern, Why use PageFactory? and much more.

7. Selenium PageObjects and PageFactory.
This article discusses the PageObject design pattern and a factory class from WebDriver support libraries. [Link]

8. Abstracting Web Page Interaction. [Link]

9. Selenium 2/Web Driver - the Land Where Page Objects are King! [Link]

10. Page Object Model – Introduction - (infact all posts related to Page Object Model, using Integrate Development Environment, called Eclipse). [Link]

11. Page Objects design issues. [Link]

12. Selenium webdriver page object.
It is tough to figure out how to define each page object BUT lets say I have figured that out and defined several page objects that represent our site.
How do you handle crossing from page to page. So I get a page object for my home page and one for my account page and one for my results page. Then I need to write a test that traverses all my pages to simulate a user performing multiple actions. [Link]

13. The PageObject pattern for Selenium WebDriver UI tests. [Link]

14. Selenium Page Objects and Abstraction (explanation that relates the page object pattern to the concept of abstraction in programming). [Link]

15. WebDriver: How to check if an page object web element exists? [Link]