Jun 28, 2010

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:@<HOSTNAME>: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 <Path where selenium-server jar is located>/selenium-server.jar -interactive -multiWindow -firefoxProfileTemplate "<profiles location>"

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");

References


1 comments:

Anonymous said...

it is interestingwhat is command for setting time out in .bat file

Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by