您的位置:首页 > 其它

Selenium 网页测试工具和perl脚本结合,我自己记录的学习过程

2012-01-03 21:12 585 查看
很有意思的一点是那个 “Selenium Remote Contrtol ” 自己是像 一个http代理服务器一样的在机器上运行的,所以他可以模拟一个假的网页出来,由他启动的浏览器 地址栏显示的是www.google.com,但其实显示的经过他自己处理之后的东西了,不是google的网页来的。详细见http://selenium-rc.openqa.org/tutorial.html

(1) download Selenium IDE,a Firefox addon

=======================
http://release.openqa.org/selenium-ide/1.0-beta-2/selenium-ide-1.0-beta-2.xpi
After download this file, click "open file" menu in firefox, firefox will inform you to

install this plugin. When installation is complete , you can find a "Selenium IDE" item in

"tool" menu . click it will run the Selenium IDE.

see more details on http://wiki.openqa.org/display/SIDE/Home .

(2)use Selenium IDE to record a testcase. open webpage "http://www.google.com/" ,

=======================

click "start record" buttion in Selenium IDE, manually did some operations in the browser,then click "stop record " button in Selenium IDE. clicke "save testcase" menu to save the testcase

into a html file. click "file" -> "export testcase as "-> "Perl Selenium RC" , save the testcase into a

file testcase1.pl .

(3)Download Selenium Remote Control

=====================

http://archiva.openqa.org/repository/releases/org/openqa/selenium/selenium-remote-control/1.0-beta-1/selenium-remote-control-1.0-beta-1-dist.zip

extract it into directory "selenium-remote-control-1.0-beta-1".

cd to "selenium-remote-control-1.0-beta-1\selenium-server-1.0-beta-1"

now let's start the selenium server, see instructions on http://selenium-rc.openqa.org/tutorial.html.
-----------------------------------

1. requires the Java Runtime Environment (JRE) version 1.5.0 or higher

----------

C:\Documents and Settings\widebright>java -version

java version "1.6.0_05"

Java(TM) SE Runtime Environment (build 1.6.0_05-b13)

Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

-------------------------------------------------

2. start server in "interactive mode" . options descriptiosn can be found here http://selenium-rc.openqa.org/options.html.
use command "java -jar selenium-server.jar -interactive"

------

D:\selenium-remote-control-1.0-beta-1\selenium-server-1.0-beta-1>java -jar selenium-server.jar -interactive

15:16:47.533 INFO - Java: Sun Microsystems Inc. 10.0-b19

15:16:47.549 INFO - OS: Windows XP 5.1 x86

15:16:47.565 INFO - v1.0-beta-1 [2201], with Core v1.0-beta-1 [1994]

15:16:48.252 INFO - Version Jetty/5.1.x

15:16:48.283 INFO - Started HttpContext[/selenium-server/driver,/selenium-server

/driver]

15:16:48.299 INFO - Started HttpContext[/selenium-server,/selenium-server]

15:16:48.299 INFO - Started HttpContext[/,/]

15:16:48.471 INFO - Started SocketListener on 0.0.0.0:4444

15:16:48.471 INFO - Started org.mortbay.jetty.Server@665753

Entering interactive mode... type Selenium commands here (e.g: cmd=open&1=http:/

/www.yahoo.com)

-------

try typing this, "cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com"

cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

15:21:48.797 INFO - ---> Requesting http://localhost:4444/selenium-server/driver
?cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

15:21:48.813 INFO - Command request: getNewBrowserSession[*firefox, http://www.g
oogle.com] on session null

15:21:48.813 INFO - creating new remote session

15:21:48.813 INFO - Allocated session 405923c6bbb94c95acc49fc78975ba2d for http:

//www.google.com, launching...

15:21:48.922 INFO - Preparing Firefox profile...

15:22:12.031 WARN - GET /selenium-server/driver/?cmd=getNewBrowserSession&1=*fir

efox&2=http://www.google.com HTTP/1.1

java.lang.RuntimeException: Firefox refused shutdown while preparing a profile

at org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc

her.waitForFullProfileToBeCreated(FirefoxCustomProfileLauncher.java:277)

at org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc

her.launch(FirefoxCustomProfileLauncher.java:147)

at org.openqa.selenium.server.browserlaunchers.AbstractBrowserLauncher.l

aunchRemoteSession(AbstractBrowserLauncher.java:24)

at org.openqa.selenium.server.BrowserSessionFactory.createNewRemoteSessi

on(BrowserSessionFactory.java:312)

at org.openqa.selenium.server.BrowserSessionFactory.getNewBrowserSession

(BrowserSessionFactory.java:113)

at org.openqa.selenium.server.BrowserSessionFactory.getNewBrowserSession

(BrowserSessionFactory.java:78)

at org.openqa.selenium.server.SeleniumDriverResourceHandler.getNewBrowse

rSession(SeleniumDriverResourceHandler.java:653)

at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(Se

leniumDriverResourceHandler.java:410)

at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleComman

dRequest(SeleniumDriverResourceHandler.java:388)

at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(Selen

iumDriverResourceHandler.java:135)

at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)

at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)

at org.mortbay.http.HttpServer.service(HttpServer.java:909)

at org.mortbay.http.HttpConnection.service(HttpConnection.java:816)

at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:982)

at org.mortbay.http.HttpConnection.handle(HttpConnection.java:833)

at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:

244)

at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)

at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)

Caused by: org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc

her$FileLockRemainedException: Lock file still present! C:\DOCUME~1\WIDEBR~1\LOC

ALS~1\Temp\customProfileDir405923c6bbb94c95acc49fc78975ba2d\parent.lock

at org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc

her.waitForFileLockToGoAway(FirefoxCustomProfileLauncher.java:235)

at org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc

her.waitForFullProfileToBeCreated(FirefoxCustomProfileLauncher.java:275)

... 18 more

Server returned HTTP response code: 500 for URL: http://localhost:4444/selenium-
server/driver/?cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

-------------

cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com

15:26:04.811 INFO - ---> Requesting http://localhost:4444/selenium-server/driver
?cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com

15:26:04.827 INFO - Command request: getNewBrowserSession[*iexplore, http://www.
google.com] on session null

15:26:04.827 INFO - creating new remote session

15:26:05.389 INFO - Allocated session 0e2ffcbf1fe0471c9e43fe3e73052aaa for http:

//www.google.com, launching...

15:26:05.389 INFO - Backing up registry settings...

15:26:06.905 INFO - Modifying registry settings...

15:26:08.311 INFO - Launching Internet Explorer...

15:26:11.467 INFO - Got result: OK,0e2ffcbf1fe0471c9e43fe3e73052aaa on session 0

e2ffcbf1fe0471c9e43fe3e73052aaa

-------------------------------------------------------------

cmd=testComplete&sessionId=0e2ffcbf1fe0471c9e43fe3e73052aaa

15:36:30.995 INFO - ---> Requesting http://localhost:4444/selenium-server/driver
?cmd=testComplete&sessionId=0e2ffcbf1fe0471c9e43fe3e73052aaa

15:36:30.995 INFO - Command request: testComplete[, ] on session 0e2ffcbf1fe0471

c9e43fe3e73052aaa

15:36:31.010 INFO - Restoring registry settings (won't affect running browsers).

..

15:36:32.635 INFO - Got result: OK on session 0e2ffcbf1fe0471c9e43fe3e73052aaa

-----------------------------------------------------------

##########################

It looks like it dosen't work with my firefox3.

After searching in Selenium wiki(http://wiki.openqa.org/display/SRC/Home),

the "Firefox refused shutdown while preparing a profile" problem is a frequently asked question.

It seems to be a bug of this verison. So I download the "latest nightly build" one

( http://release.openqa.org/cgi-bin/selenium-remote-control-redirect.zip),
and try again, it worked this time.

########################

selenium-remote-control-1.0-SNAPSHOT\s

elenium-server-1.0-SNAPSHOT>java -jar selenium-server.jar -interactive

16:59:54.201 INFO - Java: Sun Microsystems Inc. 10.0-b19

16:59:54.201 INFO - OS: Windows XP 5.1 x86

16:59:54.217 INFO - v1.0-SNAPSHOT [1123], with Core v1.0-SNAPSHOT [2182]

16:59:54.389 INFO - Version Jetty/5.1.x

16:59:54.389 INFO - Started HttpContext[/selenium-server/driver,/selenium-server

/driver]

16:59:54.404 INFO - Started HttpContext[/selenium-server,/selenium-server]

16:59:54.404 INFO - Started HttpContext[/,/]

16:59:54.436 INFO - Started SocketListener on 0.0.0.0:4444

16:59:54.436 INFO - Started org.mortbay.jetty.Server@c40c80

Entering interactive mode... type Selenium commands here (e.g: cmd=open&1=http:/

/www.yahoo.com)

cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

17:00:07.092 INFO - ---> Requesting http://localhost:4444/selenium-server/driver
?cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

17:00:07.248 INFO - Checking Resource aliases

17:00:07.248 INFO - Command request: getNewBrowserSession[*firefox, http://www.g
oogle.com] on session null

17:00:07.248 INFO - creating new remote session

17:00:07.670 INFO - Allocated session e1fe00f7a7a441809ab126af638abc04 for http:

//www.google.com, launching...

17:00:07.826 INFO - Preparing Firefox profile...

17:00:13.420 INFO - Launching Firefox...

17:00:22.842 INFO - Got result: OK,e1fe00f7a7a441809ab126af638abc04 on session e

1fe00f7a7a441809ab126af638abc04

4) Try the exported perl script .

============================

On my computer ,there are two modules are required which were not supplied in the Selenium package.

it is "Test::Exception.pm " and "Sub::Uplevel.pm".

we can download it from http://search.cpan.org/, place them in the "perl lib" directory which other Selenium perl libs in.

--------------------

Added this into “testcase.pl” , so it can find our lib.

use FindBin qw($Bin);

use lib "$Bin/selenium-remote-control-1.0-SNAPSHOT/selenium-perl-client-driver-1.0-SNAPSHOT/lib";

-----------------------------

Now the script becomes:

use strict;

use warnings;

use FindBin qw($Bin);

use lib "$Bin/selenium-remote-control-1.0-SNAPSHOT/selenium-perl-client-driver-1.0-SNAPSHOT/lib";

use Time::HiRes qw(sleep);

use Test::WWW::Selenium;

use Test::More "no_plan";

use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost",

port => 4444,

browser => "*chrome",

browser_url => "http://www.google.cn/" );

$sel->open_ok("/");

$sel->type_ok("q", "widebright");

$sel->click_ok("btnG");

$sel->wait_for_page_to_load_ok("30000");

$sel->is_text_present_ok("widebright个人空间");

$sel->click_ok("link=widebright的个人空间?);

--------------------------------------------

run this perl script ,

D:\Selenium>perl testcase1.pl

正常的话,可以看到脚本正常运行,同时firefox浏览器会被启动在进行交互操作。

-----------------------

5) more

*Selenium Core (http://selenium-core.openqa.org/)

Selenium Core is a test tool for web applications. Selenium Core tests run directly in a browser, just as real users do. And they run in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh. No other test tool covers such a wide array
of platforms.

*Selenium Grid (http://selenium-grid.openqa.org/)

Selenium Grid is a tool that dramatically speeds up functional testing of web-apps by leveraging your existing computing infrastructure. It allows you to easily run multiple tests in parallel, on multiple machines, in an heterogeneous enviroment.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: