如何利用selenium来进行自动化页面测试

这是一个测试的话题,同样也是一个开发的话题。现在的web应用免不了需要进行自动化的页面测试,那么selenium是一个不错的选择。selenium是一个自动化测试框架,它拥有IDE和API接口,可以应用于Java, C#. Python, Ruby等语言。用selenium来构建一个自动化的测试程序非常的简单。不过首先你需要熟悉web应用里面的request, response概念,以及XPath的用法。这里我将介绍一下如何利用Junit与selenium来实现自动化页面测试。

1. 下载必要依赖文件selenium-server-standalone-2.25.0.jar, junit-4.7.jar,并将它们放置到工程的lib文件夹下面 (我这里使用Firefox浏览器来作为客户端,所以就不需要下载额外的浏览器执行器,如果你想用IE或是Chrome做客户端,请下载对应的执行器

http://code.google.com/p/selenium/downloads/list

2. 建立一个测试工程,在工程里创建一个测试文件,并添加如下代码:

import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;

import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

@RunWith(BlockJUnit4ClassRunner.class)
public class pickTest extends TestCase {
    protected static Selenium selenium;
    private static WebDriver driver;


    @Before
    public void createAndStartService() throws IOException {
        selenium = new WebDriverBackedSelenium(new FirefoxDriver(), "");
        driver = ((WrapsDriver) selenium).getWrappedDriver();
    }

    @After
    public void createAndStopService() {
        driver.quit();
    }

    @Test
    public void should_open_google_page() throws InterruptedException {
        driver.get("http://www.google.com.hk");
        WebElement searchBox = driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"));
        searchBox.sendKeys("selenium");
        WebElement searchButton = driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[1]"));
        searchButton.click();
        Wait<WebDriver> wait = new WebDriverWait(driver, 30);
        wait.until(visibilityOfElementLocated(By.xpath("//*[@id=\"ab_name\"]/span")));
    }
}

3. 运行这个测试,你将看到firebox浏览器被自动启动,然后会自动的输入selenum并搜索。

这样,一个简单的自动化页面测试就完成了。有的朋友可能不太明白这段代码的含义。上面的代码中我标出了红色和蓝色两部分,我简单解释一下。Selenium是通过对浏览器的包装来进行页面处理的,因此我们首先会创建一个与浏览器相关的WebDriver对象。然后我们需要查找页面元素就是通过findeElement的方法和XPath的方式来获取页面对象(红色部分代码)。那么通常我们的一个点击操作产生服务器相应,这里就需要一些时间。蓝色部分的代码就是创建一个等待对象,你可以通过XPath的方式来确定返回后页面上的哪个元素加载完了就认为页面加载完了,同时等待对象也有一个超时设置,这样即是服务器端一直不返回或出错。我们依然可以结束测试。如何更快的确定页面元素的XPath,如下:

转载于:https://www.cnblogs.com/moonz-wu/archive/2012/09/11/2680087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值