How to define Javascript global function accessible to JMeter WebDriver sampler - jmeter

I am using WebDriver Sampler to test the web application's client-side performance in JMeter.
NOTE: Javascript is used as Groovy was not working. I need to switch to Groovy.
Everything works fine, and page load time is displayed in listeners.
A few common functions are used across the WebDriver Samplers.
Is there a way to define the Javascript functions globally and use them within each WebDriver Samplers?
var pkg = JavaImporter(org.openqa.selenium)
var time = JavaImporter(java.time)
var support_ui = JavaImporter(org.openqa.selenium.support.ui);
var wait = new support_ui.WebDriverWait(WDS.browser, time.Duration.ofSeconds(10));
WDS.sampleResult.sampleStart();
WDS.log.info("Start navigating to new bus incident creation page");
waitAndClick(pkg.By.cssSelector("#New"));
waitAndClick(pkg.By.cssSelector("#Bus_3"));
waitAndClick(pkg.By.cssSelector("#Bus_3_0"));
waitUntilLoadingIsCompleted();
WDS.log.info("Accessed the new bus incident creation page");
WDS.sampleResult.sampleEnd();
function waitUntilLoadingIsCompleted() {
wait.until(support_ui.ExpectedConditions.presenceOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
wait.until(support_ui.ExpectedConditions.invisibilityOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
}
function waitAndClick(element){
wait.until(support_ui.ExpectedConditions.elementToBeClickable(element));
WDS.browser.findElement(element).click()
}

I can only think of storing your functions into a separate .js file and reading them using load() function, this way you will be able to refer the "common" functions without having to copy and paste them again and again.
Going forward I would like to remind that Nashorn scripting engine has been removed in OpenJDK 15 so you won't be able to use javascript engine with newer JVMs so it worth migrating to Groovy as soon as possible, moreover it's recommended scripting option since JMeter 3.1 and it has much better performance comparing to other engines.

This is really the wrong model to test client performance. Use the builkt in tools of your browser, along with the JavaScript profiling tools. If you want larger sample sets, then include a RUM agent in your code and collect all of the w3c Navtiming metrics in your choice of tool (Splunk, Datadog, Dynatrace, Elasticstack,....) for statistical analysis

Related

Jmeter Selinium webdriver - UI load testing, result analysis

I am doing UI load testing using distributed Jmeter Selinium webdriver
I have successfully execute the script and obtain the result.
Even though i have obtained the result with success status of 200 code, But how to confirm that all threads has successfully loaded the web pages including images and thumbnails
is there any assertion to very this or any suggestion pls
Unless there is a error in your script you will always get 200 as the status code. You could use WDS.sampleResult shorthand to set whether it is successful or not conditionally basing on your acceptance criteria
If you need to verify presence of a certain element or it's being displayed you could use WDS.browser.findElement() function and check whether it is displayed
You can also take a screenshot and compare it to the reference one using i.e. OpenCV Java bindings from JSR223 Assertion with Groovy as the language
WebDriver sampler returns the page source code as the response so you can also use Size Assertion to check if the response size is equal or larger than the anticipated one or Response Assertion to check if certain text is present or not
And last but not the lease using real browsers for performance testing is not recommended either by Selenium project or by WebDriver sampler developers
Performance testing using Selenium and WebDriver is generally not advised. Not because it is incapable, but because it is not optimised for the job and you are unlikely to get good results.
Note: It is NOT the intention of this project to replace the HTTP Samplers included in JMeter. Rather it is meant to compliment them by measuring the end user load time.
So I would rather recommend reconsidering your approach and create the main load using JMeter's HTTP Request samplers and use WebDriver Sampler for checking client side metrics like rendering time or JavaScript execution time.

Can JMeter do automation testing like Selenium can do?

i am still new to JMeter and i was assigned to a work that I will need to use JMeter to perform automation testing. The idea is to write script using JMeter and run the script to fill in the forms in the website. I was curious that can JMeter use different data from the database to fill in the form of the website everytime it execute?(unique data for every user)
I have followed this tutorial (https://www.blazemeter.com/blog/fill-forms-and-submit-with-jmeter-made-easy/ ) and it succeed, however, when i try to change the parameter name (to some other names that do not matches the field name found in the inpect mode), it still works. So i was wondering how JMeter knows where to put in the parameter even i have change to a wrong field name?
As per JMeter Project main page:
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time).
Browsers don't do any magic, they execute HTTP Requests, wait for response and render it. JMeter in its turn can execute the same HTTP requests so traffic would be the same, however it will not render the response, but rather measure the time and collect some more metrics.
If you change the names of the inputs in the form most probably the request will be successful, to wit you will get HTTP Status Code below 400 hence JMeter will mark the result as "green", however if you inspect the response using View Results Tree listener you will see that the form is not filled and/or you still at the same page.
If you want to use JMeter for checking the data returned by the application you're testing consider using JMeter Assertions to test presence of expected values, absence of errors, set response time thresholds, etc.
You can automate the form submission or order placement usin JMeter. You can JMeter for API testing as well by adding assertions. But the main purpose of the JMeter is to test the performance of the application. Its not like selenium which performs actions on the browser whereas JMeter sends the request in various protocols to relevant server and can also simulate many users at the same time.
If you want to do extensive automation testing,JMeter isnt the ideal tool for that.
You can use webdriver sampler to run the selenium with jmeter. It requires to configure sampler and browser config which are plugin and can be downloaded using plugin manager.
For more info:-https://www.blazemeter.com/blog/jmeter-webdriver-sampler/
Now, without the plugin it is working on protocol level and not on the frontend as pointed out in the above comments.
So, yes it can depend on which layer you want to work. It can work on frontend like selenium using the webdriver plugin and can submit the form with different data as a direct request to the server without using the frontend/GUI.
Hope this helps.
It depends on what you are trying to automate. If you plan to automate API invocation where there are some pre-requisites like grabbing tokens, cookies, session IDs from the browser, then JMeter can probably be used where existing JMeter capabilities can be leveraged using BeanShell scripting and other plugins.
But if you plan to have a full blown UI automation framework then JMeter might not be an ideal choice.

Getting js objects from browser console and Jmeter

I'm working on a new project, the client is a bank and I need to perform performance tests, I wanted to use jmeter as a main tool, but I found a problem, most of the pages ask for a token, so I need to catch token and set this to the next POST request in order to login and continue through the different page forms, the problem is that this token is generated by a javascript, so is not returned in any GET response body; I can just get it by console's browser, because this token is generated in a javascript object.
I've tried with postProcessor elements as regular expression extractor, JSR223 PostProcessor (I'm not a pro with) and finally with Webdriver Sampler, this last one is working, but I had to import some Java classes, did some modifications, etc; but I this is not an option, because if I perform the test with several virtual users the machine is going to run out of resources.
I was wondering if you have faced with something similar, if so, could you please make me a suggestion?, maybe perform tests with a different tool, framework...
Thanks in advance.
First of all I would recommend looking into the response more attentively as the token might be coming as a Cookie or a Header, if this is the case you should be able to extract it using HTTP Cookie Manager or Regular Expression Extractor correspondingly.
If the token is generated via JavaScript code you can re-implement the logic using Groovy language and use vars.put() function in order to store the generated value into JMeter Variables. If you cannot come up with the relevant Groovy code - update your question with the JavaScript code snippet and most probably we'll be able to help you with converting it into Groovy and exposing to next Sampler(s)

Is it possible to read / write a web page loaded in MS Edge from a separate process

I realize this question isn't exactly up to SO's standards but I'm hoping someone can tell me I'm wasting my time or point me in the correct direction.
I'm wondering if there was a low level API for manipulating a web page loaded in Edge. I'm working on an automation project that needs to be able to parse the DOM, inject elements, get / set input values from a separate process in real time.
I have not been able to find ANYTHING online that suggests this is possible but I'm worried I might not even be looking in the right places.
Thanks!
Shawn, take a look at the Selenium WebDriver for Microsoft Edge. It's normally used as a QA tool, but I see no reason why you couldn't do those things this way.
From the W3C definition of the WebDriver
WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.
Provided is a set of interfaces to discover and manipulate DOM elements in web documents and to control the behavior of a user agent. It is primarily intended to allow web authors to write tests that automate a user agent from a separate controlling process, but may also be used in such a way as to allow in-browser scripts to control a — possibly separate — browser
I believe that represents most of what you want to do. Here are some useful links that will help you get started.
Download link for WebDriver For Edge
W3C Spec
Selenium Components
Dev Guide
Here is some sample C# code nabbed from a MSFT Gist
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System;
namespace EdgeDriverTests
{
public class Program
{
/*
* This assumes you have added MicrosoftWebDriver.exe to your System Path.
* For help on adding an exe to your System Path, please see:
* https://msdn.microsoft.com/en-us/library/office/ee537574(v=office.14).aspx
*/
static void Main(string[] args)
{
/* You can find the latest version of Microsoft WebDriver here:
* https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
*/
var driver = new EdgeDriver();
// Navigate to Bing
driver.Url = "https://www.bing.com/";
// Find the search box and query for webdriver
var element = driver.FindElementById("sb_form_q");
element.SendKeys("webdriver");
element.SendKeys(Keys.Enter);
Console.ReadLine();
driver.Quit();
}
}
}

Wait until option JMeter

I wanted to know if JMeter has a option where you wait until some element disappears.
Example a loading bar only once that has completed or no longer visible then to carry on. (Also being able to monitor the length of time taken)
I have through about writing it as a webdriver test and then running it as a Junit test in JMeter but wanted to know if there is a simpler solution.
Any ideas welcome :)
First of all you need to realize that JMeter is not a browser
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time).
So JMeter doesn't execute any client-side JavaScript, the only way of implementing "wait until" option is using While Controller in order to re-execute the same request again and again until response data will contain (or stop containing) the element you're looking for.
If you need to evaluate client-side JavaScript the only option would be going for Selenium. I would recommend using WebDriver Sampler instead of going for JUnit as this way you won't have to recompile your script for any change, it will be inlined into .jmx
You can use Transaction Controller to monitor the time taken by the whole process and to wait for a change , have a look at this:
http://www.sourcepole.ch/2011/1/4/waiting-for-a-page-change-in-jmeter

Resources