问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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

发布网友 发布时间:2022-04-21 01:26

我来回答

2个回答

热心网友 时间:2022-04-06 05:40

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");

        <span style="color: #ff0000;">WebElement searchBox = driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"));</span>

        searchBox.sendKeys("selenium");

        WebElement searchButton = driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[1]"));

        searchButton.click();

        <span style="color: #3366ff;">Wait<WebDriver> wait = new WebDriverWait(driver, 30);

        wait.until(visibilityOfElementLocated(By.xpath("//*[@id=\"ab_name\"]/span")));</span>

    }

}

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


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

热心网友 时间:2022-04-06 06:58

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

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");

        <span style="color: #ff0000;">WebElement searchBox = driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"));</span>

        searchBox.sendKeys("selenium");

        WebElement searchButton = driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[1]"));

        searchButton.click();

        <span style="color: #3366ff;">Wait<WebDriver> wait = new WebDriverWait(driver, 30);

        wait.until(visibilityOfElementLocated(By.xpath("//*[@id=\"ab_name\"]/span")));</span>

    }

}

   

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

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

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
2010 CPA 大马哈鱼子维生素含量 “翔气饶云悲”的出处是哪里 “西顾明月窍”的出处是哪里 “左载王子乔”的出处是哪里 “杯酒慷以慨”的出处是哪里 “纷纷射杀五单于”的出处是哪里 ...的文章 2.根据上题,你最感兴趣的内容是什么? 刚降雪后的街道会显得格外寂静,周围环境中的噪音都跑到哪里去了_百度知... ...的街道,马路,降雪后,显得格外寂静,周围环境中的噪声都上哪去了... selenium+java没有浏览器可以运行吗 软件测试工程师初学者都需要学习了解些什么? 新手,对selenium自动化测试具体流程不太明白, Selenium的功能 怎样开始用selenium进行自动化测试 如何理解java selenium关键字驱动的原理 梦到水里有鱼,就是捉不到 梦见大鱼在水水里,想抓缺抓不住,是什么意思 梦到好多鱼和泥鳅在水里游,但是没抓 梦见鱼在水里游抓不住 梦到发大水自己在拼命的逃跑 经常梦见很多鱼,水少鱼多,捉都捉不完 梦见鱼在池塘浅水游想抓住,抓不住,只能抓住小鱼但是鱼多很漂亮 梦见好多鱼,却抓不到 梦见河流并且看见很多鱼然后没抓到 梦见我在河里游,河里有很多大鱼 触手可及但没抓 梦见池塘里有好多大鱼在游是什么意思?有什么预兆? 梦见好多鱼在水里游,想捉一条花鱼,但是捉不到 梦见好多鱼但抓不住 如何查询招商银行信用卡消费明细 event.widget.winfo_name后面的winfo_name是什么意思? 如何使用Selenium-Grid 如何辨别nike zoom winfo 5真假? Python中的winfo_windth和winfo_reqwidth的区别是什么? Selenium 和python是啥关系?是否相关参考文档 科隆大学的详细介绍 求解答,软件测试都需要掌握哪些技能? python 中有个winfo_name方法,winfo的英文代表啥意思 零基础小白,自学软件测试可以学会嘛? 404 Not Found 0基础学软件测试,行不行呢? python Tkinter编程问题 python新手代码问题? selenium+python 自动化测试,下面页面中元素如何定位,我试了很多方法都不行 怎么实现无边框,无标题栏,全透明的窗口 如何用Selenium IDE 实现简单的循环和条件语句 新西兰一个小地方Whangarei的介绍 指定的参数已超出有效值的范围。参数名: index python中关于Canvas的问题 python tk怎样调整一个button控件和Entry控件在界面的位置