JMeter做UI自動化
不推薦,好彆扭,但可行
外掛安裝
- 搜外掛selenium,安裝
新增config
- 新增執行緒組
- 右鍵執行緒組->新增->配置元件->jp@gc - Chrome Driver Config
- option和proxy不解釋了
新增Sampler
-
右鍵執行緒組->新增->取樣器->jp@gc - WebDriver Sampler
-
script language 選擇:JavaScript(可惜沒有python)
-
介面說明
- Name - for the test that each Web Driver Sampler will have. This is used by the reports.
- Parameters - is optional and allows the reader to inject variables used in the script section.
- Script - allows the scripter to control the Web Driver and capture times and success/failure outcomes
DEMO程式碼及解釋
- 示例程式碼
// 相當於python的from import java的import
var pkg = JavaImporter(org.openqa.selenium)
// Start capturing the sampler timing 開始捕獲取樣
WDS.sampleResult.sampleStart()
// 程式碼塊
WDS.browser.get('http://114.116.2.138:8090/forum.php')
WDS.browser.findElement(pkg.By.id('ls_username')).sendKeys('admin')
// 等價於 WDS.browser.findElement(org.openqa.selenium.By.id('ls_username')).sendKeys('admin')
WDS.browser.findElement(pkg.By.id('ls_password')).sendKeys('123456')
// js語法定義了一個 sleep函式 ,讓你從python平滑過渡到 js
var sleep = function(time) {
var timeOut = new Date().getTime() + parseInt(time, 10);
while(new Date().getTime() <= timeOut) {}
}
WDS.browser.findElement(pkg.By.cssSelector('.pn.vm')).click()
sleep(3000)
// 斷言部分
if(WDS.browser.getCurrentUrl() != 'http://114.116.2.138:8090/forum.php') {
WDS.sampleResult.setSuccessful(false)
WDS.sampleResult.setResponseMessage('Expected url to be XXX')
}
else{
WDS.sampleResult.setSuccessful(true)
}
// Stop the sampler timing 停止取樣
WDS.sampleResult.sampleEnd()
-
WDS就是Web Driver Sampler
-
麻煩的是你可能並沒有自動補齊(好像可以出來,但沒研究),這個物件有哪些屬性方法
-
深入的研究要看
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/package-summary.html
-
比如顯式等待的一個應用
var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui) var wait = new pkg.WebDriverWait(WDS.browser, 5, 0.5) wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.cssSelector('ul.suggestions')))
關於WDS的一些屬性
- WDS.name - is the value provided in the Name field (above).
- WDS.vars - JMeterVariables - e.g.
vars.get("VAR1"); vars.put("VAR2","value"); vars.remove("VAR3"); vars.putObject("OBJ1",new Object());
- WDS.props - JMeterProperties (class
java.util.Properties
) - e.g.
props.get("START.HMS"); props.put("PROP1","1234");
- WDS.ctx - JMeterContext
- WDS.parameters - is the value provided in the Parameters field (above).
- WDS.args - is an array of the strings provided in the Parameters field, but split by the space ' ' character. This allows the scripter to provide a number of strings as input and access each one by position.
- WDS.log - is a Logger instance to allow the scripter to debug their scripts by writing information to the jmeter log file (JMeter provides a GUI for its log entries)
- WDS.browser - is the configured Web Driver browser that the scripter can script and control. There is detailed documentation on this object on the Selenium Javadocs page.
- WDS.sampleResult - is used to log when the timing should start and end. Furthermore, the scripter can set success/failure state on this object, and this SampleResult is then used by the JMeter reporting suite. The JMeter javadocs provide more information on the API of this object