【问题标题】:How to call http.post using protractor in typescript如何在打字稿中使用量角器调用 http.post
【发布时间】:2020-02-13 21:46:46
【问题描述】:

我正在尝试在量角器中进行 http 发布。 http.post 的状态处于挂起状态,不返回任何响应。

我在specDone下的onPrepare函数中调用一个方法:

    jasmine.getEnv().addReporter({
      specDone: function(result) {
        if (result.status == "failed") {
          browser.getCapabilities().then(function(caps) {
            var browserName = caps.get("browserName");
            browser.takeScreenshot().then(function(png) {
              var stream = fs.createWriteStream(
                "./reports/screenshots/" +
                  browserName +
                  "-" +
                  result.fullName +
                  ".png"
              );
              stream.write(new Buffer(png, "base64"));
              stream.end();
            });
          });
        }
        new PortalData().PushDataToPortal("");
      }
    });

从onPrepare调用下面的函数,API从body中获取参数。我正在使用 protractor-http-client 包进行 API 调用。

  export class PortalData {
  public PushDataToPortal(result) {
    const http: HttpClient = new HttpClient();
    const LogFilePathInSharedLocation =
      "\\\\10.101.101.11\\temp\\DocStar\\Automation\\TestLogs\\Logs.txt"; 


    http
      .post(
        someurl,
        LogFilePathInSharedLocation,
        { "Content-Type": "application/x-www-form-urlencoded" }
      )
      .then((response: ResponsePromise) => {
        console.log(response);
      });
  }

请指教。谢谢!

【问题讨论】:

  • 尝试从邮递员那里点击那个 Http 帖子。如果它不起作用,请发布您完整的 Http.post 调用格式。
  • 它从邮递员那里工作。我的邮政编码在上面更新。 http .post( someurl, LogFilePathInSharedLocation, { "Content-Type": "application/x-www-form-urlencoded" } ) .then((response: ResponsePromise) => { console.log(response); });
  • 尝试这个问题的解决方案:stackoverflow.com/questions/21689089/…

标签: protractor


【解决方案1】:

我建议你在 beforeAll 中使用 Http 调用而不是 onPrepare。

您可以尝试 superagent 或 supertest npm 模块:

超级代理示例

const superagent = require('superagent');

// callback
superagent
  .post('/api/pet')
  .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
  .set('X-API-Key', 'foobar')
  .set('accept', 'json')
  .end((err, res) => {
    // Calling the end function will send the request
  });

// promise with then/catch
superagent.post('/api/pet').then(console.log).catch(console.error);

// promise with async/await
(async () => {
  try {
    const res = await superagent.post('/api/pet');
    console.log(res);
  } catch (err) {
    console.error(err);
  }
})();

超测示例

const supertest = require('supertest');
const request = supertest(`${baseURL}`);
request.put('/test/sendlocal')
          .send(profileAddressData.createData)
          .set('Content-Type', 'application/json')
          .set('Accept', '*/*')
          .expect(200)
          .end((err, res) => {
                if (err) {
                    console.error('Error: ', err);
                    console.error('Response: ', res);
                }
           });

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2023-03-05
    • 2023-03-28
    • 2019-05-09
    • 2020-08-11
    • 1970-01-01
    相关资源
    最近更新 更多