Hello,
i am testing App via dusk. it has this button which checks validation and fires ajax and gives form if suceeses and error message if fails. To intercept ajax request i am adding variable AppdataLoaded = false and when ajax finishes it will be true using $browser->script. this works but i am having trouble checking AppdataLoaded variable via waitUntil, as per document i should be able to check if AppdataLoaded == true but it does not work. Here is the code,
$browser->type('#search_value',$searchtext);
$test = $browser->script('
var AppdataLoaded = false;
$( document ).ajaxComplete(function() {
AppdataLoaded = true;
$(".subheadingtext").text("loaded");
});
');
$browser->press('Continue');
$browser->waitUntil('AppdataLoaded == true',10);
Facebook\WebDriver\Exception\TimeOutException: Waited 10 seconds for callback. I am not sure if i am understanding docs wrong or there is issue with waituntill. I tried $browser->waitUntil('AppdataLoaded',10); first but it also it did not work.
Can anyone point me what i am missing here. If its not right way to test then any other way to test this ?
Each script() call is a separate execution context, so AppdataLoaded is undefined when you try to access it in waitUntil().
Try this:
$browser->type('#search_value', $searchtext);
$browser->press('Continue');
$browser->waitUntil('!$.active');
Hey @staudenmeir ,
Thanks for the reply.
But i am actually confused what is $.active?
Wow. This is one of those Aha moment. Never heard of $.active. Really awesome solution :) :+1:
I was wondering do you know anything where i can actually intercept ajax response and evalute what to do.
Do you want to intercept the AJAX response in the test?
The actual code fires $.get() request and based on response does diffrent things based on error or success response. I want to get that response so i don't have to work with html as responses vary so much in the old legacy app i am working with.
I'm not sure this is possible and even if it is, it's not really a good approach.
Browser tests are meant to be high-level and should only test user interaction. This is also the reason why you don't have access to the HTTP status code.
the thing is this legacy app we are testing results error in span like,
<span style="color:red;">Not Found</span>
I am not sure how can i get that element. as by default it is present like,
<span style="color:red;"></span>
and when error happens it adds "Not found". I want to test this case using dusk. for success i am able to find the div with id but for error how do i check element as it does not have id or class.
I can use assertsee but how do i check like in if condition that is bit confusing. i.e. i wish i could test it like,
if($browser->assertsee('Not Found') but this will generate error when sucess response will be there.
Like @staudenmeir said I don't think this is really possible with Dusk. Can you first please try one of the following support channels? If after that you still feel that this should be added, feel free to report back.
So that works for jQuery AJAX calls, what about axios & vue? Check out my axios interceptors in this SO ticket, but it isn't working. It works similar to $.active, but I still have to use pause instead.
Most helpful comment
https://stackoverflow.com/a/25520719/4848587