How to fix the error "async callback wasnt invoked within 5000ms specified by jest.settimeout() using react testing library? - react-testing-library

i have a test like below which works most of the times but sometimes throws error "async callback wasnt invoked within 5000ms timeout specified by jest.settimeout()
test('can delete', async () => {
//some logic to render component
await utils.waitForDomChange();
click icon
await utils.waitForDomChange();
expect(queryAllByTestId('remove')[2]).toBeUndefined();
});
I am not sure what is causing this problem. could someone help me fix this. thanks.

Bu default timeout is set to 5000ms but it can take longer than that to resolve async call. Try setting timeout to 15s by adding this at the top of your script: jest.setTimeout(15000). Note that timeout is set for all tests, so multiple async/await tests will need higher timeout.

Related

Getting "Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined ....""

I'm fairly new to Protractor and JS. I wrote a simple code to add few numbers using an online calculator. Add I am doing by calling a add() function where I am returning a Promise. This Promise I am handling in my it block where it is causing issue and giving above error.
Tried updating the versions but no help
describe("Using Protractor and working", function(){
var resultsExpected = [];
function add(a,b){
return new Promise(function(resolve, reject){
element(by.model("first")).sendKeys(String(a));
element(by.model("second")).sendKeys(String(b));
element(by.id("gobutton")).click();
var len=0;
len = resultsExpected.length;
resultsExpected[len] = {value:String(a+b)};
console.log("Length is: "+resultsExpected.length);
console.log("Item inside: "+resultsExpected[len].value);
resolve();
});
}
it("Addition from calc should give correct result", function(){
browser.get('http://juliemr.github.io/protractor-demo/');
add(2,4).then(function(ff){
expect(element(by.css("h2.ng-binding")).getText()).toEqual("6");
return add(5,3);
}).then(function(ff){
expect(element(by.css("h2.ng-binding")).getText()).toEqual("8");
return add(5,8);
}).then(function(ff){
expect(element(by.css("h2.ng-binding")).getText()).toEqual("13");
})
})
})
Failures:
1) Using Protractor and working Addition from calc should give correct result
Message:
[31m Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"[0m
Stack:
Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"
at runWaitForAngularScript.then (D:\JavaScriptWorkSpace\FirstProtractor\protractor\built\browser.js:463:23)
at ManagedPromise.invokeCallback_ (D:\JavaScriptWorkSpace\FirstProtractor\protractor\node_modules\selenium-webdriver\lib\promise.js:1376:14)
at TaskQueue.execute_ (D:\JavaScriptWorkSpace\FirstProtractor\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14)
at TaskQueue.executeNext_ (D:\JavaScriptWorkSpace\FirstProtractor\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27)
at asyncRun (D:\JavaScriptWorkSpace\FirstProtractor\protractor\node_modules\selenium-webdriver\lib\promise.js:2927:27)
at D:\JavaScriptWorkSpace\FirstProtractor\protractor\node_modules\selenium-webdriver\lib\promise.js:668:7
at process._tickCallback (internal/process/next_tick.js:68:7)Error
at ElementArrayFinder.applyAction_ (D:\JavaScriptWorkSpace\FirstProtractor\protractor\built\element.js:459:27)
at ElementArrayFinder.(anonymous function).args [as getText] (D:\JavaScriptWorkSpace\FirstProtractor\protractor\built\element.js:91:29)
at ElementFinder.(anonymous function).args [as getText] (D:\JavaScriptWorkSpace\FirstProtractor\protractor\built\element.js:831:22)
at D:\JavaScriptWorkSpace\FirstProtractor\WorkinWithDropDown.js:55:45
at process._tickCallback (internal/process/next_tick.js:68:7)
Try the below one
it("Addition from calc should give correct result", async() =>{
await brwoser.waitForAngularEnabled(true);
await browser.get('http://juliemr.github.io/protractor-demo/');
})
});
Hope it helps you
This may not help, because I don't see browser.wait() in your code, but FWIW we spent days debugging this exact error message, and eventually fixed it by updating all browser.wait() calls with an explicit timeouts. E.g.
browser.wait(condition, 5000);
Instead of
browser.wait(condition);
We started by verifying that browser.waitForAngularEnabled() was set to true, which it was (we're testing a pure Angular app with Cucumber/Protrator/Selenium/Chai, no non-angular pages, so no reason to turn off waitForAngular). So no luck there.
Then, after much experimentation, we concluded that browser.wait() doesn't always throw an exception and fail the test when the expected condition never becomes true, UNLESS you provide the timeout argument. Without the timeout, the best case scenario was that the test would hit the global cucumber timeout and we would get an unhelpful generic failure message. The worst case scenario was that execution would occasionally continue to the next feature, while the first feature was timing out in the background. When the first feature finally timed out, it caused the second feature to fail with the cryptic message both angularJS testability and angular testability are undefined. There's no mention of this behavior in the documentation for wait().
We tried increasing the global cucumber timeout and setting a global protractor timeout, but it had no effect. So we did a blanket update to always provide the timeout parameter, and haven't seen the error since.
I suspect that the whole misadventure might be related to our use of Chai instead of Jasmine, since the documentation mentions jasmineNodeOpts.defaultTimeoutInterval, so maybe there's some assumption in the code that we're breaking.

Checking if an element is present in protractor

I have a protractor test that expects a certain panel to be NOT PRESENT after login. My code is below, but every time it is executed, protractor hangs and then fails later on.
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
it('The team overlay page should not be present when another user logs in.', function() {
loginPage.login(user.username, user.password);
expect(element(by.css('div.panel#myPanel')).isPresent()).toBe(false);
});
I also tried using .count() but it also does the same thing. Same error as above.
expect(element.all(by.css('div.panel#myPanel')).count()).toBe(0);
You could try waiting for the element by allowing the browser to fully load with some of the following:
browser.driver.sleep(time in milliseconds)
browser.waitForAngular()
You could increase the timeout interval:
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000
Otherwise:
Make sure your locator via css is working correctly (i.e, test it when the panel should be present), and make sure the webpage you are trying to access supports Angular. My bet is there is something incorrect with the format of your locator, as I don't see what else could be an issue.

Protractor: test loading state

I'm setting up protractor for our web app, and all works fine, except one thing: When someone clicks the "login" button, while the HTTP request is running, the button should have class "loading". However, when I try to test this, protractor waits for the HTTP request to finish before it runs the expectation, which then fails, because the loading class is removed again.
How can I assert that the loading class is added?
describe('Authorization', function() {
it('the site loads', () => {
browser.get('/');
expect(browser.getCurrentUrl()).toBe('http://localhost:8000/#/login');
element(by.model('vm.credentials.username')).sendKeys('username');
element(by.model('vm.credentials.password')).sendKeys('password');
element(by.css('#sign-in')).click();
expect(element(by.css('#sign-in')).getAttribute('class')).toMatch(/\bloading\b/);
});
});
I think I found the solution.
The element() function waits for angular to settle in, but browser.driver.findElement() doesn't. So by changing the assertion line to
expect(browser.driver.findElement(by.css('#sign-in')).getAttribute('class')).toMatch(/\bloading\b/);
the tests now pass
As per your problem, protractor is executing your expect statement along with click() function. Protractor is async and fast so it executes everything that it can and then waits for promise/callback to be returned. Try waiting for the click to happen first and then try to assert the class. Here's how you can do it -
element(by.css('#sign-in')).click().then(function(){
expect(element(by.css('#sign-in')).getAttribute('class')).toMatch(/\bloading\b/);
});
Also if the http request is blocking your execution, then try to wait for the element to be displayed. If the element is displayed then it's as good as your element is verified.
browser.wait(protractor.ExpectedConditions.visibilityOf($('.loading')), 10000)
.then(function(){
expect(true).toBe(true);
});
Hope this helps.

Protractor: how to catch error or timeout on browser.driver.get(url) if remote url is fail to load?

It seems when protractor execute "browser.driver.get(...)" it waits until page is loaded or throw "Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL" message. Remote url is 1 to 10 times fail to load (following by freeze). A workaround for that is to refresh/reload page. Is there any way to implement that behaviour in Protractor? (let say repeat action 4-8 times and then continue)
You should be able to catch errors thrown through the promises api, like this:
browser.driver.get(...).then(function(result) {
// do something when page is found
}.thenCatch(function(error) {
// do something with the error
});

GTK: cancel timeout

GTK allows you to set a timeout with g_timeout_add. Just like g_signal_connect, the g_timeout_add function returns an id representing the timeout. So, is there a way to cancel a timeout using the id? Looking through the documentation, I don't see any way to cancel a timeout event, but I would assume there must be some way, otherwise what is the point of the id value returned by g_timeout_add?
So, is there any way to cancel a timeout event, or is this just something that needs to be handled manually by setting a "cancellation flag" which can be checked within the user-provided timeout handler function?
There are two ways to remove a callback registered through g_timeout_add():
Have the callback function return FALSE,
Call g_source_remove() with the identifier returned by g_timeout_add().