copy from: http://www.jroller.com/selenium/
3. Getting Started/Selenium RC steps
4. Selenium Tips and Quirks/FAQ
Selenium Introduction/Overview
What does selenium do ?
You can use opensource(ie free!) - Selenium tool ( selenium IDE is a plugin to firefox) to record and playback tests (like WinRunner, QTP). You can then export the recorded test in most language e.g. html, Java , .net , perl , ruby etc. The exported test can be run in any browser and any platform using "selenium remote control".
Selenium Flash Demo(Highly recommended for beginners) :
http://wiki.openqa.org/download/attachments/400/Selenium+IDE.swf?version=1
What are the components of selenium ?
Selenium IDE - Plugin to Firefox to record and play test in firefox
and also export tests in different languages. The most appealing
format is the html test case which seems to based on fit html .
Selenium RC- Allows playing of exported test in different platform/OS
Selenium Grid - Allows to control lots of selenium machines(you typically dont need this only for load test -and hence may ignore it)..
Other Project Management/wrapper tools around selenium:
Bromine(wrapper for selenium tests):
http://bromine.openqa.org/
NOTE: The above tool seemed hard to install - besides looking primitive when I last checked - and did not support "HTML test case"
How does Selenium remote control internally works ?
Selenium RC launches the browser with itself as the proxy server (hence you may get certificate warning in some modes) and then it injects javascript - to play the test. This also means it can easily work in ALL browsers/platform - and it can be easily used to test AJAX(unlike professional tools).
What are the different modes that Selenium RC uses ?
With latest release of Selenium RC(1.0 beta 2) there
are two modes for IE *iexplore(same as *iehta) and *iexploreproxy.
Similarly for Firefox use *firefox(same as *chrome) and *firefoxproxy.
You should prefere *iexplore(ie *iehta) and *firefox (ie *chrome)
respectively as they work with cross domain.
Who should use it ?
Developers can use it - for “browser” regression testing ( and replace htmlunit/httpunit in some cases) .
Per the one of the forces behind selenium(Neal ford) - it should really be used by Business Analyst first .
QA should enhance/use it do regression test/cross browsers testing on all platforms .
Selenium IDE
In below screen shot - you can see that you need to do recording in main firefox window.
You can use selenium IDE to type any additional command that you may have missed(e.g. timeout)
In selenium IDE - you can open Selenium IDE Options - to change default timeout or
to add you own user extension to extend selenium.
Pre Requisite :
Install Firefox
Install Java jdk: Download jdk (not JRE) without Netbeans IDE or JEE. I am using jdk1.5(but it should work with jdk1.6)
Install (optional) Junit for java rc client(ONLY if you are using java client, you also need to add selenium java client driver in classpath)
Main TOOLS Installation:
Install Selenium IDE
After installation - you should see selenium ide option in firefox.
Install Selenium RC
After installation - go to directory containing file ‘selenium-server.jar‘-
you will need to add appropriate bat/sh script as mentioned in
below steps.
Getting Started with Selenium/ Selenium RC steps
Step a) Record your test case using Selenium IDE as shown in earlier flash demo.
For intial recording- avoid using "cross domain" testing (eg www.abc.com, www.def.com) and
preferably use http site only.(till you figure out the workarounds)
eg You can use my test case that opens this tutorial(it searches for selenium tutorial
on google and opens this tutorial):
<html>
<!--NOTE - Sometimes you may have wrong or blank base url - as seen on top of IDE-->
<link rel="selenium.base" href="http://www.google.com/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<!-- Open the base URL like google.com-->
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<!-- type selenium tutorial in text field which has id/name of q -->
<tr>
<td>type</td>
<td>q</td>
<td>selenium tutorial</td>
</tr>
<!-- click on submit button which has html id/name of btnG and wait for results -->
<tr>
<td>clickAndWait</td>
<td>btnG</td>
<td></td>
</tr><tr>
<!-- click on google results with text/link/href of below patern -->
<!-- Note selenim ide noted this as click. you have to change it manually to clickAndWait--><td>clickAndWait</td>
<td>link=Selenium Tutorial for Beginner/Tips for Experts</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
Step c) Export your recording test case - as html file
and/or optionally in programming languages like java, perl etc
Do I Export in HTML or do I export in programming language like java client ?
If you need dynamic capability (e.g. reading data from csv file or database) then
you have to export it in programming language like java.
You would also need to export in java - if your page has lots of flash
components. e.g.
http://www.adobe.com/devnet/flash/articles/flash_selenium_04.html
There is also attempt to make silverlight work with java and .net out here.
The client code using java also is more stable (less permission denied errors) then
the html case.
But the html is easy to modify/maintain and it generates nice results - whereas in
java client code it tends to be cryptic(but again html test case - does not seem to be flexible).
I personally prefer the html option for the long term(if i don‘t need dynamic capability)
Step d) Create a test suite
After you saved your html test case (eg save as a.html) - you need to create
a "Test Suite" (eg suite.html) - as you can NOT play test case directly.
Test suite is just a collection of test case which are defined/ordered using html table tags .
If you are adding another test case in the same test suite, then - initially - try to keep it to the
same - base URL(ie starting point)
- eg below
Suite.html
<table >
<tr><td>Overall Tests </td></tr>
<tr><td><a href="./a.html">TEST CASE A</a></td></tr>
</table>
Note:
1) The first row (e.g Overall Tests) in your suite is always considered as Header
2) Selnium RC does not support a TestSuite where you go across domains
(ie the only domain that you would be allowed is for e.g. www.abc.com)
You may be able to work around it with *chrome for firefox and *iehta for IE.
Continue Next step?
If you are happy in running you test suite manually in Firefox only with Selenium
IDE - then you can skip below steps.(Its also the most stable - with no cross domain/security
warnings)
But If you wish to run you test suite in IE or if you want to schedule your
test suite bat file automatically via inbuilt Microsoft Scheduler on XP
or cron on Unix - then follow below steps.
Step e) Run your exported html test case in IE(or firefox) using selenium rc
run it in command line/bat file with below command :
java -jar selenium-server.jar -multiwindow -htmlSuite "*iexplore" "https://www.abc.com" "C:\suite.html" "C:\results.html"
REM Use *chrome for firefox and try *iexploreproxy
You can then see the nice results it generated and you can write a simple script to
email the results to yourself in case of failure(The result html file will contained "failed"
keyword).
You may also schedule your test suite with default/inbuilt Microsoft Scheduler on XP.
NOTE:
1) You may use -port option to change ports in above.
2) You may have problems in using *iehta (necessary for removing security warning
and going across domains) with above options..
3)Warning:Its better to use *chrome mode - which is more stable. Else- You are likely
to get weird permission denied error or some script error with this option - or
maybe it will ask you to download your -.do/.faces page.
(But note - I don‘t seem to get similar problem with java client code ie
using the code that was exported to java)
In short firefox is more likely to be stable as compared with IE for selenium.
In rare cases - You may get some lock errors with firefox though.(in
which case you may try to create seperate profile eg http://seleniumhq.org/docs/05_selenium_rc.html#specifying-the-firefox-profile
or in worst case - reboot machine/reinstall it )
Continue Next step ?
If you want dynamic capability (eg reading from CSV file or reading from database) - then
you may want to export your test case in programming language like java etc.
else skip next section.
Step f) OPTIONAL - Run your exported test case in programming like java .
Once selenium rc is extracted/installed then go to the directory containing selenium-server.jar.
If you want to use selenium-rc using programming language like java/.net/php etc(most cases)
You may want to add a bat/sh script to keep selenium rc always running..
REM set PATH variable to contain location of firefox/IE binary
REM set PATH=%PATH%;C:\firefox/bin
java -jar selenium-server.jar -multiwindow
REM -interactive is more user friendly - but does not work with websites using non frameable pages
REM - optionally use -port , if you want to run more then 1 instance
Now compile your selenium java client code with junit jar files , use selenium
java client driver, point to above selenium RC server and run it.
Assuming you exported the test case with name "NewTest.java" - you
can try to use below bat file(after getting the junit*jar and selelenium-java-client.jar - and creating clases directory in same folder)
--------- eg. put below in compileJava.bat ----------------------------
REM Get junit*.jar (got from junit.org)
REM Get selenium-java-client-driver.jar
REM (got from selenum rc installation/selenium-java-client-driver* folder)
REM NOTE: classes directory should already exist in current directoryset CLIENT_JAR=.
set CLASSPATH=%CLASSPATH%;.;.\classes;;%CLIENT_JAR%\junit-4.4.jar;%CLIENT_JAR%\selenium-java-client-driver.jarREM COMPILE(note default package at first line is com.example.tests)
javac -d classes NewTest.javaREM ensure that selenim rc server is already running on current machine
REM Now running the actual test.java junit.textui.TestRunner com.example.tests.NewTest
REM - remove pause if you are going to schedule it.
pause;
This implies that java(or programming) code is dependent on
selenium rc server to be running on same(or other) machine. But the "html test case" - launches the "selenium rc server" every time.(hence you need different port - if you run html test cases in parallel)
TIP:
By default selenium java client does not give nice reports(seen with html option)
You may use loggingselenium" project to get the nice reports(along with timing).
Also if you use this, it will also embed the screenshot that you called- in the html file.
Best way is to run/edit- after right click/saving this attached code as the usage on their website is incomplete/confusing.
It depends on loggingselenium and apache commons jar.
Step g)(OPTIONAL) - Migrating your test case to Selenium 2
Since Selenium IDE does not support Selenium 2(also note that Selenium 2 does not support HTML test case).
You may use WebDriverBackedSelenium steps to migrate to Selenium 2 .
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
//*NEW _ ADDED THIS IMPORT
import org.openqa.selenium.*;
import org.openqa.selenium.ie.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.chrome.*;
public class Selenium2Test extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
//*NEW - Commented below line and added the remaining 2 lines.
//selenium = new DefaultSelenium
//("localhost", 4444, "*chrome", "http://www.bing.com/");
// WebDriver driver = new InternetExplorerDriver();WebDriver driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium
(driver, "http://www.bing.com");
//comenting below implies taht the default selenium rc server at port 4444 is running
//else if you uncommment it - then it will automatically launch selenium rc server.
// selenium.start();
}@Test
public void testUntitled2() throws Exception {
selenium.open("/");
selenium.type("sb_form_q", "selenium tutorial");
selenium.click("sb_form_go");
selenium.waitForPageToLoad("80000");
selenium.click("link=www.jroller.com");
selenium.waitForPageToLoad("80000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
You can get overview of internal Selenium (Webdriver) architecture at :
http://www.aosabook.org/en/selenium.html
You may download all the above code/scripts here.
However if ther are too much javascripts - consider use programming languate.
<td>//a[@id=‘overridelink‘] </td>
<td></td>
</tr>
With firefox 3 - you can permanently store the exception. However selenium -rc starts
new firefox profile every time(probably to get around lock issues) - hence you may
need to start firefox with same/particular profile.
You may also consider changing IE options to be strict (eg Tool->Internet Options->Advanced->Display
Notification on every script error) to have selenium catch any javascript issues/bugs in your website.
9-important-tips-for-selenium-remote-control-java-client-test-tool
Selenium core faq(eg permission denied) - Must read
Selenium rc Faq - Must read
PS: Autoit and autohotkey seem to have same codebase - and both have primitive recorder.
(else we probably wont have needed selenium)
But in autoit vs autohotkey - I will give slight edge to autoit.
NOTE: On windows - you should use ‘start‘ in front of exe/.au3 file to make it run in background -
and then run your selenium rc test in the same batch file(eg start autotItscript.au3)
There is nice article which links to - google download code - to extend selenium to
support loop(while) and goto..
http://51elliot.blogspot.com/2008/02/selenium-ide-goto.html
You should though export your test case in programming language - if you need
this kind of facility.(or see if you can wrap the html in JSP/asp .net)
You can get a list of all GOOD extension(eg UI-Element) at:
http://wiki.openqa.org/display/SIDE/Contributed+Extensions+and+Formats
Selenium is NOT designed for load test. However - some companies
can try to use Selenium Grid for low load testing or even give
it to some external companies - who will test your selenium test - with lots
of browsers.
There is also interesting attempt by "PushtoTest" to convert selenium
test to low level - "htmlunit" test - and hence make it suitable
for load testing. I don‘t think its stable yet - but please let me know if i am wrong.
eg
http://www.pushtotest.com/products/selenium-how-to/
You may have noted that selenium requires lots of workarounds - as it uses javascript (which are restricted by browsers) to drive the tests.
However in future selenium(2.0) - will merge with webdriver - allowing selenium tests to
easily run even without browser(and hence suitable for load test). Selenium 2 uses
native plugin to browser to run its tests - and hence does not faces the javascript limitations.
Disadvantage of Selenium 2:
It may not be easy for selenium 2 to support newer browser (eg androids) or newer IE versions- as they are not going
to provide native browser plugin for all browser(or even make it consistent).
As of now (may 2010)- I dont see any support for IDE(i expect this to happen eventually) nor is
there any talk of supporting HTML Test case(not sure if they would ever support this). (in fact I was hoping that
would support JSP natively - which should be easy for selenium rc)
Selenium 2 is documented at :
http://groups.google.com/group/webdriver/browse_thread/thread/b089f9a193e4eec9?pli=1
Using selenium with command line for IE or using Ant
Effective Selenium presentation zip
Advanced Selenium presentation pdf
If you are using "exported" java client code with selenium RC,
then the API for two main class are:
(There is facility for screen shot - but it appears to be protected )
b)
Selenium vs Low/Network level tools like Webtest/HttpUnit/HtmlUnit(java)
You should continue low level tools like httpunit etc to use
it for monitoring - but don‘t use those tools for browser/regression testing.
Selenium vs Watir
If you like your test cases in Ruby (or maybe .net) and are willing to
dig through "hard to understand" documentation - then Watir is a tool
for you.(if you are perl/php/java shop - don‘t even bother considering Watir
for now)
Watir does have advantage that its recorder uses IE to record.
With Selenium - if your site does not work with Firefox - you will
have to manually write the code(not big deal)..
But the biggest advantage of Selenium is its support for FIT HTML
and the vast languages/browsers/platform that it supports.
(with good documentation)
But maybe things may change in future.
Selenium vs Imacros
Imacros appeared better in recording. But overall its not as powerful for automation(or scheduling).
Selenium vs Dejaclick
Dejaclick is probably the best (free) macro tool for websites record and play. However Selenium beats DejaClick in overall features(multiple language suport vs only XML support, free automation vs paid automation).
Dejaclick Advantage:
It nicely plays back the recorded stuff - by highlighting appropriate items in green.
It can easily record flash/silverlight - by recording position.(however it may mean that it may not
nicely work in all computers)
It has facility to encrypt sensitive data in your file.
Dejaclick easily allow you to bookmark your script. So that you can play it back in one click.
You can easily toggle Dejaclick to True Screen mode(ie recording by mouse positions) by pressing Alt +
(some stuff are hard to record by name/ids)
Disadvantage:
Dejaclick recording tool is free - But for automation you have to rely on paid site alertsite (AlertSite however provides great facility for running your dejaclick script and even soap xml from most of cities/region in US/world. This will allow you to monitor your CDN performance/reliability or what content your website is serving across different regions)
Miscellaneous(not related to selenium)
About me/Other blogs(unrelated to selenium):
If you want to use other cool firefox plugins(eg yslow),
or use olap(pentaho) in java(eg seam) then you may check my main bloghttp://www.jroller.com/zahid .
I have worked in past for clients like Hitachi(PCS), Bank One/Chase, Bill Me Later(Kaloke)
and Verizon. I have designed/coded in various systems using languages like MacApp
(Mac C++), VC++ and Java.
All of my blogs are my personal views - and they dont represent the views of
any companies that I have worked in past/present.
I am currently trying to develop blog on choosing Android phone/tips/top apps/app development
原文:http://www.cnblogs.com/zhang-pengcheng/p/5040826.html