JMeter (2) —— JMeter与WebDriver测试用户登陆以CAS SSO为例(101 Tutorial)
主要内容
- JMeter与WebDriver测试用户登陆以CAS SSO为例
环境与参考
jvm版本: 1.8.0_65
jmeter版本: 2.13
firefox版本: 39.0.3
参考来源:
Leverage your Load Testing using JMeter and Selenium WebDriver
jmeter-plugins.org:FirefoxDriverConfig
jmeter-plugins.org:WebDriverTutorial
jmeter-plugins.org:WebDriverSampler
Using Selenium with JMeter's WebDriver Sampler
准备
请参照JMeter (1) —— JMeter与WebDriver安装与测试(101 Tutorial) 安装好JMeter。
需要注意的是:
- JMeter的版本
- jar依赖冲突
- firefox版本
待测试的CAS环境
架构
配置
请参照以下文章搭建配置好CAS单点登陆的本地环境。
测试
注意:我们这里只是以CAS单点登陆为应用场景进行测试,此测试可以推广到其他的web应用的登陆场景,也可以扩展到更为丰富的流程或场景中。
准备
-
新建测试计划(TestPlan)
-
为测试计划添加线程组(TheadGroup)
-
依次添加
- jp@gc - Firefox Driver Config (Thread Group -> Config Element)
- jp@gc - WebDriver Sampler (Thread Group -> Sampler)
- View Results Tree (Thread Group -> Listender)
- View Results in Table (Thread Group -> Listender)
测试本地的CAS单点登陆环境
-
脚本
try { var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui) WDS.log.info('WDS Name:' + WDS.name) WDS.sampleResult.sampleStart() WDS.browser.navigate().to('https://app2.hoau.com:8423/cas2') WDS.log.info('Browser Title:' + WDS.browser.getTitle()) WDS.log.info('Browser CurrentUrl:' + WDS.browser.getCurrentUrl()) WDS.log.info('Cookie:' + WDS.browser.manage().getCookies()) WDS.log.info('Request Header: ' + WDS.sampleResult.getRequestHeaders()) var what = WDS.browser.findElement(pkg.By.id('username')) what.sendKeys(['test01']) var where = WDS.browser.findElement(pkg.By.id('password')) where.sendKeys(['psw01']) // var button = WDS.browser.findElement(pkg.By.cssSelector('.btn-submit')) var button = WDS.browser.findElement(pkg.By.xpath("//input[@type='submit']")) button.click() var wait = new pkg.WebDriverWait(WDS.browser, 10) wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath("//a[@title='Click here to log out']"))) var results = WDS.browser.findElements(pkg.By.xpath("//a[@title='Click here to log out']")) WDS.log.info('Result: ' + results) if(results.empty) { WDS.sampleResult.successful = false WDS.sampleResult.responseMessage = 'There were no results returned' } var iter = results.iterator() var element = iter.next() WDS.log.info('User: ' + element.getText()) if('test01' != element.getText()) { WDS.sampleResult.successful = false WDS.sampleResult.responseMessage = 'Login Failure' } WDS.sampleResult.sampleEnd() } catch(ex) { WDS.log.error(ex) WDS.sampleResult.successful = false WDS.sampleResult.responseMessage = 'There were no results returned' WDS.sampleResult.sampleEnd() }
-
运行
-
查看“View Results in Table”
-
查看“View Results Tree”
-
需要注意的是,诸如JMeter + WebDriver Plugin或Selenium Grid + Remote WebDriver抑或Sahi Web UI这些方式的测试,都是试图模拟用户从终端(浏览器)与服务器进行真实交互,以上这些方式长于流程、场景,而非服务端的压力(当然也可用于服务端压力测试之目的,这里强调的只是意图),如果要对服务端进行压力测试,我们可以有其他更为适合的方式。
测试cnblogs登陆
我们同样可以cnblogs为目标系统,尝试用JMeter来测试cnblogs的登陆。
-
脚本
try { var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui) WDS.log.info('WDS Name:' + WDS.name) WDS.sampleResult.sampleStart() WDS.browser.navigate().to('http://passport.cnblogs.com/user/signin') WDS.log.info('Browser Title:' + WDS.browser.getTitle()) WDS.log.info('Browser CurrentUrl:' + WDS.browser.getCurrentUrl()) WDS.log.info('Cookie:' + WDS.browser.manage().getCookies()) WDS.log.info('Request Header: ' + WDS.sampleResult.getRequestHeaders()) var what = WDS.browser.findElement(pkg.By.id('input1')) what.sendKeys(['weizhe_2008']) var where = WDS.browser.findElement(pkg.By.id('input2')) where.sendKeys(['********']) var button = WDS.browser.findElement(pkg.By.id('signin')) button.click() var wait = new pkg.WebDriverWait(WDS.browser, 1) // a href="http://home.cnblogs.com/u/richaaaard/" wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath("//a[@href='http://www.cnblogs.com/richaaaard/']"))) var results = WDS.browser.findElements(pkg.By.xpath("//a[@href='http://www.cnblogs.com/richaaaard/']")) WDS.log.info('Result: ' + results) if(results.empty) { WDS.sampleResult.successful = false WDS.sampleResult.responseMessage = 'There were no results returned' } WDS.sampleResult.sampleEnd() } catch(ex) { WDS.log.error(ex) WDS.sampleResult.successful = false WDS.sampleResult.responseMessage = 'There were no results returned' WDS.sampleResult.sampleEnd() }
-
运行
-
结果
问题
-
由于此种测试模式下,我们会使用JMeter配置的Firefox Driver Config组件,同时使用WDS(Web Driver Sampler)Script在JMeter UI环境下测试目标应用,当Thread Group 的循环次数(Loop Count)或线程数(Number of Threads (user))的值大于1时,当前线程关联的浏览器代理实例一直处于生命期,而单点登陆会保留用户的登陆信息,无需反复输入用户名密码,浏览器保持了这些信息,因此后面断言会出现错误,脚本无法反复执行从而达到为“验证用户名密码这个流程”进行压力测试的目的。
经研究,当前版本的JMeter及其使用的Selenium WebDriver Plugin除PhantomJS外,不支持动态修改Session Cookie,而网上多数文章介绍的相关Cookie的API,无法对浏览器Session Cookie进行操作。
WDS.browser.manage().getCookies() WDS.browser.manage().deleteAllCookies()
我们会使用HTTPs录制的方式来解决压力测试的问题。
扩展
为了解WDS 在JMeterUI下javascript脚本的能力,以及涉及到面问题相关的源码。
-
WDS (JMeterPlugins-WebDriver.jar)
com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler
com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverScriptablepackage com.googlecode.jmeter.plugins.webdriver.sampler; import org.apache.jmeter.samplers.SampleResult; import org.apache.log.Logger; import org.openqa.selenium.WebDriver; public final class WebDriverScriptable { private static final String[] EMPTY_ARGS = new String[0]; private String name; private String parameters; private Logger log; private WebDriver browser; private SampleResult sampleResult; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setParameters(String parameters) { this.parameters = parameters; } public String getParameters() { return this.parameters; } public String[] getArgs() { return this.parameters != null ? this.parameters.trim().replaceAll("\s+", " ").split(" ") : EMPTY_ARGS; } public void setLog(Logger log) { this.log = log; } public Logger getLog() { return this.log; } public void setBrowser(WebDriver browser) { this.browser = browser; } public WebDriver getBrowser() { return this.browser; } public void setSampleResult(SampleResult sampleResult) { this.sampleResult = sampleResult; } public SampleResult getSampleResult() { return this.sampleResult; } }
-
WDS.browser (WebDriver - selenium-api-2.47.0.jar)
org.openqa.selenium.WebDriver
org.openqa.selenium.Cookiepackage org.openqa.selenium; import java.net.URL; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.logging.Logs; public abstract interface WebDriver extends SearchContext { public abstract void get(String paramString); public abstract String getCurrentUrl(); public abstract String getTitle(); public abstract List<WebElement> findElements(By paramBy); public abstract WebElement findElement(By paramBy); public abstract String getPageSource(); public abstract void close(); public abstract void quit(); public abstract Set<String> getWindowHandles(); public abstract String getWindowHandle(); public abstract TargetLocator switchTo(); public abstract Navigation navigate(); public abstract Options manage(); @Beta public static abstract interface Window { public abstract void setSize(Dimension paramDimension); public abstract void setPosition(Point paramPoint); public abstract Dimension getSize(); public abstract Point getPosition(); public abstract void maximize(); } public static abstract interface ImeHandler { public abstract List<String> getAvailableEngines(); public abstract String getActiveEngine(); public abstract boolean isActivated(); public abstract void deactivate(); public abstract void activateEngine(String paramString); } public static abstract interface Navigation { public abstract void back(); public abstract void forward(); public abstract void to(String paramString); public abstract void to(URL paramURL); public abstract void refresh(); } public static abstract interface TargetLocator { public abstract WebDriver frame(int paramInt); public abstract WebDriver frame(String paramString); public abstract WebDriver frame(WebElement paramWebElement); public abstract WebDriver parentFrame(); public abstract WebDriver window(String paramString); public abstract WebDriver defaultContent(); public abstract WebElement activeElement(); public abstract Alert alert(); } public static abstract interface Timeouts { public abstract Timeouts implicitlyWait(long paramLong, TimeUnit paramTimeUnit); public abstract Timeouts setScriptTimeout(long paramLong, TimeUnit paramTimeUnit); public abstract Timeouts pageLoadTimeout(long paramLong, TimeUnit paramTimeUnit); } public static abstract interface Options { public abstract void addCookie(Cookie paramCookie); public abstract void deleteCookieNamed(String paramString); public abstract void deleteCookie(Cookie paramCookie); public abstract void deleteAllCookies(); public abstract Set<Cookie> getCookies(); public abstract Cookie getCookieNamed(String paramString); public abstract WebDriver.Timeouts timeouts(); public abstract WebDriver.ImeHandler ime(); @Beta public abstract WebDriver.Window window(); @Beta public abstract Logs logs(); } }
以上要注意内部静态类 Options里面提供的,对Cookie进行操作的API