selenium tutorial

23
Selenium Tutorial Business Technology Labs

Upload: mayank-pande

Post on 18-Jun-2015

318 views

Category:

Technology


2 download

DESCRIPTION

Selenium Tool Description

TRANSCRIPT

Page 1: Selenium tutorial

Selenium Tutorial

Business Technology Labs

Page 2: Selenium tutorial

Introduction

• There is a Firefox extension called Selenium IDE made by the folks over at OpenQA.org.• Selenium IDE is an integrated development environment for performing Selenium tests. • Selenium tests can be written as HTML tables or coded in various languages like C#, PHP, Perl,

Python and can be run directly in most modern browsers.• It is a very easy to use and powerful tool for controlling, automating or testing web sites. If

there is any repetitive or predictable task that you are always doing in your browser why not let the Selenium IDE handle it for you.

• The IDE can help you to record, edit and debug tests. This is all really very easy, and you don't need to know anything about computer programming to use it (although it's a plus if you do).

• Currently the IDE is only available for Firefox (as if we developers will use any other) as a add-on.

Page 3: Selenium tutorial

Installation of Selenium IDE

• Using Firefox, first, download the IDE from the http://seleniumhq.org/download/• Firefox will protect you from installing addons from unfamiliar locations, so you

will need to click ‘Allow’ to proceed with the installation, as shown in the following screenshot.

Page 4: Selenium tutorial

• When downloading from Firefox, you’ll be presented with the following window. Select Install Now. The Firefox Add-ons window pops up, first showing a progress bar, and when the download is complete, displays the following. Restart Firefox. After Firefox reboots you will find the Selenium-IDE listed under the Firefox Tools menu.

Page 5: Selenium tutorial

Supported Browsers

Browser Selenium IDE Selenium 1 (RC) Operating Systems

Firefox 3.x Record and playback tests Start browser, run tests Windows, Linux, Mac

Firefox 3 Record and playback tests Start browser, run tests Windows, Linux, Mac

Firefox 2 Record and playback tests Start browser, run tests Windows, Linux, Mac

IE 8 Test execution only via Selenium RC* Start browser, run tests Windows

IE 7 Test execution only via Selenium RC* Start browser, run tests Windows

IE 6 Test execution only via Selenium RC* Start browser, run tests Windows

Safari 4 Test execution only via Selenium RC Start browser, run tests Windows, Mac

Safari 3 Test execution only via Selenium RC Start browser, run tests Windows, Mac

Safari 2 Test execution only via Selenium RC Start browser, run tests Windows, Mac

Opera 10 Test execution only via Selenium RC Start browser, run tests Windows, Linux, Mac

Opera 9 Test execution only via Selenium RC Start browser, run tests Windows, Linux, Mac

Opera 8 Test execution only via Selenium RC Start browser, run tests Windows, Linux, Mac

Google Chrome Test execution only via Selenium RC Start browser, run tests Windows, Linux, Mac

Others Test execution only via Selenium RC Partial support possible** As applicable

Page 6: Selenium tutorial

Opening The IDE

• Go To Tools Selenium IDE Click. Below screen will be opened

Page 7: Selenium tutorial

Features in Selenium IDE• Menu Bar The File menu allows you to create, open, and save test case and test suite files. The Edit menu allows copy, paste, delete, undo, and select all operations for editing the commands in your test case. The Options menu allows the changing of settings. You can set the timeout value for certain commands, add user-defined user extensions to the base set of Selenium commands, and specify the format (language) used when saving your test cases. The Help menu is the standard Firefox Help menu; only one item on this menu–UI-Element Documentation–pertains to Selenium-IDE.• ToolbarThe toolbar contains buttons for controlling the execution of your test cases, including a step feature for debugging your test cases. The right-most button, the one with the red-dot, is the record button.

Page 8: Selenium tutorial

Features Conti…

Speed Control: controls how fast your test case runs.

Run All: Runs the entire test suite when a test suite with multiple test cases is loaded.

Run: Runs the currently selected test. When only a single test is loaded this button and the Run All button have the same effect

Pause/Resume: Allows stopping and re-starting of a running test case.

Step: Allows you to “step” through a test case by running it one command at a time. Use for debugging test cases.

Test Runner Mode: Allows you to run the test case in a browser loaded with the Selenium-Core Test Runner. The Test Runner is not commonly used now and is likely to be deprecated. This button is for evaluating test cases for backwards compatibility with the Test Runner. Most users will probably not need this button.

Apply Rollup Rules: This advanced feature allows repetitive sequences of Selenium commands to be grouped into a single action. Detailed documentation on rollup rules can be found in the UI-Element Documentation on the Help menu.

Record: Records the user’s browser actions.

Page 9: Selenium tutorial

Features Conti…

• Test Case PaneYour script is displayed in the test case pane. It has two tabs, one for displaying the command and their parameters in a readable “table” format.

The other tab - Source displays the test case in the native format in which the file will be stored. By default, this is HTML although it can be changed to a programming language such as Java or C#, or a scripting language like Python. See the Options menu for details. The Source view also allows one to edit the test case in its raw form, including copy, cut and paste operations.

The Command, Target, and Value entry fields display the currently selected command along with its parameters. These are entry fields where you can modify the currently selected command. The first parameter specified for a command in the Reference tab of the bottom pane always goes in the Target field. If a second parameter is specified by the Reference tab, it always goes in the Value field.

If you start typing in the Command field, a drop-down list will be populated based on the first characters you type; you can then select your desired command from the drop-down.

Page 10: Selenium tutorial

Features Conti…

• Log/Reference/UI-Element/Rollup Pane

The bottom pane is used for four different functions–Log, Reference, UI-Element, and Rollup–depending on which tab is selected.LogWhen you run your test case, error messages and information messages showing the progress are displayed in this pane automatically, even if you do not first select the Log tab. These messages are often useful for test case debugging. Notice the Clear button for clearing the Log. Also notice the Info button is a drop-down allowing selection of different levels of information to log.

ReferenceThe Reference tab is the default selection whenever you are entering or modifying Selenese commands and parameters in Table mode. In Table mode, the Reference pane will display documentation on the current command. When entering or modifying commands, whether from Table or Source mode, it is critically important to ensure that the parameters specified in the Target and Value fields match those specified in the parameter list in the Reference pane. The number of parameters provided must match the number specified, the order of parameters provided must match the order specified, and the type of parameters provided must match the type specified. If there is a mismatch in any of these three areas, the command will not run correctly. http://release.seleniumhq.org/selenium-core/1.0/reference.html

UI-Element and RollupDetailed information on these two panes (which cover advanced features) can be found in the UI-Element Documentation on the Help menu of Selenium-IDE.

Page 11: Selenium tutorial

• There are three primary methods for developing test cases. Frequently, a test developer will require all three techniques.

Recording• Many first-time users begin by recording a test case from their interactions with a website. When

Selenium-IDE is first opened, the record button is ON by default. If you do not want Selenium-IDE to begin recording automatically you can turn this off by going under Options > Options... and deselecting “Start recording immediately on open.”

• During recording, Selenium-IDE will automatically insert commands into your test case based on your actions. Typically, this will include:

clicking a link - click or clickAndWait commandsentering values - type commandselecting options from a drop-down listbox - select commandclicking checkboxes or radio buttons - click commandHere are some “gotchas” to be aware of:The type command may require clicking on some other area of the web page for it to record.Following a link usually records a click command. You will often need to change this to clickAndWait to ensure your test case pauses until the new page is completely loaded. Otherwise, your test case will continue running commands before the page has loaded all its UI elements. This will cause unexpected test case failures.

• Adding Verifications and Asserts With the Context MenuYour test cases will also need to check the properties of a web-page. This requires assert and verify commands. We won’t describe the specifics of these commands here; that is in the chapter on “Selenese” Selenium Commands. Here we’ll simply describe how to add them to your test case.

Building Test Cases

Page 12: Selenium tutorial

• With Selenium-IDE recording, go to the browser displaying your test application and right click anywhere on the page. You will see a context menu showing verify and/or assert commands.

• The first time you use Selenium, there may only be one Selenium command listed. As you use the IDE however, you will find additional commands will quickly be added to this menu. Selenium-IDE will attempt to predict what command, along with the parameters, you will need for a selected UI element on the current web-page.

• Let’s see how this works. Open a web-page of your choosing and select a block of text on the page. A paragraph or a heading will work fine. Now, right-click the selected text. The context menu should give you a verifyTextPresent command and the suggested parameter should be the text itself.

• Also, notice the Show All Available Commands menu option. This shows many, many more commands, again, along with suggested parameters, for testing your currently selected UI element.

• Try a few more UI elements. Try right-clicking an image, or a user control like a button or a checkbox. You may need to use Show All Available Commands to see options other than verifyTextPresent. Once you select these other options, the more commonly used ones will show up on the primary context menu. For example, selecting verifyElementPresent for an image should later cause that command to be available on the primary context menu the next time you select an image and right-click.

• Again, these commands will be explained in detail in the chapter on Selenium commands. For now though, feel free to use the IDE to record and select commands into a test case and then run it. You can learn a lot about the Selenium commands simply by experimenting with the IDE.

EditingInsert Command• Table View

Select the point in your test case where you want to insert the command. To do this, in the Test Case Pane, left-click on the line where you want to insert a new command. Right-click and select Insert Command; the IDE will add a blank line just ahead of the line you selected. Now use the command editing text fields to enter your new command and its parameters.

• Source ViewSelect the point in your test case where you want to insert the command. To do this, in the Test Case Pane, left-click between the commands where you want to insert a new command, and enter the HTML tags needed to create a 3-column row containing the Command, first parameter (if one is required by the Command), and second parameter (again, if one is required). Be sure to save your test before switching back to Table view.

Page 13: Selenium tutorial

• Insert Comment Comments may be added to make your test case more readable. These comments are ignored when the

test case is run. Comments may also be used to add vertical white space (one or more blank lines) in your tests; just create

empty comments. An empty command will cause an error during execution; an empty comment won’t.• Table View

Select the line in your test case where you want to insert the comment. Right-click and select Insert Comment. Now use the Command field to enter the comment. Your comment will appear in purple font.

• Source ViewSelect the point in your test case where you want to insert the comment. Add an HTML-style comment, i.e., <!-- your comment here -->.

• Edit a Command or Comment Table ViewSimply select the line to be changed and edit it using the Command, Target, and Value fields.

• Source ViewSince Source view provides the equivalent of a WYSIWYG editor, simply modify which line you wish–command, parameter, or comment.

• Opening and Saving a Test Case

Like most programs, there are Save and Open commands under the File menu. However, Selenium distinguishes between test cases and test suites. To save your Selenium-IDE tests for later use you can either save the individual test cases, or save the test suite. If the test cases of your test suite have not been saved, you’ll be prompted to save them before saving the test suite.

When you open an existing test case or suite, Selenium-IDE displays its Selenium commands in the Test Case Pane.

Page 14: Selenium tutorial

• Running Test Cases• The IDE allows many options for running your test case. You can run a test case all at once, stop and start

it, run it one line at a time, run a single command you are currently developing, and you can do a batch run of an entire test suite. Execution of test cases is very flexible in the IDE.

• Run a Test Case Click the Run button to run the currently displayed test case.

• Run a Test Suite Click the Run All button to run all the test cases in the currently loaded test suite.

• Stop and Start The Pause button can be used to stop the test case while it is running. The icon of this button then changes to indicate the Resume button. To continue click Resume.

• Stop in the Middle You can set a breakpoint in the test case to cause it to stop on a particular command. This is useful for debugging your test case. To set a breakpoint, select a command, right-click, and from the context menu select Toggle Breakpoint.

• Start from the Middle You can tell the IDE to begin running from a specific command in the middle of the test case. This also is used for debugging. To set a start point, select a command, right-click, and from the context menu select Set/Clear Start Point.

• Run Any Single Command Double-click any single command to run it by itself. This is useful when writing a single command. It lets you immediately test a command you are constructing, when you are not sure if it is correct. You can double-click it to see if it runs correctly. This is also available from the context menu.

For more information Please go to this link: http://seleniumhq.org/docs/02_selenium_ide.html http://seleniumhq.org/docs/ -Documents for selenium.

Page 15: Selenium tutorial

Examples-For Practice

• Suppose you have to test the login module. so what are the possible test cases for this. some of them are:1- Enter the correct user name and wrong password- result should be an error message " Invalid password"2- Enter the wrong user name and correct password- result should be an error message " Invalid user name"3- Enter the user name and leave the password field blank- result should be an error message "Password field can not be blank“ and so on and the last one is enter both the fields with correct data. Login should success.

For Testing with selenium we need to follow these steps:• Open Firefox browser and then open Selenium from tool.• Now stop recording (make sure when selenium open first time it opens in record mode) by clicking the

record button.• Now enter the url of your application.• Start recording by clicking start button again.• Now perform the steps (for login put the correct user name and put the correct password and then click on

submit button now once you log in with application you need to verify your login). For this you need to use "VerifyText Present" function/method. For this you can select the login user name ( That is you expected result ) from top of the header and right click and select verify text present option.

• Stop the recording. Now you have done with your first test . Here is the example I am giving , it is for login with rediffmail.com.

Page 16: Selenium tutorial

• Now you need to save this test. You can save this anywhere using .html ext. Later you will need to make a test suit (to run multiple test at a time ). for this make a test suite and map all the test with in this suit. you need to create a folder and place all your test with in this folder. Your test suit will also be in this folder only.

• Now run this test.• You will see the status of your test as passed in green color. For this you can play with test runner screen.

here is the test runner screen

Page 17: Selenium tutorial

Another Simple Test (selenium)

• Running a simple test : a. Start Selenium IDE in Firefox: Tools->Selenium IDE. You will see the following popup.

b. Click on the red record button on the right.c. Browse to Google.com and enter ’selenium’ in the search box and click enter.d. Click on the first result, which is that of selenium.openqa.org.e. Stop the recording by clicking on the record button.You should see something like below. If you click on the ‘ Source’ tab you can see the test html generated by selenium.

Page 18: Selenium tutorial

• The ‘table’ tab shows the commands recorded by Selenium.f. Open a new tab in Firefox and click on the Selenium IDE’s play button to run the recorded test.

The IDE should play your recorded test. After the test is complete you should have landed on the selenium page (http://selenium.openqa.org/). The IDE after the test run is shown below. In the ‘Log section’ you can see the various events run by the test. In the table tab you can see that all the rows are green, which means that the test ran successfully.

Now lets add a small assertion to the above test.a. Click on the blank line below after the last ‘clickAndWait’ command and insert ‘assertTextPresent’ command from the drop down box as shown below. You should see something like this.

Page 19: Selenium tutorial

This test checks to see if the text ‘Selenium News’ is present in the last rendered page. Run the test again and you should see that the test has run successfully. No try replacing the text with something that is not present on the page, take for example ‘elvis’ and run the test again. Now the test fails and you should get the screen below.

Page 20: Selenium tutorial

• There is much more to do in selenium please find others details by going to the link and learn.

http://seleniumhq.org/docs/

Thanking You

Page 21: Selenium tutorial

Questions on Selenium

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 specificallydesigned 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 GridQ3. What is Selenium IDE?Ans. Selenium IDE is for building Selenium test cases. It operates as a Mozilla Firefox add on andprovides an easy to use interface for developing and running individual test cases or entire testsuites. Selenium-IDE has a recording feature, which will keep account of user actions as they areperformed 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 inother browsers by using Selenium RC (Selenium Remote Control) and specifying the name of the testsuite 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 needed2. It is a record & playback tool and the script format can be written in various languages includingC#, Java, PERL, Python, PHP, HTML3. Auto complete for all common Selenium commands4. Debug and set breakpoints5. Option to automatically assert the title of every page6. Support for Selenium user-extensions.js file

Page 22: Selenium tutorial

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 maximumflexibility and extensibility in developing test logic. For example, if the application under test returnsa result set and the automated test program needs to run tests on each element in the result set, theiteration / 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 useSelenium RC with a high level programming language to develop test cases also allows the automatedtesting 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 suitesthat must be run in multiple environments. Selenium Grid can be used to run multiple instances ofSelenium 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 entiretest 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 Seleniumframework to customize test automation. As compared to other test automation tools, it isSelenium’s strongest characteristic. Selenium Remote Control support for multiple programming andscripting languages allows the test automation engineer to build any logic they need into theirautomated 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 andenhancements can be submitted for contribution.

Page 23: Selenium tutorial

Q12. What test can Selenium do?Ans. Selenium is basically used for the functional testing of web based applications. It can be used fortesting in the continuous integration environment. It is also useful for agile testingQ13. 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 inSelenium Remote Control.Q15. What programming languages can you use in Selenium RC?Ans. C#, Java, Perl, PHP, Python, RubyQ16. 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 usedfor 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 fortesting 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 toInternet Explorer on Windows.QTP uses scripting language implemented on top of VB Script. However, Selenium test suite has theflexibility 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 applicationscan be testing using Selenium testing suite. However, Silk Test can be used for testing client serverapplications. Selenium supports following web browsers: Internet Explorer, Firefox, Safari, Opera orKonqueror on Windows, Mac OS X and Linux. However, Silk Test is limited to Internet Explorer andFirefox.