run automated selenium junit tests pdf

18
© Copyright IBM Corporation 2012 Trademarks Run automated Selenium JUnit tests Page 1 of 18 Run automated Selenium JUnit tests Create, export, and configure Selenium tests to run them from Rational Quality Manager Matthew J. Bradshaw ([email protected]) Technical Consultant IBM Skill Level: Introductory Date: 30 Oct 2012 As quality assurance teams are put under more pressure to improve the time that it takes to carry out their testing, the use of automated test tools to create regression test suites is becoming commonplace. Selenium is a popular open source tool for web application testing. Linking regression tests to a lifecycle management application ensures that full end-to-end traceability of requirements to development through to testing can be maintained. This guide shows you how to connect IBM Rational Quality Manager to Selenium. The benefits of this link include traceability, improved project knowledge, and the ability to run tests automatically after a build. Version 4.0 of IBM® Rational® Quality Manager introduced the ability to create an automated JUnit Selenium test script. Much like running a Rational Functional Tester automated test remotely from Rational Quality Manager, you can now run Selenium tests. Test results are returned to Rational Quality Manager. Selenium is an open source software testing framework designed for web applications. There are different components available: The Selenium IDE, which you can use for recording and creating automated tests without knowledge of a specific scripting language. The Selenium WebDriver API, which you can use to create tests by using a scripting language of your choice. Supported languages are Java, Ruby, Python, and C#. Because JUnit is a unit-testing framework for the Java programming language, and the tests are run from Rational Quality Manager using JUnit, Java is the language that any Selenium WebDriver tests must be written in to be supported. Selenium also offers a wider range of browser support and can be deployed on Microsoft Windows, Linux, and Apple Macintosh platforms.

Upload: vu-quang-dinh

Post on 08-Nov-2014

109 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Run Automated Selenium Junit Tests PDF

© Copyright IBM Corporation 2012 TrademarksRun automated Selenium JUnit tests Page 1 of 18

Run automated Selenium JUnit testsCreate, export, and configure Selenium tests to run themfrom Rational Quality Manager

Matthew J. Bradshaw ([email protected])Technical ConsultantIBM

Skill Level: Introductory

Date: 30 Oct 2012

As quality assurance teams are put under more pressure to improve the timethat it takes to carry out their testing, the use of automated test tools to createregression test suites is becoming commonplace. Selenium is a popular opensource tool for web application testing. Linking regression tests to a lifecyclemanagement application ensures that full end-to-end traceability of requirementsto development through to testing can be maintained. This guide shows youhow to connect IBM Rational Quality Manager to Selenium. The benefits of thislink include traceability, improved project knowledge, and the ability to run testsautomatically after a build.

Version 4.0 of IBM® Rational® Quality Manager introduced the ability to create anautomated JUnit Selenium test script. Much like running a Rational Functional Testerautomated test remotely from Rational Quality Manager, you can now run Seleniumtests. Test results are returned to Rational Quality Manager.

Selenium is an open source software testing framework designed for webapplications. There are different components available:

• The Selenium IDE, which you can use for recording and creating automatedtests without knowledge of a specific scripting language.

• The Selenium WebDriver API, which you can use to create tests by usinga scripting language of your choice. Supported languages are Java, Ruby,Python, and C#. Because JUnit is a unit-testing framework for the Javaprogramming language, and the tests are run from Rational Quality Managerusing JUnit, Java is the language that any Selenium WebDriver tests must bewritten in to be supported.

Selenium also offers a wider range of browser support and can be deployed onMicrosoft Windows, Linux, and Apple Macintosh platforms.

Page 2: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 2 of 18

In this tutorial, the server running Rational Quality Manager also runs the Seleniumtests. In a real test environment, you would run the Selenium tests from a differenttest workstation to the Rational Quality Manager server.

Configure Selenium testsIf you already have the Selenium IDE, Selenium WebDriver, and JUnit installed andworking with an example of a JUnit WebDriver test, you can skip to the section calledGenerate a library of Selenium tests for execution.

If you are interested in Selenium but have never used it before, the next six sectionswill show you how to install it and create a simple test that can be run from RationalQuality Manager later in the tutorial. This is not a tutorial on how to create complexSelenium tests. It explains only how to create a test that will suffice in demonstratingthe connection with Rational Quality Manager. Given that Selenium is an opensource tool, there are plenty of resources available for learning how to use it to its fullpotential.

Download and install Selenium

1. Download both the Selenium IDE and Selenium WebDriver from theSeleniumHQ website (see Resources). The Selenium IDE is a Mozilla Firefoxadd-on that will run from within the browser.

Important:Make sure that you download the Selenium client driver for Java, not for any of theother available languages.

Figure 1. Select the Java driver

2. After the .zip file has downloaded, extract it to a different location.3. Download JUnit from junit.org (see Resources). The Version 4 basic Java

archive (JAR) file will suffice for this tutorial.4. Save this file in the same location where you extracted the Selenium-2 file.

The steps to install Selenium, JUnit, and the adapter must be repeated on any testcomputer that will be executing the tests.

Page 3: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 3 of 18

Configure Eclipse

The steps in Eclipse need to be performed by anyone who is responsible forauthoring or modifying any Selenium tests.

To view the test source code, make any changes, or run the test for debuggingpurposes, the test will need to be added to a Java IDE. In this tutorial, you will beusing Eclipse. If you have IBM® Rational Team Concert™ installed as part of theRational solution for Collaborative Lifecycle Management (CLM) solution, these stepscan be carried out in the that IDE, because it is based on Eclipse. Using this ratherthan a standard Eclipse installation will give you the added benefit of storing the testsunder source control. You can also use it to assign tasks to developers or testers forcreating or enhancing tests. Other Java IDEs will work, but the configuration stepswill be slightly different for them.

1. Switch to the Java perspective by clicking Window > Open Perspective >Java.

2. Create a new Java project. To start, select =File > New > Java Project.3. Give the project a meaningful name (SeleniumProject for this example),

manually specify a location where the project files will be stored, and then clickFinish.

Figure 2. Specify the name and location for this sample project

4. In the Package Explorer on the left side, right-click the project, and create a newfolder called libs.

5. Copy the junit-4.jar file and Selenium-2 folder to the libs folder that you justcreated.

Figure 3. This is what your Package Explorer view should look like

6. Next, the JARs that need to be added to the project class path, so they can beused within the tests. Right-click the project (in this case, SeleniumProject),and select Build Path > Configure Project Build Path.

Page 4: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 4 of 18

7. Select the Libraries tab, and then Add External JARs. Choose the junit-4.jarfile.

8. In the Selenium-2 folder, add the two Selenium JAR files, as well as all of theJAR files located in the libs folder. Click OK to confirm the addition.

9. Now create a package hierarchy to contain the tests. (The benefit of doing thiswill be covered in more detail in the section titled Create the Selenium test inRational Quality Manager.) Right click-the SeleniumProject in the PackageExplorer, and create a new package called com.example.tests.

Create a sample Selenium test

You will now create a simple automated test to import into the project that you havejust created. You will record the test, using the Selenium IDE, during which the testwill perform a search on Google.

1. To open the Selenium IDE window, inside Firefox, click the Firefox icon, andthen Web Developer > Selenium IDE. Alternatively, you can open it using thekeyboard shortcut Ctrl+Alt+S. A separate Selenium IDE window will open.

2. In this window, set the Base URL to www.google.com and ensure that the redRecord button is selected.

3. Navigate to the Base URL in the original Firefox window. After the Google pagehas loaded, enter a search term such as Rational Jazz and then click theSearch button.

4. Click the first result.5. Return to the Selenium IDE window, and stop the recording by clicking the red

Record button again.

Your Selenium IDE window should now contain commands that look similar to thosein Figure 4.

Page 5: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 5 of 18

Figure 4. What your test recording should resemble

6. You can verify that the recording has worked by clicking the Play button justbelow the Base URL field. To see the test playback in detail, you can slow theplayback by using the slider on the left.

Export the Selenium test in the correct format

The Selenium IDE is designed for rapid creation of regression test suites. They canbe created and played back within the tool as just demonstrated. To play the testback from Rational Quality Manager, the script needs to have been written in Java.Handily, the Selenium IDE has an Export function to export the recorded test to thecorrect Java format.

1. In the Selenium IDE, select File > Export Test Case As > Java/JUnit4/WebDriver, which is the supported type of Selenium test.

2. Give the test a meaningful name, such as ExampleSelTest, and save it asa java file in the SeleniumProject/src/com/example/test folder within theSeleniumProject that you created earlier.

Page 6: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 6 of 18

Figure 5. How to export a Selenium test

View the created test in Eclipse

In Eclipse, by refreshing the Package Explorer, the Selenium Java test file that wasjust added into the project will be visible.

1. To refresh, click File > Refresh on the Eclipse menu or press F5.2. Examine the com/example/tests package to see that the new test is now

visible, and double-click it to open it.

Page 7: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 7 of 18

Figure 6. What the new project should look like

To check that the test can be run correctly, you can run it from within Eclipse byselecting the ExampleSelTest Java file and clicking the Run As on the toolbar andthen selecting JUnit Tests from the drop-down menu. You can also do that by right-clicking the actual test and then clicking Run As > JUnit Tests.

You should then see the test playback, and the same steps that were recorded inFirefox will be repeated.

Generate a library of Selenium tests to run

The Selenium test can now be run in Eclipse as a stand-alone JUnit test. To run itfrom Rational Quality Manager, it needs to be exported as a JAR file. Exporting willcreate a transportable and executable version of any Selenium test. You can create alibrary of tests in a JAR file and move that file to multiple test computers so they canbe run there.

1. To export the sample test, right-click SeleniumProject, and click Export.2. Expand the Java node, select the JAR File option, and click Next.3. The default options will be fine on the next form, but you will need to specify an

export destination (see Figure 7).

Page 8: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 8 of 18

Figure 7. Sample export form

4. Click Finish to export the tests.

Configure and run the adapterNow that you have created a library of multiple Selenium tests and it is ready to use,you must set up the Rational Quality Manager side. The first task is to configure theadapter that you will use to run the tests.

Configure the adapterThe adapter is included in any Rational Quality Manager installation, and it can becopied to any computer to allow Selenium tests to be run on different test computers.This is the location of the adapter:

JAZZ_INSTALL_DIRECTORY/server/conf/qm/adapters/RQMJUnitSeleniumAdapter.zip

JAZZ_INSTALL_DIRECTORY is the directory on the Rational Quality Manager serverwhere Rational Quality Manager is installed.

Page 9: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 9 of 18

1. Either copy this file onto a chosen test computer or leave it on the same serverif you are executing tests locally. (This example will use the same server).

2. Unzip the file.

In the extracted adapter folder, a readme.txt file contains detailed information onhow the adapter can be configured. This article is a guide for a simple startup. Thisexample is for starting the adapter on Microsoft Windows, but the steps can bemodified for a Linux system. The modifications are shown in the readme file.

When you start the adapter for the first time, it needs to be started from the commandline with arguments. For a test to be run, a list of the absolute paths to all of thedifferent JAR files that could be needed when running is required. There are a lotof JAR files in the Selenium-2/libs folder, so it saves time to use a command to addthese entire file paths to a text file rather than do it manually.

3. On the test computer where you are executing the Selenium tests, open acommand window (Start > Run > cmd), navigate to the Selenium-2.x.x/libsfolder using the cd command, and then enter: dir /S /B > jars.txt

This will create a text file called jars.txt in the libs directory that contains the filenames and paths of the JAR files on separate lines.

4. Add a semicolon (;) to the end of each line.

A completed example line should look like this:

C:\Users\Rational\Documents\Selenium\SeleniumProject\lib\Selenium-2.25.0\libs\apache-mime4j-0.6.jar;

5. Then make sure to add these two JARs in the Selenium-2 directory as well asthe junit jar and the Selenium tests jar:

• Selenium-java-2.x.x.jar• Selenium-java-2.x.x-srcs.jar• junit-4.x.jar• SelTest.jar (which was exported from Eclipse)

6. Compact the list so that all of the file paths reside on just one line.7. Open the selenium.properties file that is stored in the

RQMJUnitSeleniumAdapter folder.8. Paste in the list of file paths from the jars.txt file next to the

com.ibm.rqm.selenium.seleniumClasspath= property, and remove # from theline if there.

This tells the adapter where to find the code that will be used when executing thetest.

Note:The selenium.properties file needs to be modified for every test computer that isrunning an adapter.

Page 10: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 10 of 18

Run the adapter

1. Open a new command line, and navigate to the directory where the adapterwas extracted by using the cd command again.

2. Enter this command (replace anything in italics with your own information):-

start.bat -repository https://rqmserver:port/qm -userid userid –password password –adapter adapter -projectArea projectArea -adapterName adaptername

where:

rqmserver is the fully qualified host name of the Rational Quality Manager server.

port is the port where the Rational Quality Manager server is running.

userid is a registered user ID within Rational Quality Manager that has the license torun an adapter.

password is the password of the user ID used to log in.

adapter is a user-given adapter identifier. This value must be unique, because it isused by the Rational Quality Manager server to identify this adapter.

project area is the name or alias of the project that you are logging in to. If there arespaces in the project name, put the words within quotation marks.

adapter name is a user-given adapter name.

This example command uses the example JKEBanking project area:

start.bat -repository https://clm.jkebanking.com:9443/qm -user adapter -passwordadapter -adapter RQMSelAdapter -projectArea "JKE Banking (Quality Management)"

This adapter is using a user account created specifically for connecting adapters.This user, adapter, has the same password as the username, and it was assigned theConnector Client Access License within Rational Quality Manager. This is to avoidwasting a Quality Professional license for a constantly connected adapter workstationwhen all it will be doing is executing remote tests.

Tip:If the adapter fails to start, make sure that Java has been added to the path variable.For additional information on configuring the adapter or to troubleshoot, see thereadme file.

Create the Selenium test in Rational Quality Manager

The final steps are to create the test assets within Rational Quality Manager that willpoint to a specific Selenium test in the JAR file, and run it through the adapter that

Page 11: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 11 of 18

you just configured and started. The test assets must be created by a user with aQuality Professional License.

Create a test script

To run a Selenium test from Rational Quality Manager, you need to create acorresponding test script in Rational Quality Manager.

1. To create a test script, select the Construction tab and, from the drop-downmenu, and select Create > Test Script.

Figure 8. Create a test script

2. Give the test script a name, and use the drop-down menu to change the type toJUnit Selenium.

Page 12: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 12 of 18

Figure 9. Create a JUnit Selenium type test script

Three fields are available to add options for the running of the test:

• The first, Test Class, specifies which Selenium test will be run by this script. It isneeded for the script to run.

• The second, Classpath, specifies any JAR files that will be used by theSelenium test that are not specified in the selenium.properties file.

• The last one, Java System Properties, is where any Java system argumentscan be passed to the Java Virtual Machine (JVM).

The Selenium test example was created with a meaningful name and organized intoa package. This means that a specific Selenium test can be selected to be run bya test script from a JAR file that contains a library of other Selenium Tests. In thisexample, only one Selenium test exists in the JAR file, so it seems trivial, but havingmultiple tests organized into different packages makes it quick and easy to createtest scripts in Rational Quality Manager, which will run them.

Figure 10. Using one Selenium test library for multiple scripts

3. Specify the Test Class as com.example.tests.ExampleSelTest.

Page 13: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 13 of 18

Figure 11. JUnit Selenium script details example

As shown in the diagram in Figure 10, by changing the test class property in differentscripts, a different test will be run from the same JAR file.

1. Save the test script to finish.

Run the automated test

To run a test script. it needs to be within a test case and be started from there. Eithercreate a new test case from the menu (Construction > Create > Test Case) or addthe test script to an existing test case in a Rational Quality Manager project.

Figure 12. Create a new test case

Page 14: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 14 of 18

To add the test script to either a new test case or an existing one:

1. Open the test case, and navigate to the Test Scripts section on the left side of atest case summary.

Figure 13. Test Scripts section location

2. Use the green plus sign Add Test Script icon to bring up a list of all test scripts inthe Rational Quality Manager project.

3. Find the correct test script in the list, check the box to select it, and then clickOK.

4. Save the test case.5. You can run it by selecting Run Test Script > Run.6. The next window asks you to specify details for the test case run.

a. Make sure that the Selenium test script is the one being run.b. Also, ensure that the adapter that was started earlier is running, as

indicated by a green circle (see Figure 14).

Page 15: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 15 of 18

Figure 14. Run test case parameters

7. Click OK to run the test.

View the results

After the test has finished, a Show Results button should appear. Click that, andRational Quality Manager will display the detailed log of the test run. A Pass or Failverdict will be shown for the test, as well as other information, such as the test runtime. This sample test example contained only one step, but a more complicatedSelenium test would show a step-by-step guide of which verifications have passedand failed.

Page 16: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 16 of 18

ResourcesLearn

• Check the Rational Quality Manager and Rational Test Lab Manager pages ondeveloperWorks for links to product documentation, articles, tutorials, courses,downloads, and other useful areas. Also, explore the Rational Quality ManagerInformation Center for technical details, check the Rational Quality ManagerRoadmap for links to more resources, and browse the IBM Quality Managementpage to learn more about what else is available. For how-to tips, read thesedeveloperWorks articles by Michael Kelly:

• Lab Management using IBM Rational Test Lab Manager: Extending IBMRational Quality Manager for collaborative, comprehensive test planningand test asset management (developerWorks, October 2008)

• Getting started with IBM Rational Quality Manager (developerWorks,December 2008)

• Managing your first project with IBM Rational Quality Manager(developerWorks, October 2008)

• Stay current with developerWorks technical events and webcasts focused on avariety of IBM products and IT industry topics.

• Attend a free developerWorks Live! briefing to get up-to-speed quickly onIBM products and tools, as well as IT industry trends.

• Watch developerWorks on-demand demos, ranging from productinstallation and setup demos for beginners to advanced functionality forexperienced developers.

• Improve your skills. Check the Rational training and certification catalog, whichincludes many types of courses on a wide range of topics. You can take someof them anywhere, any time, and many of the Getting Started ones are free.

Get products and technologies

• Download both the Selenium IDE and Selenium WebDriver from theSeleniumHQ website.

• Get Version 4 or later basic version of JUnit as a JAR file from the junit.orgwebsite.

• Download the free trial version of Rational Quality Manager, which includesRational Test Lab Manager.

• Evaluate other IBM software in the way that suits you best: Download it for atrial, try it online, use it in a cloud environment, or spend a few hours in the SOASandbox learning how to implement service-oriented architecture efficiently.

Discuss

• Get involved in the Rational Quality Manager forum on developerWorks, whichalso includes discussions and information from IBM about Rational Test LabManager, as well as the Using Quality Manager forum on Jazz.net

Page 17: Run Automated Selenium Junit Tests PDF

ibm.com/developerWorks/ developerWorks®

Run automated Selenium JUnit tests Page 17 of 18

• Share your knowledge and help others who use Rational software by writing adeveloperWorks article. Find out what makes a good developerWorks articleand how to proceed.

• Join the Rational community to share your Rational software expertise and getconnected with your peers.

• Rate or review Rational software. It's quick and easy.• Follow Rational software on Facebook, Twitter (@ibmrational), and YouTube,

and add your comments and requests.

Page 18: Run Automated Selenium Junit Tests PDF

developerWorks® ibm.com/developerWorks/

Run automated Selenium JUnit tests Page 18 of 18

About the author

Matthew J. Bradshaw

Matthew joined IBM Rational software in August 2011 as a technicalconsultant for the quality management products. At the Universityof York, he studied computer science, covering topics such asprogramming principles, Java, and software engineering best practices.

© Copyright IBM Corporation 2012(www.ibm.com/legal/copytrade.shtml)Trademarks(www.ibm.com/developerworks/ibm/trademarks/)