Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor HTTP GET request

In my protractor tests, I'm trying to hit an endpoint that will reset all data for the currently logged in user. Right now, I'm using plain old javascript to hit that endpoint in a beforeAll, and it's successfully hitting it, however now my tests are executing before the page has refreshed causes them to fail. Here's my current code:

var EC = protractor.ExpectedConditions,
    el = element(by.css('body div.modal-x'));

browser.executeAsyncScript(function() {
    var inj = angular.element(document.body).injector();
    var reHTTP = inj.get('reHTTP');
    var myService = inj.get('myService');
    var someVar = myService.getCurrent().definition.userName;
    console.log(someVar); // works
    reHTTP.get('/app/' + someVar + '/reset').then(function () {
        // console.log('i am here'); // this works
        // alert('Success!'); // this works
        return browser.wait(EC.visibilityOf(el)); // this does not work
    });
});

Problem: I can't use Protractor functions within that executeScript block because I'm in the browser context now. i.e. EC is undefined

Question: How can I properly access/use my ExpectedConditions while also hitting this endpoint (again, I'm currently using this function in a beforeAll block)?

A few things to note:

  • If I don't use browser.executeAsyncScript then angular is undefined and I can't access the services (or I couldn't find another way)
  • Using executeScript (rather than AsyncScript) fails even faster than async script

Any ideas?

like image 729
Gunderson Avatar asked May 03 '26 23:05

Gunderson


1 Answers

The method executeAsyncScript is expecting a callback function to be called. You could call it once the request returns a result:

var EC = protractor.ExpectedConditions,
    el = element(by.css('body div.modal-x'));

browser.executeAsyncScript(function(callback) {
    var inj = angular.element(document.body).injector();
    var reHTTP = inj.get('reHTTP');
    var myService = inj.get('myService');
    var someVar = myService.getCurrent().definition.userName;
    console.log(someVar); // works
    reHTTP.get('/app/' + someVar + '/reset').then(function () {
        callback();
    });
});
browser.wait(EC.visibilityOf(el));
like image 59
Florent B. Avatar answered May 06 '26 13:05

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!