Friday, August 24, 2012

Selenium-RC: Tutorial



Selenium-RC: Tutorial
This Selenium Remote Contrtol tutorial will teach you how to start the Selenium Server from the command line, and how to use the Server in interactive mode. It assumes that you are familiar with running programs from the command line. At the end, we'll demonstrate how to write a simple test that does the same thing as what we did in interactive mode.


The Selenium Server is written in Java, and requires the Java Runtime Environment (JRE) version 1.5.0 or higher in order to start. You may already have it installed. Try running this from the command line:
java -version
You should see a brief message telling you what version of Java is installed, like this:
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode)
If you see an error message instead, you may need to install the JRE, or you may need to add it to your PATH environment variable.
For this example, you'll also need to make sure that you have a supported browser installed. 
Windows: If you're on Windows XP or Windows 2003, you can just use Internet Explorer for this example, or install Mozilla Firefox or Opera. If you're using Windows 2000, you'll need to install reg.exe in order to use Internet Explorer, but Firefox should work regardless. We recommend (but do not require) that you add your browser executable to your PATH environment variable. (If you do not explicitly add your browser's installation directory to the PATH, then you must install your browser in its standard location; Firefox's standard location is "c:\Program Files\Mozilla Firefox\firefox.exe"; Internet Explorer's standard location is "c:\Program Files\Internet Explorer\iexplore.exe".) 
Unix/Linux: For this tutorial, install Firefox and add the Firefox directory to your PATH environment variable. Note that on Unix/Linux we'll be trying to invoke "firefox-bin" directly, so make sure that executable is on the path; also don't forget to add the Firefox libraries to your LD_LIBRARY_PATH. If needed, we can invoke Firefox using a shell script (e.g. "firefox" or "run-mozilla.sh"), but in that case, we may not be able to stop Firefox until the server is shut down. 
Mac OS X: On Mac OS X, it should be enough to install Firefox.app in your /Applications directory. Note in order to control the browser accurately, we need to invoke the embedded Firefox executable (firefox-bin) directly in /Applications/Firefox.app/Contents/MacOS; if your browser isn't installed there, then you'll want to add the correct embedded location to your PATH environment variable as well as your DYLD_LIBRARY_PATH environment variable.

Interactive Mode
Selenium Server "interactive mode" is a way of rapidly prototyping tests that requires no coding whatsoever, so it's a good way to introduce new users to Selenium Remote Control. In interactive mode, you type your commands one by one into the Selenium Server command window; this allows you to immediately see the results of running your command in a working browser, on-the-spot. With that said, normally you'll be coding these tests in your favorite programming language, so the whole thing is completely automated.
Once you've got Java installed and ready to go, you can start the Selenium Server from the command line like this:
java -jar selenium-server.jar -interactive
That will start the Selenium Server and allow you to type commands in the command window. After a number of log messages, you should see the following message:
Entering interactive mode... type Selenium commands here (e.g: cmd=open&1=http://www.yahoo.com) 
Let's begin by using Selenium Server to open up a browser. If you're on Windows and want to run your Selenium commands using Internet Explorer, try typing this:
cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com
If you want to use Firefox, try this:
cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com
You're running the "getNewBrowserSession" command, using the browser of your choice (*iexplore for Internet Explorer, *firefox for Firefox, or *opera for Opera), starting at www.google.com. Typing commands in this window automatically fires off HTTP web requests to the Selenium Server, requesting work to be done. (In non-interactive mode, you can use any automated tool you like to send these HTTP requests to the server, instead of typing them by hand.) When you press Enter, you'll see a message describing the request you just made:
---> Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com
If all goes well, you should see a new browser window starting with the browser of your choice. Back in the Selenium Server command window, you should see the following message:
Got result: OK,260113 on session 260113
(If this doesn't happen, you may need to take a look at our fine Troubleshooting guide.)
The first part of this message "Got result: OK" says that your request for work was successful. The second part of this message, the number, is a Session ID. The Session ID will be different every time you run the "getNewBrowserSession" command from the command line.
Let's try some more commands; let's do a Google search! We'll start by opening up the Google website. Type this in the Selenium Server command window, replacing the Session ID below with the number you got when you ran "getNewBrowserSession":
cmd=open&1=http://www.google.com/webhp&sessionId=260113
When this command finishes, your browser window should reveal google.com in the lower frame. (The /webhp makes sure we stay on www.google.com; otherwise, we might get redirected to a country-specific Google server, e.g., www.google.fr.) Don't forget that you need to replace the Session ID above with your own personal Session ID, the one you got when you ran the "getNewBrowserSession" command.
Now that Google is open, let's try typing something in the search box. Type this in the Selenium Server command window:
cmd=type&1=q&2=hello world&sessionId=260113
Again, don't forget to replace the Session ID with your own Session ID. If all goes to plan, you should see "hello world" in the search box for your Google search.
Now, let's do a search!
cmd=click&1=btnG&sessionId=260113
You should now see the results of your Google Search in your browser window. You can run dozens of Selenium commands to automate all manner of browsing tasks. For more information on particular commands, you can go look at the Selenium Core section at the Open QA website, or check out the reference materials available for any of our Client Drivers (Java, .NET, Perl, PHP, Python or Ruby).
The next thing we might want to do is read some information back out of the page... for example, let's retrieve the HTML title of the current page, like this:
cmd=getTitle&sessionId=260113
The browser will return the title of the HTML page we've loaded, like this:
Got result: OK,hello world - Google Search on session 260113
That's enough Interactive Mode for now; let's move on to writing some code! But before we go, let's stop the browser we started. Type this command in the Selenium Server command window:
cmd=testComplete&sessionId=260113
When this command finishes, your browser window should automatically close. To quit the Selenium Server, type "quit" or just press Ctrl-C.

No comments:

Post a Comment