zoukankan      html  css  js  c++  java
  • Selenium tutorial/overview

    copy from: http://www.jroller.com/selenium/

    Selenium tutorial/overview

    1. Selenium Introduction

    2. Selenium Installation

    3. Getting Started/Selenium RC steps

    4. Selenium Tips and Quirks/FAQ

    5. Extending Selenium

    6. Selenium 2(pros/cons)

    7. Load testing with selenium

    8. Good Resources

    9. Alternatives to Selenium


    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.

    selenium/

    Selenium Installation

    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 b) Play your test case using selenium IDE itself to make sure
    that your test case works.. Modify the recorded code if necessary(eg replace click by clickAndWait
    or change timeout) as explained in Quirks section.

    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: esults.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 directory

    set CLIENT_JAR=.
    set CLASSPATH=%CLASSPATH%;.;.classes;;%CLIENT_JAR%junit-4.4.jar;%CLIENT_JAR%selenium-java-client-driver.jar

    REM COMPILE(note default package at first line is com.example.tests)
    javac -d classes NewTest.java

    REM 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();
    }
    }


    Warning: all cases may not be supported and even though selenium 2 is faster due to native support in browser - certain methods in WebDriverBackedSelenium
    will take more time.
    Hence it may be better to write clean Selenium 2 tests as shown at :http://seleniumhq.org/docs/03_webdriver.html .
    )

    You can choose to export the test in JUnit(WebDriver) in Selenium IDE
    - and then modify it to use PageFactory pattern.

    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.

    Selenium Tips and Tricks/FAQ

    • Do check the reference manual here to get concepts of selenium locators
      and the various commands that are available. You could get condensed version of the manual here.
      Without reading the manual - You may easily miss useful commands like pause(to test session timeout), storeHtmlSource (you can potentially applyselenium regular expression to verify the values of hidden variables insides that html) , typeKeys (to do things like page up etc)
    • Assert vs verify: If you use seleniums verify command - then the test suite/case - will still run - and fail at the end. Hence in most cases you should use assert command which will cause the test to stop.
    • Sometimes selenium IDE may use "click" instead of "clickAndWait" for some
      "submit" button cases resulting in your next page's assert/verify to fail. Hence
      you should change it manually or add waitForPageToLoad with some timeout.
    • If the recording is not working - then go to firefox and click on "Tools->Add on -> Selenium IDE " .First disable it and then enable it and restart the browser. Sometimes clicking on Selenium IDEs
      Options->HTML Format helps.
    • If web site does not allow itself to be frameable then use -multiwidow option(instead of -interactive) with selenium rc.
    • To eliminate certificate warning(and work cross domains ) use chrome/iehta modes -
      which is now default(as compared with rc beta1 version)
      http://selenium-rc.openqa.org/experimental.html

    • If you check selenium rc options eg "java -jar selenium-server.jar -h"
      then you will find that it has a "timeout" option. But that timeout is for the overall
      run(and not for individual "clickAndWait" ) . Hence you may be advised to use "setTimeOut" via your
      java program or in case of HTML use -Selenium IDE open test case-> Add Insert new command
      -> setTimeout with value(not Target) of say 240000 (ms)
    • Selneium has this cool - captureEntirePageScreenShot command which works
      well with IDE/HTML . Consider using it at critical points(to debug
      issues, if you schedule your test cases).
      But with programming language like java - it may not work(try
      using the experimental - captureScreenshot .
    • Consider writing a wrapper for your TestCase/TestSuite in .net/jsp
      so that you can maintain your testcase/test suite in HTML.
    • Sometimes the browsers or the java process may not get killed after the test is run.. So you may need to manually
      kill it(you can try to use some automated tools like Pskill on Windows ).
    • When your client java test (using RC) finishes with error - then it does not give clear error -
      you just the line number of the failed assert in stack trace - Hence you may need to trap all exceptions and - print the last body test
    • If you are testing with page where the id dynamically changes - then You may need to use lots of xpath in your test . In that case install firebug and use "Inspect element" by right clicking on the element -> Copy XPath by right click on bottom-> Add "/" in front of value you got in your clipboard-> paste this as a "target" in Selenium IDE for your "clickAndWait" command. You can check the validility by - click on find button in Selenium IDE to ensure that your XPath is correct(assuming that page is open) . You may find more tip on xpath using firebug/xpather here
    • If you are using Selenium HTML test case - then one way to remove hard coded values
      in your selenium test is to have simple JSP(or asp/php) that converts CSV file to HTML file.
      Then you just need to invoke that JSP page (for particular CSV) - and call various "store" values
      and replace hard coded values in your test with "get" values .
      That way you can just change your test/data by just replacing the CSV file.
    • Do use (minor) javascript in HTML test case. You would need javascript to access stored values anyway.
      eg store 1 abc (store on one page)
      , type id javascript{storedVars['abc'].toUpperCase()} (extract or evaluate it on another page)

      However if ther are too much javascripts - consider use programming languate.

    • The latest selenium rc(1.0 beta 2) does support Firefox 3..
      With firefox 3 you can easily add your local https server with invalid certificate in safe list(to remove warning message).
      Its difficult to do that with Firefox 2.
    • If you are running selenium rc test with IE a site which already has invalid certificate -
      then IE will give a warning for "invalid certificate". You can have selenium
      automatically click on "Continue" link in it(which has ID of overridelink) by adding
      below command in your IE specific test case.
      <tr>
      <td>clickAndWait</td>

      <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.

    • Consider use of UI-Element - which is now supported in RC here
    • There are other good tips here.

      9-important-tips-for-selenium-remote-control-java-client-test-tool
      Selenium core faq(eg permission denied) - Must read
      Selenium rc Faq - Must read

    • if you read above FAQ - you will realize selenium has limitation in IE with certain dialog box(eg upload file, basic auth). You can get around some of the limitation with autoit script
      from this site.
      Its advisable not to directly download the exe from above site - but rather install autoit, compile autoit script - from one of it program menus options - to Exe and than use the compiled exe.
      For whatever reasons (firefox does not expose its model)- the above scripts(or autoit) does not help in Firefox dialog boxes(but you dont need it anyway as in chrome mode - selenium automatically gets rids of dialog boxes)
      However if you are trying to get rid of basic authentication dialog - you can directly pass user/pass in URL.(however for IE you would need to add some registery settings)

      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)

    Extending Selenium

    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

    Load testing with Selenium

    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/

    Selenium 2

    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

    Good Resources/External Links

    Selenium Main site

    Using selenium with command line for IE or using Ant

    Effective Selenium presentation zip

    Advanced Selenium presentation pdf

    Selenium with Maven

    If you are using "exported" java client code with selenium RC,
    then the API for two main class are:

    a)

    http://release.openqa.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/SeleneseTestCase.html

    (There is facility for screen shot - but it appears to be protected )

    b)

    http://release.openqa.org/selenium-remote-control/0.9.0/doc/java/com/thoughtworks/selenium/DefaultSelenium.html

    Alternatives to selenium

    Selenium vs Low/Network level tools like Webtest/HttpUnit/HtmlUnit(java)


    If you are using a "low/network" level tool like htmlunit then it
    has the advantage of being fast - but its javascript will
    not work work in most complex websites(besides not being a browser test)
    Selenium(java, .net) code also has the disadvantage of being dependent on RC server(not big deal).

    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

  • 相关阅读:
    模仿outlook快捷方式栏的一个控件
    买了一本书《Programming pearls》编程珠玑(88上的数学题目(1))
    一个IE动画图标的小例子
    对水波特效实现原理的解释
    向量空间的几何变换
    一个简单Led控件
    【转载】配置(visual studio.net已检测到指定的web服务器运行的不是asp.net1.1版)
    最近学习ASP2.0相关的几个小问题(非创新性文章)
    一道c的面试题,大数相乘。
    Led控件(2)——Led显示屏模拟
  • 原文地址:https://www.cnblogs.com/zhang-pengcheng/p/5040826.html
Copyright © 2011-2022 走看看