How to continue running a mocha `it` that has already failed? - mocha.js

The following mocha it statement:
it('should do truthy and falsey things', function() {
var val = true;
assert.equal(val, true, "true should be true!");
console.log('state 1:', val);
val != val;
assert.equal(true, false, "should continue running even after failing");
console.log('state 2:', val);
val != val;
assert.equal(false, false, "false should be false!");
});
results in the following log:
state 1: true
1) should do truthy and falsey things
And that's it. Once the second assert fails, the rest of the function doesn't run.
Is it possible to have the rest of the function (in this case, the last three lines), and if so, how?

The most direct way to prevent an assertion failure from ending the test right there would be to catch the exception thrown by the failure and rethrow it later. However, I do not recommend doing this because this creates complications. If more than one test fails, which exception are you going to rethrow? Also, you have to remember to rethrow the exception you caught, otherwise the test will seem to be passing.
Generally I just live with the fact that if I have a test with multiple assertions then the the test will stop on the first failure. In cases where I cannot live with it, I've generally adopted a strategy of recording checks into a structure and then comparing the structure to an expected value at the very end of the test. There are many ways a structure can be constructed. Here's an example:
it('should allow multiple tests', function () {
var expected = {
"something should be true": true,
"something should be false": false,
"forgot this one": 1
};
var actual = {};
var thing_to_test = true;
// First test
actual["something should be true"] = thing_to_test;
// Second test
actual["something should be false"] = thing_to_test;
// We forget the third test.
assert.equal(actual, expected);
});
When I run the test above, I get the following output:
1) should allow multiple tests
0 passing (12ms)
1 failing
1) should allow multiple tests:
AssertionError: { 'something should be true': true,
'something should be false': true } == { 'something should be true': true,
'something should be false': false,
'forgot this one': 1 }
+ expected - actual
{
- "something should be false": true
+ "forgot this one": 1
+ "something should be false": false
"something should be true": true
}
at Context.<anonymous> (test.js:22:12)

Related

How to detect the end_interaction signal using the dialogflow_cx python library?

I am using the DialogFlow CX python library for an agent integration. Once I send a request through detect_intent, I want to be able to check in the response for a flag that determines if the interaction with the agent has ended.
For this, I found a property within the ResponseMessage data type response called end_interaction. I tested this with one of my agents, and effectively once the interaction ends, I can see the end_interaction within the array of response messages:
# inspecting query_result.response_messages
[
text {
text: "Goodbye."
},
end_interaction {
}
]
Now, the problem is that I want to trigger some actions whenever we receive the signal, but since the end_interaction field is being sent empty whenever I try to check it returns me False:
# Both response messages from the example above return False under a bool
bool(query_result.response_messages[0].end_interaction) == False # text message response
bool(query_result.response_messages[1].end_interaction) == False # end_interaction signal
I have tried many things, such as checking with isinstance and hasattr, but they return True for all scenarios since the ResponseMessage data type always has an end_interaction attr.
Any help for finding how to detect and check for this signal is highly appreciated! Thanks!
To detect and check for the end_interaction field, you can use the following in your detect_intent:
any("end_interaction" in d for d in response.query_result.response_messages)
Please note that this will return True if the end_interaction field is found in the response_messages list and False if not. You can use this to determine if the interaction with the agent has ended.
Here is a python sample code of the detect_intent with the end_interaction field for your reference:
def detect_intent_texts(agent, session_id, texts, language_code):
session_path = f'{agent}/sessions/{session_id}'
print(f"Session path: {session_path}\n")
client_options = None
agent_components = AgentsClient.parse_agent_path(agent)
location_id = agent_components["location"]
if location_id != "global":
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
print(f"API Endpoint: {api_endpoint}\n")
client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)
for text in texts:
text_input = session.TextInput(text=text)
query_input = session.QueryInput(text=text_input, language_code=language_code)
request = session.DetectIntentRequest(
session=session_path, query_input=query_input
)
response = session_client.detect_intent(request=request)
print("=" * 20)
print(f"Query text: {response.query_result.text}")
response_messages = [
" ".join(msg.text.text) for msg in response.query_result.response_messages
]
print(f"Response Messages:\n {response.query_result.response_messages}")
print(f'End of interaction: {any("end_interaction" in d for d in response.query_result.response_messages)}')
Here is the result:

Can I get Protractor/Jasmine to hit a breakpoint when an element locator fails to find its target?

Every time a Protractor element locator fails, it prints an error and continues down a horrible path of endless cascading failures in my spec and suite. Every test that follows depends on the element locator finding its element, and depends on the current spec passing.
I would like to keep the web page under test open while I use the console. The goal is to debug the current state of the page and investigate why the element locator may have failed to find its target.
I'm not too concerned about failing the entire suite and exiting on the first spec failure (I've seen other answers on --fail-fast and stopping on first spec failure.) This is not the approach I would like to take. I want to set a breakpoint, and inspect the environment while the page is running.
Maybe there's something like a Jasmine option for doThisOnFailure: () => { debugger }, which would work for me I think.
I really do not like the solution of using a spec reporter to execute during afterEach and check the failed spec count on the Jasmine environment for the entire spec function. I want to immediately know when an element locator has failed and immediately break as soon as it has failed.
Maybe something really gross would work $('element').click().catch(() => { debugger }).
EDIT: Please, note that I am asking about breaking in a spec, not breaking at the end of the spec.
it('should execute deadly code', function () {
p.navigation.openStorageConfigTab()
$$('.bad-selector').get(0).click() /* IMPORTANT: I want to break here */
p.volume.navigateTo()
})
it('should not execute this spec', function () {
$$('.bad-selector').get(0).click()
})
And the output
✗ should execute deadly code
- Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, .bad-selector)
✗ should not execute this spec
- Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, .bad-selector)
I can recommend you the approach I use, and I hope you can take it from here
Overall approach is to wait until until you type close/ command in browser url:
await browser.waitForAngularEnabled(false);
await browser.wait(
async () => {
let url = await browser.getCurrentUrl();
return url.includes('close/');
},
5 * 60 * 1000,
'Keep-alive timeout reached, closing the session...'
);
The question is when you want to call it. I use the advantage of onComplete callback function in config file. When it's called, the browser is still available. So once all tests are completed, it doesn't exit for 5 minutes unless I submit close/ to the url field. Obviously that can be conditional, by adding something like if (DEBUG === true)
A downside of this setup is it's called when all tests are completed, and it's possible your spec has navigated away from the page where there was error. So what you can also do is to use advantage of jasmine reporter (if you use jasmine). Roughly, you just need to add this to your onPrepare func:
jasmine.getEnv().addReporter({
jasmineStarted: function(suiteInfo) {},
suiteStarted: function(result) {},
specStarted: function(result) {},
specDone: async function(spec) {
if (spec.status === 'failed') {
await browser.waitForAngularEnabled(false);
await browser.wait(
async () => {
let url = await browser.getCurrentUrl();
return url.includes('close/');
},
5 * 60 * 1000,
'Keep-alive timeout reached, closing the session...'
);
await browser.close();
process.exit(35);
}
},
suiteDone: function(result) {},
jasmineDone: function(result) {},
});
So if any it block has failed status, then it'll stop. BUT, I have not tested it, I'll leave it up to you. And second, I didn't think about what will happen to the rest of queued specs since you're redirected to non existing url close/, but I believe it'll still work for you. Worst case scenario, you can play around and make it continue or close the browser instance, as long as you understood the concept
P.S.
I modified the code to close the browser when you type close/, by adding
await browser.close();
process.exit(35);
I tested this code with the following scenarios:
happy path: all 5 it are successful
first element finder of second it block fails
second element finder of second it block fails
All passed. The code works as expected

Why isKeyInCache (by EHCache) return true when the element of key doesn't exist anymore?

sometimes the method isKeyInCache return true, but the element of key doesn't exists anymore.
Example:
Element element = new Element("test", "hello ehcache");
element.setTimeToLive(60);
element.setEternal(false);
cache.put(element);
if immediately i call get:
Element element = cache.get(key);
return element.getObjectValue();
this works perfectly. but after 60 seconds, the method isKeyInCache return true , but the get method throws NullPointerException. Any idea why?
===
another example:
CacheManager manager = new CacheManager(MainTeste.class.getResourceAsStream("/ehcache.xml"));
Cache cache = manager.getCache("siaep");
System.out.println("exists? " + cache.isKeyInCache("hello"));
Element element = new Element("hello", "hello world");
element.setTimeToLive(10);
cache.put(element);
System.out.println("exists? " + cache.isKeyInCache("hello"));
Thread.sleep(1000 * 11);
System.out.println("exists? " + cache.isKeyInCache("hello"));
output:
exists? false
exists? true
exists? true
As stated in the javadoc, the isKeyInCache method is a pretty inexpensive way of checking whether a key is currently present in the cache. However, it does not perform any status check, so it will return true even if the mapping is currently expired.

How to change the status of Jmeter Result

I created a script in jmeter, few positive cases and few are negative cases.
For Positive Cases - Response Code will come as 200
For Negative Cases - Response Code will come as 412.
As per Jmeter if Response Code 4xx or 5xx will be considered as Fail but in my case i am expecting result as 412 in negative cases and i want to consider that as Pass.
I tried with BeanShell Assertion but i didn't get the expected.
Code is as below:
String ErrorValue = "${ExpectedError}";
if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
Failure = false;
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
Failure = false;
}
else
{
Failure=true;
}
with about code i am able to check the expected error and response is same but if that is same how to change the status to pass i didn't get.
Please anyone help me.
Thanks
Sarada
Your Failure = false bit sets only Beanshell Assertion success. As far as I understand you need to change status of the parent sampler. In order to do so you need to invoke SampleResult.setSuccessful() method and set it to "true" as follows:
SampleResult.setSuccessful(true);
Full code:
String ErrorValue = "${ExpectedError}";
if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else
{
Failure=true;
}
References:
SampleResult class JavaDoc
How to Use JMeter Assertions in 3 Easy Steps
If you are expecting a HTTP Response Code "failure" in JMeter but wish to flag the sample as successful this can be accomplished by a response assertion:
For example:
When validating a DELETE call works, we might want to re-try a GET and validate 404 as expected. Normally JMeter would consider this a failure, but in the context of our test it is not.
Add A Response Assertion to the after-delete GET call.
Apply To: Main Sample
Response Field to Test: Response Code
Check off "Ignore Status"
Pattern Matching Rules: Equals
Pattern to Test: 404
The status of failed or not is always ignored. However, only if the assertion of 404 matches will the request be a success.
For example, if the call returned a 500 jmeter would still ignore the "failed" status, but mark the sample as a failure because 500 != 404.
-Addled

CasperJS test doesn't return after execution

I'm having a problem getting casperjs test to exit after execution, I have to hit CTL-C to exit execution. I'm on OSX 10.7 with casperjs 1.1 devel.
To test this wasn't my code I simply copied this sample from the docs:
var casper = require("casper").create();
function Cow() {
this.mowed = false;
this.moo = function moo() {
this.mowed = true; // mootable state: don't do that at home
return 'moo!';
};
}
casper.test.begin('Cow can moo', 2, function suite(test) {
var cow = new Cow();
test.assertEquals(cow.moo(), 'moo!');
test.assert(cow.mowed);
test.done();
});
And I get the following output
casperjs test cow-test.js
Test file: cow-test.js
# Cow can moo
PASS Subject equals the expected value
PASS Subject is strictly true
I then have to hit CTL-C to stop execution. Am I missing something to stop execution?
Per https://github.com/n1k0/casperjs/issues/593#issuecomment-23062361, the problem was that I had created a casper instance at the top of the file, which the documentation warns you not to do.
The solution was to remove the var casper = require("casper").create(); line.

Resources