手把手教你selenium_如何使用google浏览器

来源:互联网 发布:有什么变声软件 编辑:程序博客网 时间:2024/05/15 01:38

 


 .现在浏览器种类太多了,但是项目开发的时候是推荐用户使用google浏览器,所以最好在google浏览器上测试。

 

 

这事就讨厌了,照理首选qtp,不过这货现在变成了伤不起的胖纸,以前还几百兆呢,上次再见就一个多G了,最新的qtp据说已经肥到了坑爹的三个G,且不说需要去搞一个DB,还得装一个google浏览器的补丁,但是即便如此,代码是不可以录制的,只能徒手编程,神 哪。算了吧。

 

 

现在手上有个Testcomplete7.5,这个工具的能力毋庸置疑,的确不错。不过TC7.5支持比较好的是IE6。-_-|||

 

 

最后我没辙了,只好选择了selenium来完成这个任务。

 

 

说两部分,一部分是交互模式的,一部分是代码中自动回放的,其实都是一回事。

 

 

 

第一部分:交互模式使用google浏览器

 

在selenium中启用google浏览器是很简单,其实只是一个配置的不同而已。之前启动seleniumRC是命令:

java -jar selenium-server-standalone-2[1].12.0.jar

 

现在要修改为交互模式:

java -jarselenium-server-standalone-2[1].12.0.jar –interactive

 

 

<引用>: Selenium服务器的“交互模式”是一种不需要任何代码的原型测试,因此有必要对初学者介绍Selenium Remote Control。在交互模式下,你可以在Selenium 服务器窗口中输入一行行的命令,在工作窗口的某个位置中立刻会执行你命令的结果。通常你可以选用你喜欢的语言编写你的测试代码,整个过程都是完全自动化的。

 

 

以我的理解,交互模式可以理解为一个命令解释器,selenium的各种命令你可以在这里一个命令一个命令的执行。而其实质是eclipse中的命令传入seleniumRC中后,是生成了一个参数串,传递给了seleniumRC启动的服务器。

 

 

现在启动一下试试:

F:\TOOL\java\prjSelenium\seleniumRC>java -jar selenium-server-standalone-2[1].12.0.jar -interactive

2011-11-25 15:36:32 org.openqa.grid.selenium.GridLauncher main

信息: Launching a standalone server

15:36:33.000 INFO - Java: Sun Microsystems Inc. 16.0-b13

15:36:33.000 INFO - OS: Windows XP 5.1 x86

15:36:33.016 INFO - v2.12.0, with Core v2.12.0. Built from revision 14666

15:36:33.188 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub

15:36:33.188 INFO - Version Jetty/5.1.x

15:36:33.188 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]

15:36:33.188 INFO - Started HttpContext[/selenium-server,/selenium-server]

15:36:33.188 INFO - Started HttpContext[/,/]

15:36:33.234 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@b8deef

 

15:36:33.234 INFO - Started HttpContext[/wd,/wd]

15:36:33.234 INFO - Started SocketListener on 0.0.0.0:4444

15:36:33.234 INFO - Started org.openqa.jetty.jetty.Server@122cdb6

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

输入: cmd=getNewBrowserSession&1=*googlechrome&2=http://www.google.com

15:36:53.360 INFO - ---> Requesting http://localhost:4444/selenium-server/driver

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

15:36:53.516 INFO - Checking Resource aliases

15:36:53.516 INFO - Command request: getNewBrowserSession[*googlechrome, http://

www.google.com] on session null

15:36:53.516 INFO - creating new remote session

15:36:55.266 INFO - Allocated session d1a4c7b4feda46719bdc3779da4c1e6d for http:

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

15:36:55.266 INFO - Launching Google Chrome...

15:37:02.454 INFO - Got result: OK,d1a4c7b4feda46719bdc3779da4c1e6d on session d

1a4c7b4feda46719bdc3779da4c1e6d

15:37:07.344 INFO - Couldn't proxy to http://otvhjaxnnw/ because host not found

15:37:07.344 INFO - Couldn't proxy to http://ybovcjpmnr/ because host not found

15:37:07.344 INFO - Couldn't proxy to http://wbkbpmwxwd/ because host not found

15:37:08.907 INFO - Started SocketListener on 0.0.0.0:14466

15:37:09.266 INFO - Started SocketListener on 0.0.0.0:14469

 

 

你可能会问为啥知道输入:*googlechrome,其实可以用命令查询一下支持哪些浏览器,如下:

输入:cmd=getNewBrowserSession

16:07:46.621 INFO - ---> Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession

16:07:46.637 INFO - Command request: getNewBrowserSession[, ] on session null

16:07:46.637 INFO - creating new remote session

16:07:46.637 INFO - Got result: Failed to start new browser session: Browser not

 supported:

(Did you forget to add a *?)

 

Supported browsers include:

  *firefox

  *mock

  *firefoxproxy

  *pifirefox

  *chrome

  *iexploreproxy

  *iexplore

  *firefox3

  *safariproxy

  *googlechrome

  *konqueror

  *firefox2

  *safari

  *piiexplore

  *firefoxchrome

  *opera

  *iehta

  *custom

 on session null

 

 

正常启动了浏览器以后,会获得一个sessionid,如下图,以后要调试命令就可以加上这个sessionid了

 

001.JPG

 

比如你要向google浏览器中的搜索栏中输入内容

cmd=type&1=q&2=搜索内容&sessionId=d1a4c7b4feda46719bdc3779da4c1e6d

 

 

这里的1=q是什么意思?这里的q是搜索栏的名称

 

 

002.JPG

 

这下没问题了吧,就是单步执行命令而已。

 

什么?selenium有哪些命令?看看之前的< Selenium 中文API.doc >吧。

 

 

第二部分:代码中自动启动googlechrome浏览器

 

这个直接将原来的:

selenium = new DefaultSelenium("localhost",4444, "*chrome","http://weibo.com/login.php");

 

修改为:

selenium = newDefaultSelenium("localhost", 4444, "*googlechrome", "http://weibo.com/login.php");

 

 

没了,就这么简单。