在SoapUI中模拟用户操作

本文首发于博客园:在SoapUI中模拟用户点击浏览器 在博客园似乎有更好的排版效果。


SoapUI作为一款接口测试工具,具有极大的灵活性和拓展性。它可以通过安装插件,拓展其功能。Selenium作为一款Web自动化测试插件可以很好的与SoapUI进行集成。如果要在SoapUI中模拟用户点击界面的功能,不借助selenium是无法完成的。

 

一、准备工作 - 给SoapUI安装selenium插件

1. 首先,我们需要下载selenium的网站(https://docs.seleniumhq.org/download/)下载server-standalone版本,这里以selenium-server-standalone.jar 2.25.0为例。由于官网的下载地址需要经过google网址才能下载到,这里提供从maven仓库下载的办法:进入http://www.mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server-standalone,找到适合的版本,将maven的配置copy到项目中,然后等maven下载jar到本地。找到maven本地缓存的目录,在里面可以找到selenium的jar包了。

在SoapUI中模拟用户操作

 

2. 将这个jar复制一份放入"${SoapUI安装目录\bin\ext}"。如果你的SoapUI是默认安装的,则这个目录为:C:\Program Files\SmartBear\SoapUI-5.3.0\bin\ext。复制完之后,重启SoapUI(如果你在复制之前已经打开了SoapUI的话)

 

二、测试场景

这里以模拟用户在百度输入关键字“test”进行搜索为例。首先我们在项目里面新建一个Grovvy Script,输入以下脚本,执行之后就可以找到百度第一页的所有搜索结果的联接了:

在SoapUI中模拟用户操作

 

 完整的脚本:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )


//初始化HTMLUnitDriver(headless browser)
HtmlUnitDriver driver = new HtmlUnitDriver();


//在headless browser中打开网址
driver.get("https://www.baidu.com");


//log.info(driver.getPageSource());


//找到输入框,并在框中输入“test”
WebElement input = driver.findElement(By.name("wd"));
input.sendKeys("test");


//找到搜索按钮,并点击
WebElement button = driver.findElement(By.id("su"));
button.click();




//寻找出左边显示搜索结果的div
//System.out.println(driver.getPageSource());
WebElement container = driver.findElement(By.id("content_left"));


//将所有超链接打印出来
List<WebElement> list = container.findElements(By.tagName("a"));


for (WebElement ele : list) {
log.info(ele.getAttribute("href"));
}


//4. close browser window
driver.close();
driver.quit();


//5. assert
assert list.size()>0