在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
相關文章
相關標籤/搜索