Selenium Ide Tutorial

  • 13,166 views
Uploaded on

Selenium IDE tutorial for PHP programmers

Selenium IDE tutorial for PHP programmers

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
13,166
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
736
Comments
0
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. www.codediesel.com Author : Sameer Borate Selenium IDE Tutorial January 2009
  • 2. PART 1 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. The IDE can help you to record, edit and debug tests. Currently the IDE is only available for Firefox (as if we developers will use any other) as a add-on. Here is a possible scenario for using Selenium. Imagine you have created a HTML form with about twenty fields and you have to repeatedly test the form. Filling the form every time can quickly become tedious. With Selenium you can automate the whole process and run the test as required. In this part we will see how to create a simple test in Selenium. So let’s get started. STEP 1 - installation : You can download Selenium IDE from this locations. http://selenium-ide.openqa.org/download.jsp https://addons.mozilla.org/en-US/firefox/addon/2079 Once the add-on is installed make sure you do not forget to restart Firefox. STEP 2 - running a simple test : a. Start Selenium IDE in Firefox: Tools->Selenium IDE. You will see the following popup.
  • 3. 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. 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.
  • 4. 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.
  • 5. 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. You have just completed your first test run. And it was so simple.
  • 6. PART 2 In part 1 of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit. In this part we will use Delicious as our test target. I’ve already created a small Selenium test that you can use. Starting up. Restart the Selenium IDE. In the source tab replace the default HTML with the one from the above file. Set the Base URL to http://delicious.com/. After doing this your IDE should like below. If you are on a slow internet connection than it may help to slow the test speed.
  • 7. So what exactly does this test do? Simple: 1. First it opens the delicious home page 2. Then it searches by the keyword ’selenium’ 3. Reads the total number of bookmarks from the results page as shown below Now this may not look like a test for you , but it is just an exercise in using Selenium, you will surely apply it to a good test case. Onwards… Before running this test make sure that you are logged out of Delicious. Now run the selenium IDE test. After the test is successfully completed this is what you should see in the log pane. Is that all? If this was all there was I wouldn’t be really writing this post. The interesting part comes now - running the same test from PHP. From the file menu select ‘Export Test Case As…’ and as we are using PHP, select ‘PHP - Selenium RC’. Save the file by the name ‘Example.php’.
  • 8. This is what we will get in the ‘Example.php’ file.
  • 9. A short notice. Before we proceed a few points to consider. Make sure that the file name and the class name are the same, here ‘Example’. Say if you change the class name to ‘Delicious’ make sure that you also change the file name to ‘Delicious.php’. What the code basically does is it initializes the browser to ‘chrome’ (you can set the browser to your preference by changing to ‘*iexplore’ or ‘*firefox’); sets the baseurl to ‘http:// delicious.com/’ and runs the test. What's the complex looking line!? Everything may look fine to you except maybe the last line - ‘$this->getText…’. The function ‘getText’ returns the text found at the element specified in the expression. The complex looking line is an xpath expression to the results element: You don’t have to rake your brains to find an xpath to an element. An easy way is to use the XPather Firefox addon to get the xpath of any element on a page. After you download and in- stall the addon, restart Firefox, right-click on any element and from the context menu select ‘Show in XPather’. XPather retrieves the xpath to the specified element, which you can than use in your test code. Downloading and installing Selenium RC Selenium RC is a Java based command line server that starts browsers and runs commands you pass from your tests. 1. First make sure you have a Java runtime installed on your machine. 2. Download Selenium RC from here. 3. After extracting the files from the archive copy the ’selenium-server.jar’ file to any directory you feel appropriate. I copied it to my PHP installations bin directory. 4. Start the Selenium RC server from the command-line by issuing the following command: java -jar selenium-server.jar This will start the server on port 4444. 5. Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing. Installing PHPUnit 1. An easy way to install PHPUnit is to use the PEAR installer. The PEAR channel (pear.phpunit.de) is used to distribute PHPUnit so make sure that it is registered with your lo- cal PEAR environment: pear channel-discover pear.phpunit.de After the channel is registered install PHPUnit: pear install phpunit/PHPUnit
  • 10. Actual testing Now that PHPUnit is installed and the Selenium RC server is up and running, its time to run our test we saved before in our ‘Example.php’ file. Type the following on your command-line: phpunit Example This will start the test. The PHPUnit Selenium driver will execute each test command from your file and send it to the Selenium server, which does the job of launching the appropriate browser, opening web pages, and performing various specified actions; and closing the browser after the test completes. Now the above test does basically nothing important. So we will add some simple conditional statements to the code. This is what PHPUnit has to say about the above test. Or we can use an assertion as below, deliberately rigged to fail the test:
  • 11. Now PHPUnit will shout failure: You can look at more functions to implement in your tests here: 1. http://www.phpunit.de/wiki/Documentation 2. http://selenium-core.seleniumhq.org/reference.html This concludes the tutorial on selenium. ORIGINAL POST http://www.codediesel.com/php/selenium-ide-tutorial-part-1/