手把手教你selenium_第一个脚本_登录新浪微博

来源:互联网 发布:c语言从入门到入坟 编辑:程序博客网 时间:2024/05/29 06:53

假定各位以前看过前两篇文章,已经搭建好了环境,现在开始录制回放第一个selenium的脚本:登录新浪微博

 

 

基本的步骤是:

1.  打开firefox,设置seleniumIDE选项,使之可以录制下junit的代码;

2.  使用seleniumIDE进行录制,生成代码;

3.  设置断言;

4.  打开eclipse,创建工程,新建包,包下面创建类,贴入代码;

5.  工程引用相关的库文件;

6.  调试:一步步运行回放

 

 

第一步:

打开firefox,设置seleniumIDE选项,使之可以录制下junit的代码;

 

先在firefox的工具菜单中打开seleniumIDE,然后在seleniumIDE1.4.1窗口中,打开菜单<options-options…>,按照下图设置:

 

001.JPG

 

 

设置好该项后,就可以录制出junit的脚本,否则缺省录制出selenium脚本,selenium脚本可以在seleniumIDE中回放,而junit的脚本就需要导入ecplise中来执行了。

 

002.JPG

 

 

 

 

第二步:使用seleniumIDE进行录制,生成代码;

 

seleniumIDE点击录制,在firefox中访问网址,进行操作,操作完成后,页面暂时不要关闭,seleniumIDE停止录制,这时可以看到生成的junit代码,记得我们的版本是junit4呀。

 

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;

 

public class Untitled extends SeleneseTestCase {

         @Before

         public void setUp() throws Exception {

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

                   selenium.start();

         }

 

         @Test

         public void testUntitled() throws Exception {

                   selenium.open("/login.php");

                   selenium.click("id=loginname");

                   selenium.type("id=loginname", "xxx@163.com");

                   selenium.type("id=password", "xxx");

                   selenium.click("css=#login_submit_btn > img");

         }

 

         @After

         public void tearDown() throws Exception {

                   selenium.stop();

         }

}

 

 

 

第三步:设置断言

 

断言是代码级别的称谓,对于测试就是校验点,假定以是否出现发微薄的文本框为是否成功登录的校验点

 

 

打开firebug:

 

004.JPG

 

005.JPG

 

 

先点击firebug的<点击查看页面中的元素>,将鼠标移动到文本框控件上面,会出现浮动的蓝色框,点击鼠标左键,如下图,在文本上右键鼠标,选择复制XPath,得到该文本框的唯一引用

 

006.JPG

 

在点击了登录按钮后,新增加一句:

assertTrue(selenium.isElementPresent("xpath=/html/body/div/div[2]/div/div/div/div/div/div[4]/div/div/textarea"));

 

断言是什么?要不先翻翻手册吧。

 

 

 

第四步:打开eclipse,创建工程,新建包,包下面创建类,贴入代码;

 

创建工程:File—New—Projects…--java projects—项目名称:prjSelenium1—Finish

创建包:如下图选择上新建的项目-- File—New—Package—包名:pkgTest

 

007.JPG:

类似上图,选择上包-- File—New—Class—类名:ClsTest—finish

 

把之前的代码贴入ClsTest.java,修改下面标粗的内容:

package pkgTest;

 

import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.util.regex.Pattern;

 

public class ClsTest extends SeleneseTestCase {

    @Before

    public void setUp() throws Exception {

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

       selenium.start();

    }

 

    @Test

    public void testUntitled() throws Exception {

       selenium.open("/login.php");

       selenium.click("id=loginname");

       selenium.type("id=loginname", "xxx@163.com");

       selenium.type("id=password", "xxx");

       selenium.click("css=#login_submit_btn > img");

      

        assertTrue(selenium.isElementPresent("xpath=/html/body/div/div[2]/div/div/div/div/div/div[4]/div/div/textarea"));

    }

 

    @After

    public void tearDown() throws Exception {

       selenium.stop();

    }

}

 

 

将包名和类名改的和新建的一致

 

 

第五步:工程引用相关的库文件;

 

可以看到现在的代码全是红杠杠,那是因为找不到相应的库。

 

选择项目名,右键选择属性,按照下图设置:

 

008.JPG

 

此处的库文件包括了前文下载的<Selenium Client Drivers,SeleniumRC,JDK1.6>相关的jar包。

三类jar包分别是模拟客户端,模拟服务器端(细节请看Selenium私房菜(新手入门教程).pdf),虚拟机。

 

很快代码的红杠杠就没了,有些变成了黄杠杠,都是建议的,可以不用管他。

 

 

 

第六步:调试:一步步运行回放

 

首先需要启动seleniumRC,我自己创建了一个批处理,大家可以参考:

F:

cd F:\TOOL\java\prjSelenium\seleniumRC

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

 

服务启动完成后(启动后啥样子,前一篇文章已经讲过),在代码上设置一个断点,光标停留在需要中断的语句处,按下ctrl+shift+b,由于旁边有黄色标记挡住了,有点看不出来是否设置成功了,仔细看看,会发现有那么几个象素点的变化的。我把断点设置在了< selenium.start();>。现在可以清理一下环境了,关闭firefox和seleniumIDE(当然你喜欢乱点也无所谓),选择ClsTest.java,右键选择Debug As—Junit Test,代码就开始运行了,停在了断点处,后续就什么F5,F6,F7(到时候看看ecplise的Run菜单)啥的往后一步步执行了。

 

 

好了,看看最后效果如何:

 

009.JPG

 

 

 

 

期间可能会遇到的问题:

 

问题1:

java.lang.RuntimeException: Could not contact Selenium Server; have you started it on 'localhost:4444' ?

Read more at http://seleniumhq.org/projects/remote-control/not-started.html

Connection refused: connect

 

解决方法:运行前需要先启动seleniumRC,编写批处理:

F:

cd F:\TOOL\java\prjSelenium\seleniumRC

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

 

问题2:

java.lang.NoClassDefFoundError: com/google/common/base/Charsets

 

解决方法:添加:selenium-server-standalone-2[1].12.0.jar

 

问题3:

java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: java.lang.RuntimeException: Firefox 3 could not be found in the path!

Please add the directory containing ''firefox.exe'' to your PATH environment

variable, or explicitly specify a path to Firefox 3 like this:

*firefox3 c:\blah\firefox.exe

 

解决方法:

我的电脑-属性-高级-环境变量-系统变量-PATH

PATH=$PATH;D:\Program Files\Mozilla Firefox\

需要重新启动一次eclipse

 

 

0 0
原创粉丝点击