简体   繁体   中英

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. ie 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?

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));

We use a rest call in our beforeEach and afterEach as well. Use browser.ignoreSynchronization = true; at the beginning of your beforeAll function to break out of the angular synchronization, and then after the script, make sure you reset browser.ignoreSynchronization = false; .

For example:

beforeEach(function() {
    browser.ignoreSynchronization = true;
    browser.executeAsyncScript();
    browser.ignoreSynchronization = false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM