Bare HTTP calls from Protractor tests

后端 未结 3 1866
萌比男神i
萌比男神i 2021-01-06 04:42

My Protractor tests need some data setup which I would like to implement by making a series of POSTs and PUTs to the running server.

So, the question is: How do you

3条回答
  •  清酒与你
    2021-01-06 05:19

    An alternate way that doesn't depend on Angular is manually creating an XMLHttpRequest inside of browser.executeAsyncScript. This is especially helpful if you need to make a call as part of the test setup, prior to Angular loading or prior to navigating to a page at all.

    See this example in the Protractor docs:

    Example #3: Injecting a XMLHttpRequest and waiting for the result. In this example, the inject script is specified with a function literal. When using this format, the function is converted to a string for injection, so it should not reference any symbols not defined in the scope of the page under test.

    driver.executeAsyncScript(function() {
      var callback = arguments[arguments.length - 1];
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "/resource/data.json", true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          callback(xhr.responseText);
        }
      }
      xhr.send('');
    }).then(function(str) {
      console.log(JSON.parse(str)['food']);
    });
    

提交回复
热议问题