Tuesday, April 6, 2010

Selenium FAQS and Tutorials

Assertion or Verification?

Effectively an “assert” will fail the test and abort the current test case, whereas a “verify” will fail the test and continue to run the test case.

Q1. What is Selenium?

Ans. Selenium is a set of tools that supports rapid development of test automation scripts for webbased applications. Selenium testing tools provides a rich set of testing functions specifically designed to fulfil needs of testing of a web based application.

Q2. What are the main components of Selenium testing tools?
Ans. Selenium IDE, Selenium RC and Selenium Grid

Q3. What is Selenium IDE?
Ans.
Selenium IDE is for building Selenium test cases. It operates as a Mozilla Firefox add on and provides an easy to use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back.

Q4. What is the use of context menu in Selenium IDE?
Ans. It allows the user to pick from a list of assertions and verifications for the selected location.

Q5. Can tests recorded using Selenium IDE be run in other browsers?
Ans.
Yes. Although Selenium IDE is a Firefox add on, however, tests created in it can also be run in other browsers by using Selenium RC (Selenium Remote Control) and specifying the name of the test suite in command line.

Q6. What are the advantage and features of Selenium IDE?

Ans. 1. Intelligent field selection will use IDs, names, or XPath as needed
2. It is a record & playback tool and the script format can be written in various languages including

C#, Java, PERL, Python, PHP, HTML
3. Auto complete for all common Selenium commands
4. Debug and set breakpoints
5. Option to automatically assert the title of every page
6. Support for Selenium user-extensions.js file

Q7. What are the disadvantage of Selenium IDE tool?
Ans.
1. Selenium IDE tool can only be used in Mozilla Firefox browser.
2. It is not playing multiple windows when we record it.

Q8. What is Selenium RC (Remote Control)?
Ans.
Selenium RC allows the test automation expert to use a programming language for maximum flexibility and extensibility in developing test logic. For example, if the application under test returns a result set and the automated test program needs to run tests on each element in the result set, the iteration / loop support of programming language’s can be used to iterate through the result set, calling Selenium commands to run tests on each item. Selenium RC provides an API and library for each of its supported languages. This ability to use Selenium RC with a high level programming language to develop test cases also allows the automated testing to be integrated with the project’s automated build environment.

Q9. What is Selenium Grid?

Ans. Selenium Grid in the selenium testing suit allows the Selenium RC solution to scale for test suites that must be run in multiple environments. Selenium Grid can be used to run multiple instances of Selenium RC on various operating system and browser configurations.

Q10. How Selenium Grid works?

Ans. Selenium Grid sent the tests to the hub. Then tests are redirected to an available Selenium RC, which launch the browser and run the test. Thus, it allows for running tests in parallel with the entire test suite.

Q 11. What you say about the flexibility of Selenium test suite?
Ans.
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.

Q12. What test can Selenium do?
Ans.
Selenium is basically used for the functional testing of web based applications. It can be used for testing in the continuous integration environment. It is also useful for agile testing

Q13. What is the cost of Selenium test suite?
Ans.
Selenium test suite a set of open source software tool, it is free of cost.

Q14. What browsers are supported by Selenium Remote Control?
Ans.
The test automation expert can use Firefox, IE 7/8, Safari and Opera browsers to run tests in Selenium Remote Control.

Q15. What programming languages can you use in Selenium RC?
Ans.
C#, Java, Perl, PHP, Python, Ruby

Q16. What are the advantages and disadvantages of using Selenium as testing tool?
Ans.
Advantages: Free, Simple and powerful DOM (document object model) level testing, can be used for continuous integration; great fit with Agile projects.

Disadvantages: Tricky setup; dreary errors diagnosis; can not test client server applications.

Q17. What is difference between QTP and Selenium?
Ans.
Only web applications can be testing using Selenium testing suite. However, QTP can be used for testing client server applications. Selenium supports following web browsers: Internet Explorer, Firefox, Safari, Opera or Konqueror on Windows, Mac OS X and Linux. However, QTP is limited to Internet Explorer on Windows.QTP uses scripting language implemented on top of VB Script. However, Selenium test suite has the flexibility to use many languages like Java, .Net, Perl, PHP, Python, and Ruby.

Q18. What is difference between Borland Silk test and Selenium?
Ans.
Selenium is completely free test automation tool, while Silk Test is not. Only web applications can be testing using Selenium testing suite. However, Silk Test can be used for testing client server applications. Selenium supports following web browsers: Internet Explorer, Firefox, Safari, Opera or Konqueror on Windows, Mac OS X and Linux. However, Silk Test is limited to Internet Explorer and Firefox.Silk Test uses 4Test scripting language. However, Selenium test suite has the flexibility to use many languages like Java, .Net, Perl, PHP, Python, and Ruby.

Assertion or Verification?

Effectively an “assert” will fail the test and abort the current test case, whereas a “verify” will fail the test and continue to run the test case.

Default Locators

You can choose to omit the locator type in the following situations:

· Locators starting with “document” will use the DOM locator strategy. See Locating by DOM

· Locators starting with “//” will use the XPath locator strategy. See Locating by XPath.

· Locators that start with anything other than the above or a valid locator type will default to using the identifier locator strategy. See Locating by Identifier.

Store Commands and Selenium Variables

You can use Selenium variables to store constants at the beginning of a script. Also, when combined with a data-driven test design (discussed in a later section), Selenium variables can be used to store values passed to your test program from the command-line, from another program, or from a file.

The plain store command is the most basic of the many store commands and can be used to simply store a constant value in a selenium variable. It takes two parameters, the text value to be stored and a selenium variable. Use the standard variable naming conventions of only alphanumeric characters when choosing a name for your variable.

Command

Target

Value

store

paul@mysite.org

username

Later in your script, you’ll want to use the stored value of your variable. To access the value of a variable, enclose the variable in curly brackets ({}) and precede it with a dollar sign like this.

Command

Target

Value

verifyText

//div/p

${userName}

A common use of variables is for storing input for an input field.

Command

Target

Value

type

id=login

${userName}

Selenium variables can be used in either the first or second parameter and are interpreted by Selenium prior to any other operations performed by the command. A Selenium variable may also be used within a locator expression.

An equivalent store command exists for each verify and assert command. Here are a couple more commonly used store commands.

storeElementPresent

This corresponds to verifyElementPresent. It simply stores a boolean value–”true” or “false”–depending on whether the UI element is found.

storeText

StoreText corresponds to verifyText. It uses a locater to identify specific page text. The text, if found, is stored in the variable. StoreText can be used to extract text from the page being tested.

storeEval

This command takes a script as its first parameter. Embedding JavaScript within Selenese is covered in the next section. StoreEval allows the test to store the result of running the script in a variable.

echo - The Selenese Print Command

Selenese has a simple command that allows you to print text to your test’s output. This is useful for providing informational progress notes in your test which display on the console as your test is running. These notes also can be used to provide context within your test result reports, which can be useful for finding where a defect exists on a page in the event your test finds a problem. Finally, echo statements can be used to print the contents of Selenium variables.

Command

Target

Value

echo

Testing page footer now.


echo

Username is ${userName}


Selenium-RC

Introduction

Selenium-RC is the solution for tests that need more than simple browser actions and linear execution. Selenium-RC uses the full power of programming languages to create more complex tests like reading and writing files, querying a database, emailing test results.

You’ll want to use Selenium-RC whenever your test requires logic not supported by Selenium-IDE. What logic could this be? For example, Selenium-IDE does not directly support:

  • condition statements
  • iteration
  • logging and reporting of test results
  • error handling, particularly unexpected errors
  • database testing
  • test case grouping
  • re-execution of failed tests
  • test case dependency
  • screenshot capture of test failures

Although these tasks are not supported by Selenium directly, all of them can be achieved by using programming techniques with a language-specific Selenium-RC client library.

Run Selenese Directly Within the Server Using –htmlSuite:

java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "c:\absolute\path\to\my\HTMLSuite.html" "c:\absolute\path\to\my\results.html"

Handling HTTPS and Security Popups

Many applications switch from using HTTP to HTTPS when they need to send encrypted information such as passwords or credit card information. This is common with many of today’s web applications. Selenium-RC supports this.

To ensure the HTTPS site is genuine, the browser will need a security certificate. Otherwise, when the browser accesses the AUT using HTTPS, it will assume that application is not ‘trusted’. When this occurs the browser displays security popups, and these popups cannot be closed using Selenium-RC.

When dealing with HTTPS in a Selenium-RC test, you must use a run mode that supports this and handles the security certificate for you. You specify the run mode when your test program initializes Selenium.

In Selenium-RC 1.0 beta 2 and later use *firefox or *iexplore for the run mode. In earlier versions, including Selenium-RC 1.0 beta 1, use *chrome or *iehta, for the run mode. Using these run modes, you will not need to install any special security certificates; Selenium-RC will handle it for you.

Database Validations

Since you can also do database queries from your favorite programming language, assuming you have database support functions, why not using them for some data validations/retrieval on the Application Under Test?

Consider example of Registration process where in registered email address is to be retrieved from database. Specific cases of establishing DB connection and retrieving data from DB would be:

In Java:

// Load Microsoft SQL Server JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 
// Prepare connection url.
String url = "jdbc:sqlserver://192.168.1.180:1433;DatabaseName=TEST_DB";
 
// Get connection to DB.
public static Connection con =
DriverManager.getConnection(url, "username", "password");
 
// Create statement object which would be used in writing DDL and DML
// SQL statement.
public static Statement stmt = con.createStatement();
 
// Send SQL SELECT statements to the database via the Statement.executeQuery
// method which returns the requested information as rows of data in a
// ResultSet object.
 
ResultSet result =  stmt.executeQuery
("select top 1 email_address from user_register_table");
 
// Fetch value of "email_address" from "result" object.
String emailaddress = result.getString("email_address");
 
// Use the fetched value to login to application.
selenium.type("userid", emailaddress);

This is very simple example of data retrieval from DB in Java. A more complex test could be to validate that inactive users are not able to login to application. This wouldn’t take too much work from what you’ve already seen.

Selenium Server Logging

Server-Side Logs

When launching selenium server the -log option can be used to record valuable debugging information reported by the Selenium Server to a text file.

java -jar selenium-server.jar -log selenium.log

This log file is more verbose than the standard console logs (it includes DEBUG level logging messages). The log file also includes the logger name, and the ID number of the thread that logged the message. For example:

20:44:25 DEBUG [12] org.openqa.selenium.server.SeleniumDriverResourceHandler -
Browser 465828/:top frame1 posted START NEW

The message format is

TIMESTAMP(HH:mm:ss) LEVEL [THREAD] LOGGER - MESSAGE

This message may be multiline.

Browser-Side Logs

JavaScript on the browser side (Selenium Core) also logs important messages; in many cases, these can be more useful to the end-user than the regular Selenium Server logs. To access browser-side logs, pass the -browserSideLog argument to the Selenium Server.

java -jar selenium-server.jar -browserSideLog

-browserSideLog must be combined with the -log argument, to log browserSideLogs (as well as all other DEBUG level logging messages) to a file.

Specifying the Path to a Specific Browser

You can specify to Selenium-RC a path to a specific browser. This is useful if you have different versions of the same browser, and you wish to use a specific one. Also, this is used to allow your tests to run against a browser not directly supported by Selenium-RC. When specifying the run mode, use the *custom specifier followed by the full path to the browser’s executable:

*custom 

Server Command Line options

Usage:

$ java -jar selenium-server.jar [-interactive] [options]

Options:

-port

The port number the selenium server should use (default 4444)

-timeout

An integer number of seconds before we should give up

-interactive

Puts you into interactive mode. See the tutorial for more details.

-singleWindow

Puts you into a mode where the test web site executes in a frame. This mode should only be selected if the application under test does not use frames.

-profilesLocation


Specifies the directory that holds the profiles that java clients can use to start up selenium. Currently supported for Firefox only.

-forcedBrowserMode


Sets the browser mode (e.g. “*iexplore” for all sessions, no matter what is passed to getNewBrowserSession

-forcedBrowserModeRestOfLine


Sets the browser mode to all the remaining tokens on the line (e.g. “*custom /some/random/place/iexplore.exe”) for all sessions, no matter what is passed to getNewBrowserSession

-userExtensions


Indicates a JavaScript file that will be loaded into selenium

-browserSessionReuse


Stops re-initialization and spawning of the browser between tests

-avoidProxy

By default, we proxy every browser request; set this flag to make the browser use our proxy only for URLs containing ‘/selenium-server’

-firefoxProfileTemplate


Normally, we generate a fresh empty Firefox profile every time we launch. You can specify a directory to make us copy your profile directory instead.

-debug

Puts you into debug mode, with more trace information and diagnostics

-browserSideLog


Enables logging on the browser side; logging messages will be transmitted to the server. This can affect performance.

-ensureCleanSession


If the browser does not have user profiles, make sure every new session has no artifacts from previous sessions. For example, enabling this option will cause all user cookies to be archived before launching IE, and restored after IE is closed.

-trustAllSSLCertificates


Forces the Selenium proxy to trust all SSL certificates. This doesn’t work in browsers that don’t use the Selenium proxy.

-log

Writes lots of debug information out to a log file

-htmlSuite

Run a single HTML Selenese (Selenium Core) suite and then exit immediately, using the specified browser (e.g. “*firefox”) on the specified URL (e.g. “http://www.google.com“). You need to specify the absolute path to the HTML test suite as well as the path to the HTML results file we’ll generate.

-proxyInjectionMode


Puts you into proxy injection mode, a mode where the selenium server acts as a proxy server for all content going to the test application. Under this mode, multiple domains can be visited, and the following additional flags are supported:

-dontInjectRegex


An optional regular expression that proxy injection mode can use to know when to bypss injection

-userJsInjection


Specifies a JavaScript file which will then be injected into all pages

-userContentTransformation


A regular expression which is matched against all test HTML content; the second is a string which will replace matches. These flags can be used any number of times. A simple example of how this could be useful: if you add “-userContentTransformation https http” then all “https” strings in the HTML of the test application will be changed to be “http”.

No comments:

Post a Comment