在SoapUI中模拟用户操做

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

 

1、准备工做 - 给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包了。less

 

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

 

2、测试场景工具

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

 

 完整的脚本:网站

 

 1 import org.openqa.selenium.By;
 2 import org.openqa.selenium.WebElement;
 3 import org.openqa.selenium.firefox.FirefoxDriver;
 4 import org.openqa.selenium.firefox.FirefoxProfile;
 5 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 6 import org.openqa.selenium.ie.InternetExplorerDriver;
 7 import org.openqa.selenium.remote.CapabilityType;
 8 import org.openqa.selenium.remote.DesiredCapabilities;
 9 import org.openqa.selenium.support.ui.ExpectedConditions;
10 import org.openqa.selenium.support.ui.WebDriverWait;
11 
12 def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
13 
14 //初始化HTMLUnitDriver(headless browser)
15 HtmlUnitDriver driver = new HtmlUnitDriver();
16 
17 //在headless browser中打开网址
18 driver.get("https://www.baidu.com");
19 
20 //log.info(driver.getPageSource());
21 
22 //找到输入框,并在框中输入“test”
23 WebElement input = driver.findElement(By.name("wd"));
24 input.sendKeys("test");
25 
26 //找到搜索按钮,并点击
27 WebElement button = driver.findElement(By.id("su"));
28 button.click();
29 
30 
31 //寻找出左边显示搜索结果的div
32 //System.out.println(driver.getPageSource());
33 WebElement container = driver.findElement(By.id("content_left"));
34 
35 //将全部超连接打印出来
36 List<WebElement> list = container.findElements(By.tagName("a"));
37 
38 for (WebElement ele : list) {
39   log.info(ele.getAttribute("href"));
40 }
41 
42 //4. close browser window
43 driver.close();
44 driver.quit();
45 
46 //5. assert
47 assert list.size()>0