Jmeter BeanShell Assertion not failing the tests - jmeter

I am learning into Jmeter.
I have a BeanShell Assertion which should make the tests fail (failure is hardcoded into the assertion). But all the tests pass. What am I doing wrong?
My understanding is that if the BSA sets
Faliure = true;
the assertion fails.
But in my case it does not fail.
Please see:
You can see the disabled XPath Assertion on the screenshot which is not fulfilled, if I enable that one, that one does fail the test as I expect.
Update: now I see why it didn't fail the tests: Failure has a typo... Then the question: Why did it even run? Isn't this java? Isn't this an undeclared variable?
Thank you!

You have 2 typos, correct statements are:
Failure = true;
FailureMessage = "Here goes the failure message";
Assertion is successful as the code is fine from Beanshell perspective, in Beanshell you don't need to explicitly define object class. As long as it is valid code - your assertion is successful.
Here is a couple of troubleshooting techniques:
Adding debug(); as a first line of your Beanshell script will trigger debugging output to stdout
By surrounding your code in try/catch block like:
try {
//your code here
}
catch (Throwable ex) {
log.error("Failure", ex);
throw ex;
}
you will have the relevant stacktrace printed to jmeter.log file
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on using Beanshell test elements in JMeter

Related

Jmeter : validating a log viewer result with sampler

I am executing a jp#gc - WebDriver Sampler script in which console - log view i am getting a result in text for example check the below image INFO c.g.j.p.w.s.WebDriverSampler: Result1:Image not present
I need to validate the webdriver sample from that result . Any suggestion pls
If you really want to read jmeter.log file and look for the specific message in there you could do something like:
def log = org.apache.commons.io.FileUtils.readFileToString(new File('jmeter.log'), 'UTF-8')
if (org.apache.commons.lang3.StringUtils.containsIgnoreCase('Banner not present', log)) {
WDS.sampleResult.setSuccessful(false)
WDS.sampleResult.setResponseMessage('Failed to locate message "Banner not present" in the log')
}
where WDS.sampleResult stands for SampleResult implementation and you can amend response code, message, mark the sampler as passed or failed and so on.
however it's better to do it on Groovy variable level, i.e.
if (!k) {
WDS.sampleResult.setSuccessful(false)
}
you can also consider relying on Groovy Truth, there is no need to declare booleans
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

How to assert stdout statements from JMeter logs?

We have a beanshell test that executes a JAR, that in turn, prints the response on stdout; very similar to the process described in this article. However, I'm wondering if there is a way to validate the statements printed in the log as part of the tests without resorting to manual verification.
Forget about Beanshell, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language
Your test design looks very flaky ang vague, however if you cannot think of a better option than capturing stdout you can create your own PrintStream and use System.setOut() function to use your own PrintStream instead of STDOUT. Something like:
Add setUp Thread Group to your Test Plan
Add JSR223 Sampler to the setUp Thread Group
Put the following code into "Script| area:
def baos = new ByteArrayOutputStream()
def ps = new PrintStream(baos)
System.setOut(ps)
props.put('baos', baos)
Once done you can access all the messages which were printed to the STDOUT using the following code:
def stdout = props.get('baos').toString()
Demo:

How to ignore failure/skipping statement in cucumber for next than statements

In cucumber suppose my one than statement is failed then my all than statement is skipped by cucumber for that scenario and it started executing next scenario ... Do anyone have any way to assist cucumber to run next step without skipping all other than statement for that scenario.. do we have any provision for same?
I am using cucumber, maven with java
This is a bad practice. If you have the need for something like this, it only means that your Cucumber scenario is not written properly.
Having said that, if there is a step that is expected to fail but its failure does not imply a failure of the whole scenario, you will have to implement some sort of "failsafe" workaround within your glue code. For example try...catch clause that will acknowledge the failure, perhaps log it but will not fail the scenario due to thrown exception.
Cucumber steps should not be polluted with internal logic.
If a step in a scenario fails, then the entire scenario fails. To do anything else undermines several principles of testing. Once a failure has happened executing the subsequent steps make no sense as we don't have a consistent starting point ( something has already gone wrong)
If you want to run a single scenario and exclude a particular step, just remove it from the scenario.
In this case its up to you to use the tool properly. Cucumber is not going to help you do stupid things with it.
You can either handle it using try - - - catch block or you can use soft assertion
Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement.This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.AssertJ is library providing fluent assertions. It is very similar to Hamcrest which comes by default with JUnit. Along with all the asserts AssertJ provides soft assertions with its SoftAssertions class inside org.assertj.core.api package
Consider the below example:
public class Sample {
#Test
public void test1() {
SoftAssert sa = new SoftAssert();
sa.assertTrue(2 < 1);
System.out.println(“Assertion Failed”);
sa.assertFalse(1 < 2);
System.out.println(“Assertion Failed”);
sa.assertEquals(“Sample”, “Failed”);
System.out.println(“Assertion Failed”);
}
}
Output:
Assertion Failed Assertion Failed Assertion Failed
PASSED: test1
Even now the test PASSED instead of FAILED. The problem here is the test would not fail when an exception is not thrown. In order to achieve the desired result we need to call the assertAll() method at the end of the test which will collate all the exceptions thrown and fail the test if necessary.
Extending the SachinB answer.
We can use assertj to achive same.
We need to use it's lib/dependency as below
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.0</version>
</dependency>
You need to create object of SoftAssertions() which is provide by assetj
package you need to import
import org.assertj.core.api.SoftAssertions;
Example code
public class myclass {
SoftAssertions sa = null;
#Then("^mycucucmberquote$")
public void testCase2() {
sa = new SoftAssertions();
sa.assertThat("a").contains("b");
}
#Then("^mycucucmberquoteLastThen of that scario$")
public void testCase3() {
try {
sa.assertAll();
} catch (Exception e) {
}
}
}
sa.assertAll(); implemented function fails and it will provide the stack trace of failed steps.

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. how can I do it?

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. How can I do it?
If any other solution than beanShell then let me know.
The easiest way is running your JMeter test using Taurus tool as a wrapper, it naturally supports JMeter tests, moreover it provides some nice extensions.
Particular in your case you can use Modifications for Existing Scripts functionality which allows enabling or disabling Test Elements
---
scenarios:
modification_example:
script: /your/jmeter/testplan.jmx
modifications:
disable: # Names of the tree elements to disable
- Response Assertion
- Duration Assertion
I see two ways in addition to previous answers:
1) Wrap assertions into If controllers, then - yes, set a flag var & check at the If block.
2) Run JMeter programmatically through JMeter API - here you'd have programmatic access to each and every element in the TestPlan.
Although that way is documented quite poorly while the API model is far from being clear itself.
UPD: some clues for the way of doing the latter
1) Here's the main reference: http://jmeter.apache.org/api/index.html
2) Instantiate engine and load properties:
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties("/path/to/jmeter.properties");
3) Instantiate SaveService and load your plan (yes, save service is what resposible for that)
SaveService.loadProperties();
File yourplan = new File("/path/to/yourplan.jmx");
HashTree planTree = SaveService.loadTree(yourplan);
4) Here's the point where you can access & work your plan elements, going through the HashTree, retrieving test elements in sub-hashtrees (for elements, see the reference mentioned in p.1) & changing them and/or the test structure (cast to TestElement must be good enough for enabling/disabling).
5) As you got done with it, the rest is straightforward:
jmeter.configure(planTree);
jmeter.run();
That should be pretty much it.
From my knowledge, you can not disable all Assertions in the Test Plan by using BeanShell
The work around is as follows:
Create a variable as processAssertions in User Defined Variable config element.
Keep All Assertions inside If Controllers.
Add condition as ${processAssertions}==true, so Assertions will be evaluated ONLY when you set the processAssertions value to true. Set any value other than true, to make JMeter to ignore Assertions.
Using Bean Shell Assertion:
Pre-condition: create processAssertions (Set to TRUE) in User Defined Variables
import org.apache.jmeter.assertions.ResponseAssertion;
log.info("hello");
try{
ResponseAssertion obj = new ResponseAssertion();
if(${processAssertions}==TRUE) { // value accessed from UDV
log.info("inside if");
obj.Enabled=false;
SampleResult.setSuccessful(true); // set sample result to PASS, set to false to mark it failure.
}
} catch(Exception e) {
}
If Controller - with assertions:
If Controller - without assertions:
References:
https://www.blazemeter.com/blog/how-use-jmeter-assertions-3-easy-steps
http://jmeter.apache.org/usermanual/component_reference.html#assertions

Error while processing a beanshell preprocessor

I am new to the beanshell scripting.So my query might have basic syntactical issue.
I am getting "DocConnectionId" from regular expression extractor which is the number of elements in app screen. I have GetNewReferralId which the variablevalue i want to match with DocConnectionId.
I have written the below code:
int DocConnectionId = Integer.parseInt(vars.get("connectionIDWithDoc_matchNr"));
int GetNewReferralId = Integer.parseInt(vars.get("GetNewReferral"));
for(int i = 1;i<=DocConnectionId;i++)
{
if(GetNewReferralId.equals(vars.get("connectionIDWithDoc_"+i))){
Integer.parseInt(vars.put("ConnectionWithDoc"));
break;
}
}
But I am getting the below error in error log.
jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``int DocConnectionId = Integer.parseInt(vars.get("connectionIDWithDoc_matchNr")); . . . '' : Typed variable declaration : Method Invocation Integer.parseInt
Integer.parseInt(vars.put("ConnectionWithDoc"));
This line is wrong, and is guaranteed to generate a Integer parse exception. vars.put returns void value, so you're effectively trying to parse an integer from void, which will throw an exception.
I cant really tell from your code, but are you trying to store the value of i in variable ConenctionWithDoc? In which case, you should do:
vars.put("ConnectionWithDoc", Integer.toString(i));
Most probably connectionIDWithDoc_matchNr is not defined or you made a mistake in its case.
Could you show you full test plan.
Your code does not make a lot of sense, try elaborating what needs to be done so we could come up with more elegant solution in explanations.
Till that time here is a piece of advice:
If you add debug(); line at the very beginning of your Beanshell script - you'll get extra debug output to STDOUT (console, where you launched JMeter from)
If you surround your code in try/catch block like:
try {
//your code here
}
catch (Throwable ex) {
log.error("Error in script", ex);
}
you'll be able to see more readable and understandable stacktrace in jmeter.log file (usually being generated in the folder, you launch JMeter form)
Familiarize yourself with JMeter API w.r.t. classes you're targeting to use. Copy-pasting from stackoverflow without understanding what does the code do can lead to undefined results
See How to Use BeanShell: JMeter's Favorite Built-in Component article for a little bit more detailed explanations and several real-life examples of using Beanshell in JMeter test scripts.

Resources