简体   繁体   English

量角器HTTP GET请求

[英]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. 现在,我使用普通的旧javascript在beforeAll命中该端点,并且它已成功击中它,但是现在我的测试在页面刷新之前执行导致它们失败。 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. 问题:我不能在executeScript块中使用Protractor函数,因为我现在在浏览器上下文中。 ie EC is undefined EC未定义

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)? 问题:如何在访问此端点的同时正确访问/使用我的ExpectedConditions (同样,我目前在beforeAll块中使用此功能)?

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) 如果我不使用browser.executeAsyncScript那么angular是未定义的,我无法访问服务(或者我找不到另一种方式)
  • Using executeScript (rather than AsyncScript) fails even faster than async script 使用executeScript (而不是AsyncScript)失败甚至比异步脚本更快

Any ideas? 有任何想法吗?

The method executeAsyncScript is expecting a callback function to be called. executeAsyncScript方法期望调用回调函数。 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. 我们在beforeEachafterEach使用rest调用。 Use browser.ignoreSynchronization = true; 使用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; 在您的beforeAll函数开始时打破角度同步,然后在脚本之后,确保重置browser.ignoreSynchronization = false; .

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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