GL_Web application testing using selenium


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

GL_Web application testing using selenium

  1. 1. Connect. Collaborate. Innovate. Web Application Testing using Selenium Amit Saxena 17/08/2011© Copyright GlobalLogic 2010 1
  2. 2. Web Application Testing using Selenium Connect. Collaborate. Innovate. Training Agenda:  Selenium Introduction  Selenium Components  Recording & Testing with Selenium IDE  Exercise 1 – Selenium IDE  Testing using Selenium RC  Exercise 2 – Selenium RC  Parallel testing using Selenium Grid© Copyright GlobalLogic 2010 2
  3. 3. Selenium – What is it? Connect. Collaborate. Innovate. • Selenium is a set of tools to automate web application testing across many platforms. • Selenium runs in many browsers and operating systems. • Selenium can be controlled by many programming languages and testing frameworks. • Selenium is highly flexible. • Selenium is an Open Source project. The code can be modified and enhancements can be submitted for contribution.© Copyright GlobalLogic 2010 3
  4. 4. Selenium Components Connect. Collaborate. Innovate.  Selenium IDE - is a Firefox add-on that records clicks, typing, and other actions to make a test, which you can play back in the browser.  Selenium RC - runs your tests in multiple browsers and platforms. Tweak/Customize your tests in your preferred programming language.  Selenium Grid - extends Selenium RC to distribute your tests across multiple servers, saving you time by running tests in parallel.© Copyright GlobalLogic 2010 4
  5. 5. Selenium Components… Connect. Collaborate. Innovate.© Copyright GlobalLogic 2010 5
  6. 6. Platforms Supported by Selenium Connect. Collaborate. Innovate.© Copyright GlobalLogic 2010 6
  7. 7. Connect. Collaborate. Innovate.Selenium Commands – Selenese Selenium commands come in three “flavors”: Actions, Accessors and Assertions. Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped. Accessors examine the state of the application and store the results in variables, e.g. “storeTitle”. They are also used to automatically generate Assertions. Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”. All Selenium Assertions can be used in 3 modes: Assert – Upon failure test is aborted Verify – Upon failure error is logged and test continues WaitFor – Waits for a conditions for a given timeout© Copyright GlobalLogic 2010 7
  8. 8. Connect. Collaborate. Innovate.Selenium Scripts Syntax Selenium commands are simple, they consist of the command and two parameters. For example: type id=phone (555) 666-7066 Parameters vary, however they are typically: a locator for identifying a UI element within a page. a text pattern for verifying or asserting expected page content a text pattern or a selenium variable for entering text in an input field or for selecting an option from an option list.© Copyright GlobalLogic 2010 8
  9. 9. Commonly Used Selenium Commands Connect. Collaborate. Innovate. open opens a page using a URL. click/clickAndWait performs a click operation, and optionally waits for a new page to load. verifyTitle/assertTitle verifies an expected page title. verifyTextPresent verifies expected text is somewhere on the page. verifyElementPresent verifies an expected UI element, as defined by its HTML tag, is present on the page. verifyText verifies expected text and it’s corresponding HTML tag are present on the page. verifyTable verifies a table’s expected contents. waitForPageToLoad pauses execution until an expected new page loads. Called automatically when clickAndWait is used. waitForElementPresent pauses execution until an expected UI element, as defined by its HTML tag, is present on the page.© Copyright GlobalLogic 2010 9
  10. 10. Locating Elements Connect. Collaborate. Innovate. For many Selenium commands, a target is required. This target identifies an element in the content of the web application, and consists of the location strategy. Some of the location strategies using in Selenium are: Locating by Identifier This is probably the most common method of locating elements and is the catch-all default when no recognized locator type is used. With this strategy, the first element with the id attribute value matching the location will be used. If no element has a matching id attribute, then the first element with a name attribute matching the location will be used. Locating by Id This type of locator is more limited than the identifier locator type, but also more explicit. Use this when you know an element’s id attribute. Locating by Name The name locator type will locate the first element with a matching name attribute. Locating by XPath XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute.© Copyright GlobalLogic 2010 10
  11. 11. Selenium IDE Connect. Collaborate. Innovate. Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Features: •Easy record and playback •Intelligent field selection will use IDs, names, or XPath as needed •Autocomplete for all common Selenium commands •Walk through tests •Debug and set breakpoints •Save tests as Html, Java, .NET, Php, Perl, Ruby scripts etc. •Support for Selenium user-extensions.js file© Copyright GlobalLogic 2010 11
  12. 12. Selenium IDE Installation Connect. Collaborate. Innovate.Selenium IDE is installed as a Firefox extension. It can be installed directly withinFirefox by going to and downloading (XPIs arethe Firefox Extensions)© Copyright GlobalLogic 2010 12
  13. 13. Opening Selenium IDE Connect. Collaborate. Innovate.Selenium IDE is opened from the Tools menu of Firefox.© Copyright GlobalLogic 2010 13
  14. 14. Connect. Collaborate. Innovate.Selenium IDE Features Menu Bar Toolbar Test Case Pane Test Case Pane Log/Reference Pane© Copyright GlobalLogic 2010 14
  15. 15. Connect. Collaborate. Innovate.Selenium IDE Basic Toolbar Commands 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. Record: Records the user’s browser actions. Step: Allows you to “step” through a test case by running it one command at a time. Use for debugging test cases.© Copyright GlobalLogic 2010 15
  16. 16. Connect. Collaborate. Innovate.Building Test Cases using IDE Recording When Selenium-IDE is first opened, the record button is ON by default. 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 commands • entering values - type command • selecting options from a drop-down listbox - select command • clicking checkboxes or radio buttons - click command Adding Verifications and Asserts With the Context Menu Your test cases will also need to check the properties of a web-page. This requires assert and verify commands. 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. Insert Commands in Table View or Source View Commands can also be inserted in the Table view and Source View manually.© Copyright GlobalLogic 2010 16
  17. 17. Exercise 1 – Selenium IDE Connect. Collaborate. Innovate. 1. Install Selenium IDE 2. Record & Save the following test cases in Selenium IDE:  Open Search GlobalLogic in Google. Click GlobalLogic link in search result. GlobalLogic homepage should open up.  Open Click About Us. Verify that the About Us page opens up.  Open Click Careers Page. Go to current openings section. Conduct Search. Verify that the search results show up. 3. Execute the above test cases individually in Selenium IDE. 4. Execute the above created test cases as a test suite in Selenium IDE.© Copyright GlobalLogic 2010 17
  18. 18. Selenium RC Connect. Collaborate. Innovate. 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, and 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© Copyright GlobalLogic 2010 18
  19. 19. Selenium RC Components Connect. Collaborate. Innovate. Selenium-RC components are: The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT (Application Under Test). Client libraries which provide the interface between each programming language and the Selenium-RC Server.© Copyright GlobalLogic 2010 19
  20. 20. How Selenium RC Works Connect. Collaborate. Innovate.© Copyright GlobalLogic 2010 20
  21. 21. Installing Selenium RC Server Connect. Collaborate. Innovate.• The Selenium Server is written in Java, and requires the Java Runtime Environment (JRE) version 1.5.0 or higher in order to start.• Go to the and download selenium RC (Latest version is 1.0.3) -• Unzip and extract it any directory.© Copyright GlobalLogic 2010 21
  22. 22. Start the RC Server Connect. Collaborate. Innovate.• Open command prompt and go to the directory where you have extracted the Selenium RC.• Run the command – java -jar selenium-server.jar© Copyright GlobalLogic 2010 22
  23. 23. Using Java Client Driver Connect. Collaborate. Innovate.• Extract the file selenium-java-client-driver.jar from• Open your desired Java IDE (Eclipse, NetBeans, IntelliJ etc.)• Create a new project.• Add the selenium-java-client-driver.jar files to your project as references.• Add to your project classpath the file selenium-java-client-driver.jar.• From Selenium-IDE, export a script to a Java file and include it in your Java project, or write your Selenium test in Java using the selenium-java-client API.• Run Selenium server from the console.• Execute your test from the Java IDE or from the command-line.© Copyright GlobalLogic 2010 23
  24. 24. Exercise 2 – Selenium RC Connect. Collaborate. Innovate. 1. Install Selenium RC. 2. Create a Java Project in Eclipse and add JUnit and Selenium Client Driver in project classpath. 3. Export Test Cases From Selenium IDE as JUnits. 4. Export Test Case Suite From Selenium IDE as JUnit Test Suite. 5. Import the Test Cases in Eclipse. 6. Start the Selenium Server. 7. Execute the test cases individually from Eclipse as JUnit. 8. Execute the test cases as a JUnit Suite from Eclipse.© Copyright GlobalLogic 2010 24
  25. 25. Selenium Grid Connect. Collaborate. Innovate. Selenium-Grid allows the Selenium-RC solution to scale for large test suites or test suites that must be run in multiple environments. With Selenium-Grid, multiple instances of Selenium-RC are running on various operating system and browser configurations; Each of these when launching register with a hub. When tests are sent to the hub they are then redirected to an available Selenium- RC, which will launch the browser and run the test. This allows for running tests in parallel, with the entire test suite theoretically taking only as long to run as the longest individual test.© Copyright GlobalLogic 2010 25
  26. 26. Typical Selenium Grid Setup Connect. Collaborate. Innovate.© Copyright GlobalLogic 2010 26
  27. 27. Installing Selenium Grid Connect. Collaborate. Innovate.• Selenium Grid requires the following to be installed: • JDK 1.5 or above • Apache Ant 1.7 or above• Go to the and download Selenium Grid (Latest version is 1.0.8) –• Unzip and extract it any directory.© Copyright GlobalLogic 2010 27
  28. 28. Starting Selenium Grid Connect. Collaborate. Innovate. Selenium Grid requires the Hub and RCs to be started : Hub To start Hub, open command prompt and go to the directory where you have extracted Selenium Grid and type: ant launch-hub RCs To start RCs, open command prompt and go to the directory where you have extracted Selenium Grid and type: ant -Denvironment=“*firefox“ -Dport=5555 launch-remote-control ant -Denvironment=“*iexplore” -Dport=5556 launch-remote-control The above commands would start RCs on ports 5555 and 5556 for firefox and internet explorer environments. When having multiple machines for RCs, following parameters should be used to provide the Hub URL and RC host: ant -Dport=<port> -Dhost=<hostname> -DhubURL=<hub url> launch-remote-control Grid Console Open http://localhost:4444/console to view the registered RCs and their status.© Copyright GlobalLogic 2010 28
  29. 29. Exercise 3 – Selenium Grid Connect. Collaborate. Innovate. 1. Install Selenium Grid. 2. Start Selenium Grid & RCs. 3. Add Parallel JUnit to the classpath in the Eclipse project. 4. Export Test Case Suite From Selenium IDE as JUnit Suite. 5. Import the Test Case Suite in Eclipse. 6. Execute the Test Case suite using Parallel JUnit from Eclipse. 7. View the available/active RCs in Grid Console.© Copyright GlobalLogic 2010 29
  30. 30. References Connect. Collaborate. Innovate. Selenium Documentation© Copyright GlobalLogic 2010 30
  31. 31. Questions Connect. Collaborate. Innovate. ??© Copyright GlobalLogic 2010 31
  32. 32. Connect. Collaborate. Innovate.Web Application Testing using Selenium Thank You!© Copyright GlobalLogic 2010 32