SoapUI作為一款接口測(cè)試工具术瓮,具有極大的靈活性和拓展性粥鞋。它可以通過(guò)安裝插件,拓展其功能北发。Selenium作為一款Web自動(dòng)化測(cè)試插件可以很好的與SoapUI進(jìn)行集成。如果要在SoapUI中模擬用戶(hù)點(diǎn)擊界面的功能喷屋,不借助selenium是無(wú)法完成的琳拨。
一、準(zhǔn)備工作 -?給SoapUI安裝selenium插件
1. 首先屯曹,我們需要下載selenium的網(wǎng)站(https://docs.seleniumhq.org/download/)下載server-standalone版本狱庇,這里以selenium-server-standalone.jar 2.25.0為例。由于官網(wǎng)的下載地址需要經(jīng)過(guò)google網(wǎng)址才能下載到恶耽,這里提供從maven倉(cāng)庫(kù)下載的辦法:進(jìn)入http://www.mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server-standalone密任,找到適合的版本,將maven的配置copy到項(xiàng)目中偷俭,然后等maven下載jar到本地浪讳。找到maven本地緩存的目錄,在里面可以找到selenium的jar包了涌萤。
2.?將這個(gè)jar復(fù)制一份放入"${SoapUI安裝目錄\bin\ext}"淹遵。如果你的SoapUI是默認(rèn)安裝的口猜,則這個(gè)目錄為:C:\Program Files\SmartBear\SoapUI-5.3.0\bin\ext。復(fù)制完之后透揣,重啟SoapUI(如果你在復(fù)制之前已經(jīng)打開(kāi)了SoapUI的話(huà))
二济炎、測(cè)試場(chǎng)景
這里以模擬用戶(hù)在百度輸入關(guān)鍵字“test”進(jìn)行搜索為例。首先我們?cè)陧?xiàng)目里面新建一個(gè)Grovvy?Script淌实,輸入以下腳本冻辩,執(zhí)行之后就可以找到百度第一頁(yè)的所有搜索結(jié)果的聯(lián)接了:
?完整的腳本:
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中打開(kāi)網(wǎng)址
driver.get("https://www.baidu.com");
//log.info(driver.getPageSource());
//找到輸入框,并在框中輸入“test”
WebElement input = driver.findElement(By.name("wd"));
input.sendKeys("test");
//找到搜索按鈕拆祈,并點(diǎn)擊
WebElement button = driver.findElement(By.id("su"));
button.click();
//尋找出左邊顯示搜索結(jié)果的div
//System.out.println(driver.getPageSource());
WebElement container = driver.findElement(By.id("content_left"));
//將所有超鏈接打印出來(lái)
List 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
本文首發(fā)于博客園:在SoapUI中模擬用戶(hù)操作?在博客園似乎有更好的排版效果恨闪。