Selenium Tutorial for Beginners
In this post, we offer an overview of Selenium 2.0, and how it can benefit your development process, no matter your language of choice.
Join the DZone community and get the full member experience.Join For Free
Selenium is a suite of browser automation tools for automating web browsers across a variety of platforms. It’s primarily used for automating web applications for testing purposes, although it’s capable of much more. You might be wondering: How can a Selenium tutorial improve your development and testing processes?
Most of the big-name browser vendors support it. And many of these either have taken or are taking steps to make Selenium a native component of their browsers. Additionally, it’s the core technology used in a variety of other browser automation tools, APIs, and frameworks.
Selenium runs on many browsers and operating systems, and a variety of programming languages and testing frameworks can control it. Also, Selenium can automate tedious web-based administration tasks.
The Two Primary Selenium Offerings
Selenium comes in two primary forms: the Selenium WebDriver, which enables developers to scale and distribute scripts across many different environments and create browser-based regression automation suites and tests, and the Selenium IDE, an integrated developer environment useful for creating quick bug reproduction scripts and for exploratory testing aided by automation. Selenium WebDriver is the successor to the now-deprecated Selenium Remote Control, and coupled with Selenium 1.0, comprises the current version, Selenium 2.0.
Most developers get started with Selenium IDE. This helps with gaining familiarity with Selenium commands. Using this IDE, developers can create simple tests rapidly – sometimes within mere seconds. However, Selenium’s developers don’t recommend using Selenium IDE for all testing automation purposes. They suggest building your scripts using Selenium 1.0 or Selenium 2.0 along with one of the supported programming languages. Since Selenium 2.0 is the version that will continue to be supported going forward, if you’re new to Selenium or are building a test suite from scratch, you’ll want to opt for Selenium 2.0.
Setting Up Projects in Selenium 2.0 (a.k.a. Selenium WebDriver)
Selenium WebDriver aims to overcome the limitations of the previous Selenium Remote Control, offering better support for dynamic web pages where the elements on a page are subject to changing without the page itself being reloaded. Because a variety of browsers support it, Selenium WebDriver uses each browser’s native support for automation. Thus, the features supported and the way direct calls are made depends on the browser that you’re using.
Additionally, the way that you’ll set up a Selenium WebDriver Project depends on both your programming language and your development environment. It’s possible to set up projects using several programming languages:
- Java: These projects are most easily setup using Maven, which will download Java bindings and all dependencies and create the project for you.
- C#: C# bindings are distributed as part of a set of signed dlls and dependency dlls. You can download the latest selenium-dotnet zip file here. Then, you’ll need to add a reference to each unzipped dll to your project in whatever IDE you’re using.
- Python: Run pip install selenium from a command line to add Selenium to a Python environment.
- Ruby: Run gem install selenium-webdriver from a command line to add Selenium to your Ruby environment.
- Perl: Bindings for Perl are provided through a third party, such as this example, which offers documentation on getting started.
- PHP: PHP bindings are also available through third parties, including Chibimagic, Lukasz Kolczynski, and Facebook.
Once you’ve set up your project, Selenium WebDriver functions just like a normal library. It’s self-contained and typically doesn’t require you to start additional processes or run any installers before using it. There are a few exceptions, however, with additional setup steps required for:
Creating and Editing Test Cases Using Selenium IDE
The Selenium IDE is the tool that you’ll use to develop your test cases. It’s a Firefox plugin that’s intuitive to use, containing a time-saving context menu and Selenium command options that make the process of learning Selenium script syntax simple.
Once you’ve installed Selenium IDE in Firefox, it’s available in the Firefox Tools menu. Selecting it will open a new, empty script-editing window as well as a menu for loading or creating test cases. From there, you can use one of three methods to build test cases (often, you’ll need to use all three):
- Recording test cases: This records a test case based on interactions with a website. The record button is on by default when Selenium IDE is opened, but you can turn this off in the Options menu. During recording, Selenium automatically inserts commands based on your actions, such as clicking a link (the click or clickAndWait command), entering values (the type command), selecting options from a drop-down menu (the select command), or clicking check boxes or radio buttons (the click command).
- Adding verifications and asserts: This process enables your test to check the properties of a web page using the assert and verify. To use it, right-click anywhere on the page in the browser displaying your test application and choose from the assert and verify options in the context menu. More commands get added to this menu as you continue to use Selenium IDE, which attempts to predict what command and parameters you’ll need for an UI element on a web page. Pure simplicity!
- Editing: You’ll also likely encounter the need to edit test cases and manually insert or edit commands and comments. In the table view, simply select the point in your test case where you want to insert a command, right-click, and select insert command. Selenium IDE adds a blank line ahead of your selected line where you can use the command editing text fields to enter a new command and its accompanying parameters. In source view, the process is similar: select the point where you want to enter the new command, left-click between the commands in the test pane where you want to enter it, and enter HTML code to create a three-column row with the command, the first parameter (if required), second parameter (if required to locate an element), and third parameter (if required to have a value). You can also enter comments throughout your test cases; these will be ignored when the test case is run.
Running Test Cases in Selenium IDE
Running test cases in Selenium IDE is just as simple as setting them up. There are many options for running a test case including:
- Running a test case: Simply click the “Run” button to begin running the currently displayed test case.
- Running a test suite: Click the “Run All” button to run all test cases in the test suite currently loaded.
- Stop and start: Use the “Pause” button to stop a test case currently running.
- Stop in the middle: This option enables you to set a specific breakpoint that stops a running test at a particular command. Setting a breakpoint is easy; just select a command, right-click, and then choose “Toggle Breakpoint” from the context menu.
- Start from the middle: Similar to stopping a test at a certain command, you can also set a test to begin running from a specific command. The process for setting this point is the same, but instead of choosing “Toggle Breakpoint,” you’ll select “Set/Clear Start Point” from the context menu.
- Running a single command: Double-click on any command to run it by itself, a useful tool for testing a command while you’re constructing it.
With Selenium IDE, you can also use a base URL to run test cases in different domains by specifying a relative URL instead of an absolute URL as the argument to open a test case.
To define a test suite, a collection of test cases that you want to run as a continuous batch job, you can use a simple HTML file to define your list of tests. Each row in your HTML file should define the file system path to the tests you want to include in the suite.
Once you’ve mastered the basics of Selenium, you’ll find tons of ways you can put these tools to use to improve your development and testing processes. With many advanced features and functionality, yet being quite simple for beginners to begin using and master, Selenium is a valuable suite of tools for novices and experienced developers alike. The Selenium Wiki on GitHub is a useful source for learning about many advanced capabilities this tool offers.
Published at DZone with permission of Angela Stringfellow, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.