selenium初步使用


selenium初步使用

selenium 大致可分为两个版本Selenium Core和SeleniumRC,第一种是以网页的形式编写TestCase,而后一种用Junit,并且支持多种语言,个人表较喜欢后一种,可以和 ANT整合用于自动化测试。本人也是初步使用,学的过程中主要结合API和编写好的用例说明,具体的情况可在问官方网站进行了解 http://www.openqa.org/selenium/
        API:   http://release.openqa.org/selenium-remote-control/0.9.2/doc/java/
        seleniumRC下载地址: http://selenium-rc.openqa.org/download.jsp运行测试前在DOS命令下启动selenium-server.jar(无需放到环境目录下):java -jar selenium-server.jar
具体测试用例如下:主要用于form表单的提交测试

package org.selenium.test;

import com.thoughtworks.selenium.SeleneseTestCase;

public class TestSelenium extends SeleneseTestCase ... {

    
public void testSelenium() throws Throwable ...{
        
try ...{
            
// 每个测试步骤间隔1秒
             selenium.setSpeed("1000");
            
// 打开测试的网页
             selenium.open("http://localhost:8080/TestProject/form/selenium.jsp");
            
// 获取标题
             assertEquals("selenium", selenium.getTitle());
             assertTrue(selenium.isElementPresent(
"xpath=//input[@name='username']"));
            
// 向input中type为text的栏位键入信息
             selenium.type("xpath=//input[@name='username']", "chenxu");
             selenium.type(
"xpath=//input[@name='password']", "password");
            
// 选择下拉列表
             selenium.select("xpath=//select[@name='select']", "index=1");
             assertEquals(
"1", selenium.getSelectedIndex("xpath=//select[@name='select']"));
             assertEquals(
"chenxu", selenium.getSelectedValue("xpath=//select[@name='select']"));
            
// 勾选或去选check/radio
             selenium.check("xpath=//input[@id='check2']");
            
//获取name为check栏位的id属性
             selenium.getAttribute("check@id");
             assertTrue(selenium.isChecked(
"xpath=//input[@id='check2']"));
             selenium.uncheck(
"xpath=//input[@id='check2']");
             assertFalse(selenium.isChecked(
"xpath=//input[@id='check2']"));
             assertEquals(
"chenxu", selenium.getValue("xpath=//select[@name='select']"));
            
// 点击提交按钮
             selenium.click("xpath=//input[@type='submit']");
            
// 等待页面载入
             selenium.waitForPageToLoad("3000");
            
// 获取新页面标题
             assertEquals("success", selenium.getTitle());
            
//确保测试中没有错误出现
             checkForVerificationErrors();
         }
finally ...{
            
//清空错误信息列表
             clearVerificationErrors();
         }

     }

}

assertTrue("disabled".equals(selenium.getAttribute("levelsForm:j_id275:0:j_id299@disabled")));

注:1,这里的ElementLocator使用xpath方式,表达式看上去复杂,其实并不难理解,如"xpath=//input[@id= 'check']"xpath=//是固定模式,input指form表单的input栏位,id自然就是 input中的id属性了。selenium可以使用多种元素定位方法(ElementLocator)具体方法可查阅API.
        2,这里的测试类继承了SeleneseTestCase,该类继承至Junit的TestCase,其setup()和teardown()方法用于初 始化和终止selenium服务,同时覆盖了一些断言(assert)方法。

 

Selenium 的几种使用

官方网站:http://www.openqa.org/selenium/ 下面我就要讲讲怎么使用selenium 这个对象来进行测试。

1、测试文本输入框

假设页面上有一个文本输入框,我们要测试的内容是 在其中输入一些内容,然后点击一个按钮,看看页面的是否跳转到需要的页面。

1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.type("xpath=//input[@name='userID']", "test-user");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }

上面的代码是这个意思:
1、调用 selenium.open 方法,浏览器会打开相应的页面
2、使用 type 方法来给输入框输入文字
3、等待页面载入
4、看看新的页面标题是不是我们想要的。

2、测试下拉框
java 代码

1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.select("xpath=//SELECT[@name='SBBUSYO']", "index=1");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }

可以看到,我们可以使用 select 方法来确定选择下拉框中的哪个选项。
select 方法还有很多用法,具体去看看文档吧。

3、测试check box

java 代码

1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.check("xpath=//input[@name='MEICK_000']");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }

我们可以使用 check 方法来确定选择哪个radio button

4、得到文本框里的文字
java 代码

1. assertEquals(selenium.getValue("xpath=//input[@name='WNO']"), "1");

getValue 方法就是得到文本框里的数值,可不是 getText 方法,用错了可就郁闷了。

5、判断页面是否存在一个元素
java 代码

1. assertTrue(selenium.isElementPresent("xpath=//input[@name='MEICK_000']"));

一般这个是用来测试当删除一些数据后,页面上有些东西就不会显示的情况。

7、如果有 alert 弹出对话框怎么办?

这个问题弄了挺长时间,可以这样来关闭弹出的对跨框:
java 代码

1. if(selenium.isAlertPresent()) {
2.
3. selenium.getAlert();
4.
5. }

其实当调用 selenium.getAlert() 时,就会关闭 alert 弹出的对话框。
也可以使用 System.out.println(selenium.getAlert()) 来查看对跨框显示的信息。


selenium开源功能测试工具


1.概述
        Selenium是非常非常有用的,对JavaScript支持良好的Web层功能测试,集成测试工具

        Selenium分为Core与RC(Remote Controll)两个部分,其中Core是基础的,直接在HTML Table里编写测试代码的模块,而Remote Controll则支持用Java等语言编写测试用例,并自动调用FireFox1.5来运行。

        具体的语法见http://www.openqa.org/selenium-core/usage.html

2.Better Practice
1. Never use Selenium FIT mode
        Selenium分为两种运行模式,Driven Mode(现在叫Selenium Remote Control)和FIT Mode(现在叫Selenium Core)。

        FIT Mode顾名思义,就是类似FIT Testing Framework那种使用方式,主要用于QA等非技术人员编写Web应用的功能测试。FIT Mode的Selenium测试使用HTML来组织测试用例。例如我要测试一个web应用的登陆功能。我可能写出这样的HTML 表格。

 1 <table>
 2 <tr>
 3  <td>open</td>
 4         <td>http://localhost:8080/login</td>
 5         <td></td>
 6 </tr>
 7 <tr>
 8  <td>type</td>
 9         <td>id=username</td>
10         <td>someuser</td>
11 </tr>
12 <tr>
13  <td>type</td>
14         <td>id=password</td>
15         <td>password</td>
16 </tr>
17 <tr>
18  <td>click</td>
19         <td>id=login_button</td>
20         <td></td>
21 </tr>
22 <tr>
23  <td>assertTextPresent</td>
24         <td>Welcome to xxxx</td>
25         <td></td>
26 </tr>
27 </table>
        不同于FIT,Selenium内置了一系列的命令,如上例中的open, type, click以及assertTextPresent,因此QA可以完全抛开DEV独立地编写测试(FIT需要DEV提供Behavior Fixture)。因此FIT Mode是相当容易使用的,哪怕不会使用HTML的QA,也可以使用FrontPage画出三列表格,依次填入数据。

        然而对于大多数team而言——尤其是敏捷team,FIT Mode平易的外表下是令人恐惧的泥沼。大多数团队往往选择使用Selenium作为功能测试和集成测试工具而不仅仅是QA测试工具,在不同的迭代间遇到 功能流程或UI变化时,必须要重构Selenium测试,或者说,Functional Test Migration。令人遗憾的是,HTML based的Selenium FIT Testing的重构竟然令人难以置信的困难。我们可以使用include等Selenium FIT扩展,使得它可以重用详细的功能(Log in, Log out诸如此类)。即便如此,在一个真实的项目中,Selenium Test的数量往往在200-500之间(我目前所处的项目在改用Driven Mode前已达350+),对于这么大基数的Selenium测试,手工重构几乎是不可想象的,而目前尚没有HTML代码重构工具。即便存在泛泛意义上的 HTML重构工具,对于Selenium测试重构的有效性尚待商榷。而使用Driven Mode上述代码可以写为:

1  public void testShouldShowAWeclomeMessageAfterUserLoggedIn()  {
2     selenium.open("http://localhost:8080/login");
3     selenium.type("id=username","someuser");
4     selenium.type("id=password", "password");
5     selenium.click("id=login_button");
6     assertTrue(selenium.isTextPresent("Welcome to xxxx"));
7}

        很自然,一个训练有素的程序员会重构出如下代码:

 1  public void login(String username, String password)  {
 2     selenium.open("http://localhost:8080/login");
 3     selenium.type("id=username",username);
 4     selenium.type("id=password", password);
 5     selenium.click("id=login_button");
 6}
 7 
 8  public void testShouldShowAWeclomeMessageAfterUserLoggedIn()  {
 9     login("someuser", "password");
10     assertTrue(selenium.isTextPresent("Welcome to xxxx"));
11}
        之后无论是pull up到公共基类还是extact到Utils class都是很容易的事情。由于Java在代码重构上便利,Java Selenium Remote Control成为使用Selenium的最佳方式。在这一点上,纵使Ruby语法上比Java简单灵活得多,它仍不是编写Selenium测试的最佳载 体(当然一个经过精心设计的ruby selenium dsl wrapper还是具有非凡的价值的,这个我们后面会涉及到)。

        2. Using the name user, system, page instead of selenium
观察上面提到的代码,其中使用selenium来操纵web应用的行为,这在Remote Control里是常见的做法,但是仍然不够好,我们可以做一些小的变化以得到更好的测试:

 1  protected void setup()  {
 2     selenium =   // intialize selenium instance
 3      user = selenium;
 4     currentPage = selenium;
 5}
 6 
 7  public void login(String username, String password)  {
 8     user.open("http://localhost:8080/login");
 9     user.type("id=username",username);
10     user.type("id=password", password);
11     user.click("id=login_button");
12}
13
14  public void testShouldShowAWeclomeMessageAfterUserLoggedIn()  {
15     login("some guy", "password");
16     assertTrue(currentPage.isTextPresent("Welcome to xxxx"));
17}
        基本上这只不过是"另一种写法"而已,但是它更好的表达了"用户的行为",如login代码所示。以及"系统的正确相应",即 currentPage.isTextPresent()。这种是典型的对编译器无意义对人有意义的代码,也就是普遍意义上好的代码。

3. Creating a DSL base on your test codes
        懂得HTML的QA可以在没有DEV的帮助下使用Selenium FIT mode,然而却不能在没有DEV的帮助下使用Driven Mode。于是最自然也是最fashion的做法,就是在已有的test codes之上提供Testing DSL或者Scripting Language,让FIT mode变得更加FIT。这方面内容是一个更大的主题,以后再详细展开吧。

4. Hacking Selenium Object to support FIT command

        Selenium FIT mode和RC mode下的命令有些许差异,比如FIT中的assertTextPresent,在RC中变成了isTextPresent。同样还有FIT中最实用的 命令clickAndWait,在RC中变成了click和waitForPageToLoad。在RC中使用FIT mode中的命令也非难事,找到com.thoughtworks.selenium.Selenium,添加方法:

 public void doCommand(String commmand, String parameters);
然后在com.thoughtworks.selenium.DefaultSelenium中添加实现:

1  public void doCommand(String commmand, String parameters)  {
2     String[] paras = new String[] {"","",""}
3    for (int i = 0; i < parameters.length && i < 3; i++)
4       paras[i] = parameters[i];
5    commandProcessor.doCommand(command, paras);
6}
然后试验一下:

 selenium.doCommand("clickAndWait");
        在我们使用纯RC mode之前曾经用过一段中间方案,将rc code转化为fit code来跑(因为rc不支持https),由于不是真正的rc mode,像isTextPresent之类的方法都没有办法使用,只能使用FIT mode command。因此如果因为一些特殊的原因(https, chrome起不来,hta bug多等等),你没有办法使用RC mode,但是有希望得到RC可重构的好处,那么这个tricky的技巧倒是不错的选择。

5. Using chrome and IE hta lanucher to support https
6. Run test using different browser lanucher to test browser compatibility
        这两个都是和browser lanucher相关的,Selenium和JWebUnit最大的不同在于它使用真实的浏览器来跑测试,从而可以更加真实地考察系统在不同浏览器中的表 现。因此使用不同的浏览器lanucher来运行测试,可以更好测试应用的浏览器兼容性,这对于web 2.0应用而言是很有帮助的。此外,使用rc提供的试验性lanucher,chrome和hta可以解决跨domain测试和https的问题。不过目 前hta还是有很多bug的,推荐使用chrome。当然,最希望的还是澳洲的同事可以早日在selenium里提供https支持。


Selenium sample

Selenium selenium = new DefaultSelenium("localhost",

       SeleniumServer.getDefaultPort(), "*firefox", "http://localhost:8080/gt15/");

selenium.start();

selenium.type("widget", "pg98-01");       
selenium.click("submit");
selenium.waitForPageToLoad("10000");

selenium.stop();

String name = selenium.getText("//div[@class='item']//a[@title='" + filename + "']");
//先找属性class="item"的div,再在里面找属性title=变量filename的超级链接a标签,getText是读出<a>和</a>之间的内容
String href = selenium.getAttribute("//div[@id='prest_list']//a[@title='" + filename + "']/@href");
类似,只不过读的是href属性



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium是一个用于自动化浏览器操作的工具,它可以模拟用户在浏览器中的行为,例如点击、输入文本、提交表单等。Selenium可以用于Web应用程序的功能测试、自动化脚本编写、数据抓取等场景。 Selenium支持多种编程语言,包括Java、Python、C#等。下面以Python为例,介绍一下Selenium使用步骤: 1. 安装Selenium库:在Python环境中使用pip命令安装Selenium库,命令如下: ``` pip install selenium ``` 2. 下载浏览器驱动:Selenium需要与特定浏览器配合使用,所以需要下载对应浏览器的驱动程序。常见的浏览器驱动有ChromeDriver、GeckoDriver(Firefox)和EdgeDriver(Edge)等。下载对应浏览器版本的驱动,并将驱动程序所在路径添加到系统环境变量中。 3. 创建WebDriver对象:在Python脚本中导入selenium库,并创建一个WebDriver对象,指定使用的浏览器和驱动程序。例如,使用Chrome浏览器和ChromeDriver驱动的示例代码如下: ```python from selenium import webdriver # 创建Chrome浏览器的WebDriver对象 driver = webdriver.Chrome() ``` 4. 打开网页:使用WebDriver对象的`get()`方法打开指定的网页。例如,打开百度首页的示例代码如下: ```python driver.get("https://www.baidu.com") ``` 5. 进行操作:使用WebDriver对象提供的方法进行各种操作,例如查找元素、点击按钮、输入文本等。常用的方法有`find_element_by_xxx()`系列方法用于查找元素,`click()`方法用于点击元素,`send_keys()`方法用于输入文本等。 6. 关闭浏览器:使用WebDriver对象的`quit()`方法关闭浏览器。 以上是Selenium的基本使用步骤,你可以根据具体需求进行更多的操作和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值