![]()
Running Selenium from jMeter
Test Automation
Author
lucaxel
Date
2021-02-14 04:24
Views
51118350
Initial Notes
This is my first post so please have mercy. I'm writing this hoping that I will save some time for whoever tries to follow me into the madness of running selenium tests in a headless browser from jMeter in a docker container, thus enabling performance.
I don't like writing stories so I'm keeping this a list/point format so it's easy to understand and find the things I though are useful. You can find the script attached and details on how it works bellow.
Why
- Easy way to deliver tests to CI/CD pipeline integrators
- Flexible as you can config jmx to be run locally or on the CI/CD pipeline, switch easily between them
- Assert vital fields inside the test using selenium directly (no testng or junit required)
- jMeter logging offers clues to what happens inside docker
- Any system that can run java, can run your test
How
- Selenium commands are written in JSR samplers in jMeter
- Pass the browser as object between samplers
- Use jMeter logging to log progress, errors and more
Setup
- Install jMeter and its plugin manager
- Install the Selenium Support plugin
Implement script
- Start the browser depending on some variables/parameters defined in JMeter
if ("true".equals(vars.get("runLocal")))
System.setProperty("webdriver.gecko.driver", vars.get("setLocal"));
else
System.setProperty("webdriver.gecko.driver", vars.get("setMsmt"));
FirefoxBinary firefoxBinary = new FirefoxBinary();
if (vars.get("headless") == "true") firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxOptions);
WebDriverWait wait = new WebDriverWait(driver, 50);
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("${msm_url}"); // Navigate to website
driver.manage().window().maximize();
- Pass the browser as object between sampler:
vars.putObject("driver", driver)
WebDriver driver = vars.getObject("driver");
- Assertions and jMeter logging functionality
boolean asserMailbox = driver.findElement(By.xpath("//a[contains(text(),'Mailbox')]")).isDisplayed();
if (asserMailbox) {
log.info("Mailbox tab is displayed: " + asserMailbox);
} else {
log.info("SeleniumTestInvalidated" + driver.findElement(By.xpath("//a[contains(text(),'Mailbox')]")));
}
Selenium headless quirks and other fun stuff
Ok so this section is what I believe is most helpful and I want to share with you. I encountered a lot of problems in my app (checking the jmx will prove that but these are the most helpful tips I can offer anyone)
- Scrolling options:
Require a JavascriptExecutor to be defined. The objects can be passed between samplers.
import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = vars.getObject("driver");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView({block: 'center'});", driver.findElement(By.xpath("(//li[@id='tui-pin']//i[@class='fas fa-trash mr-1'])[2]")));
js.executeScript("window.scrollTo(0,0)"); //Scroll to top
- Print Screens:
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
}
this.takeSnapShot(driver, vars.get("pic_location")+"HomePage.png") ;
- Forcing clicks when everything else fails:
Sometimes a button would simply not be clickable so as a workaround implement an action so it simulates a mouse click. Requires an action to be defined.
import org.openqa.selenium.interactions.Actions;
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.id("saveSubscriber")), 0, 0).click().build().perform();
- A final check in jMeter
In case a selenium action fails the whole sampler is marked as failure. but test might fail due to your assertions. In case you don't want to search jmeter logs for multiple error strings I added a log in case sampler is marked as failure in a JSR Assertion Sampler added as a child to the JSR Sampler running the selenium scripts.
String StartProcessResponseCode = SampleResult.getResponseCode();
log.info('Assertion Start')
try {
assert StartProcessResponseCode.contains('200') : "SeleniumTestInvalidated: A web element was not found or could not be clicked."
}
catch (AssertionError e) {
log.info(e.getMessage())
}
log.info('Assertion Ended for :' + StartProcessResponseCode)
Total 0
You must be logged in to post a comment.
Total 2,401
- All
- Acceptance Testing
- Agile
- Bug Tracking
- Capture Tool
- Code Review
- Database Testing
- Embed/Mobile Testing
- Functional Test
- GUI
- Usability Testing
- HP
- IBM
- Java Testing
- Microfocus
- Network Testing
- Performance Test
- Security Testing
- Selenium
- Test Automation
- Test Metrics
- Test Data
- Test Case Management
- Unit Testing
- Test Management
- Web Testing
| Number | Title | Author | Date | Votes | Views |
| 2401 |
Running Selenium from jMeter
lucaxel
|
2021.02.14
|
Votes 0
|
Views 51118350
|
lucaxel | 2021.02.14 | 0 | 51118350 |
| 2400 |
ZeuZ: Test automation framework for Web, Mobile, Desktop, API, and Cloud apps
(TestExpert)
|
2021.02.04
|
Votes 0
|
Views 52265379
|
(TestExpert) | 2021.02.04 | 0 | 52265379 |
| 2399 |
Testsigma: Test web, mobile apps, and APIs continuously @ DevOps speed (1)
(TestExpert)
|
2021.02.04
|
Votes 0
|
Views 51335693
|
(TestExpert) | 2021.02.04 | 0 | 51335693 |
| 2398 |
Mobile User Equipment Tester
(TestExpert)
|
2021.02.04
|
Votes 0
|
Views 52084587
|
(TestExpert) | 2021.02.04 | 0 | 52084587 |
| 2397 |
Katalon TestOps OnPremise (KTOP): TestOps Tool
VTB
|
2020.03.23
|
Votes 0
|
Views 52477686
|
VTB | 2020.03.23 | 0 | 52477686 |
| 2396 |
[Paid] Ranorex Webtestit : UI web tests with Selenium or Protractor
ItSeTsQtB
|
2019.08.08
|
Votes 0
|
Views 51984429
|
ItSeTsQtB | 2019.08.08 | 0 | 51984429 |
| 2395 |
[Paid] Zabbix : Monitor any possible performance metrics and incidents in your network
^Software^
|
2019.07.24
|
Votes 0
|
Views 51964675
|
^Software^ | 2019.07.24 | 0 | 51964675 |
| 2394 |
[Paid] NeoLoad 7.0 : Performance Test APIs to Full Applications
IT-Tester
|
2019.07.24
|
Votes 0
|
Views 50780582
|
IT-Tester | 2019.07.24 | 0 | 50780582 |
| 2393 |
[FREE] Cucumber : behavior-driven development (BDD) tool
IT-Tester
|
2019.07.22
|
Votes 0
|
Views 49380276
|
IT-Tester | 2019.07.22 | 0 | 49380276 |
| 2392 |
[Free] HTTrack : Website Testing Tool
IT-Tester
|
2019.07.22
|
Votes 0
|
Views 51526013
|
IT-Tester | 2019.07.22 | 0 | 51526013 |
| 2391 |
Arquillian : Automated integration, functional and acceptance tests for Java
IT-Tester
|
2019.07.08
|
Votes 0
|
Views 51057516
|
IT-Tester | 2019.07.08 | 0 | 51057516 |
| 2390 |
FitNesse : Automated acceptance tests are power tools for fixing a broken requirements process
IT-Tester
|
2019.07.08
|
Votes 0
|
Views 50667741
|
IT-Tester | 2019.07.08 | 0 | 50667741 |
| 2389 |
Hiptest : Continuous Testing Management Platform
IT-Tester
|
2019.06.26
|
Votes 0
|
Views 51158740
|
IT-Tester | 2019.06.26 | 0 | 51158740 |
| 2388 |
[Free] Test Data Generation Tool : DTM Data Generator
IT-Tester
|
2019.06.26
|
Votes 0
|
Views 50236293
|
IT-Tester | 2019.06.26 | 0 | 50236293 |
| 2387 |
[Free] Assertible : API testing tool which concentrates on the automation and reliability
VTB
|
2019.06.21
|
Votes 0
|
Views 51838397
|
VTB | 2019.06.21 | 0 | 51838397 |
| 2386 |
[Free] Rest-Assured : Java Domain-specific language that makes testing REST service
VTB
|
2019.06.21
|
Votes 0
|
Views 51386918
|
VTB | 2019.06.21 | 0 | 51386918 |
| 2385 |
Postman : API Testing Tool (1)
Testersbest
|
2019.06.21
|
Votes 0
|
Views 51426515
|
Testersbest | 2019.06.21 | 0 | 51426515 |
| 2384 |
Robotium: Android UI Testing
Testersbest
|
2019.06.21
|
Votes 1
|
Views 51778357
|
Testersbest | 2019.06.21 | 1 | 51778357 |
| 2383 |
Ranorex : Automation Testing Tool for desktop, web, and mobile applications
Testersbest
|
2019.06.21
|
Votes 0
|
Views 51018504
|
Testersbest | 2019.06.21 | 0 | 51018504 |
| 2382 |
TestingWhiz : Automation Testing Tool for Software, Web, Mobile, Database, Cloud, Web Services and API testing
Testersbest
|
2019.06.21
|
Votes 0
|
Views 51666648
|
Testersbest | 2019.06.21 | 0 | 51666648 |
| 2381 |
Travis CI : Test and Deploy with Confidence
edouwens
|
2018.11.19
|
Votes 0
|
Views 51235391
|
edouwens | 2018.11.19 | 0 | 51235391 |
| 2380 |
QF- Test : automation testing tool that is meant for testing Java and Web GUI application (1)
aiitistqb
|
2018.10.16
|
Votes 0
|
Views 52450804
|
aiitistqb | 2018.10.16 | 0 | 52450804 |
| 2379 |
Wink : Time-based and action-based capture of user actions
aiitistqb
|
2018.10.16
|
Votes 0
|
Views 52036411
|
aiitistqb | 2018.10.16 | 0 | 52036411 |
| 2378 |
Crucible : a web-based application primarily targeting enterprise
aiitistqb
|
2018.10.16
|
Votes 0
|
Views 51861145
|
aiitistqb | 2018.10.16 | 0 | 51861145 |
| 2377 |
Automated Mobile Testing Tools List
aiitistqb
|
2018.10.16
|
Votes 0
|
Views 50808055
|
aiitistqb | 2018.10.16 | 0 | 50808055 |
| 2376 |
SpecFlow : software tool that computer programmers use for testing other software
aiitistqb
|
2018.10.16
|
Votes 0
|
Views 51364412
|
aiitistqb | 2018.10.16 | 0 | 51364412 |
| 2375 |
Ranorex : Test Automation Tools for Complete Awesomeness
Testersbest
|
2018.10.16
|
Votes 0
|
Views 51910826
|
Testersbest | 2018.10.16 | 0 | 51910826 |
| 2374 |
SmartBear : End-to-End Automated Testing for Mobile, Web & Desktop Apps
Testersbest
|
2018.10.16
|
Votes 0
|
Views 50989950
|
Testersbest | 2018.10.16 | 0 | 50989950 |
| 2373 |
Bugdojo : Continuous QA for your web apps
Testersbest
|
2018.10.16
|
Votes 0
|
Views 50744710
|
Testersbest | 2018.10.16 | 0 | 50744710 |
| 2372 |
TEstNg (5)
kg2031
|
2018.02.28
|
Votes 0
|
Views 51510279
|
kg2031 | 2018.02.28 | 0 | 51510279 |
| 2371 |
Locators (1)
kg2031
|
2018.02.28
|
Votes 0
|
Views 51928967
|
kg2031 | 2018.02.28 | 0 | 51928967 |
| 2370 |
Crystal Reports
nk1
|
2018.02.23
|
Votes 0
|
Views 50998537
|
nk1 | 2018.02.23 | 0 | 50998537 |
| 2369 |
Selenium Learning Tutorial (4)
Dwarika
|
2017.08.28
|
Votes 0
|
Views 51329577
|
Dwarika | 2017.08.28 | 0 | 51329577 |
| 2368 |
[Paid] Module for Binary formats
SoftLogica
|
2017.08.11
|
Votes 0
|
Views 52119398
|
SoftLogica | 2017.08.11 | 0 | 52119398 |
| 2367 |
Getting Started with Selenium IDE
VijayShinde
|
2017.06.09
|
Votes 0
|
Views 51811147
|
VijayShinde | 2017.06.09 | 0 | 51811147 |
| 2366 |
UniPi Is A Powerful Board That Can Control Your Smart Home
TLP
|
2017.04.26
|
Votes 0
|
Views 51137449
|
TLP | 2017.04.26 | 0 | 51137449 |
| 2365 |
what is diff between SVN and GITHUB
vivekjog
|
2017.04.24
|
Votes 0
|
Views 52329402
|
vivekjog | 2017.04.24 | 0 | 52329402 |
| 2364 |
Macro to remove trailing spaces (1)
Christo
|
2017.03.29
|
Votes 0
|
Views 51024297
|
Christo | 2017.03.29 | 0 | 51024297 |
| 2363 |
Comparing 2 work sheets (2)
Christo
|
2017.03.29
|
Votes 0
|
Views 51990537
|
Christo | 2017.03.29 | 0 | 51990537 |
| 2362 |
Tool to generate all combinations of test parameters. (3)
Christo
|
2017.03.29
|
Votes 0
|
Views 51699101
|
Christo | 2017.03.29 | 0 | 51699101 |
| 2361 |
Best Testing Tools of 2014 (2)
kleyzit
|
2017.02.24
|
Votes 0
|
Views 50212075
|
kleyzit | 2017.02.24 | 0 | 50212075 |
| 2360 |
JCrawler (1)
Sky Driver
|
2017.01.26
|
Votes 0
|
Views 51361841
|
Sky Driver | 2017.01.26 | 0 | 51361841 |
| 2359 |
fwptt
Sky Driver
|
2017.01.26
|
Votes 0
|
Views 50929319
|
Sky Driver | 2017.01.26 | 0 | 50929319 |
| 2358 |
AppLoader: Performance Testing for Any Application
Sky Driver
|
2017.01.26
|
Votes 0
|
Views 50847699
|
Sky Driver | 2017.01.26 | 0 | 50847699 |
| 2357 |
LoadStorm
loveelok
|
2017.01.20
|
Votes 0
|
Views 52122011
|
loveelok | 2017.01.20 | 0 | 52122011 |
| 2356 |
SiteBlaster
loveelok
|
2017.01.20
|
Votes 0
|
Views 50518456
|
loveelok | 2017.01.20 | 0 | 50518456 |
| 2355 |
Xceptance LoadTest (1)
loveelok
|
2017.01.20
|
Votes 0
|
Views 51386234
|
loveelok | 2017.01.20 | 0 | 51386234 |
| 2354 |
Test Manager (1)
loveelok
|
2017.01.12
|
Votes 0
|
Views 51243034
|
loveelok | 2017.01.12 | 0 | 51243034 |
| 2353 |
Test Environment Toolkit (1)
master15
|
2017.01.06
|
Votes 0
|
Views 52201008
|
master15 | 2017.01.06 | 0 | 52201008 |
| 2352 |
Burpsuite Pro 1.5.01 (1)
master15
|
2016.12.15
|
Votes 0
|
Views 50675333
|
master15 | 2016.12.15 | 0 | 50675333 |