【问题标题】:How to use protractor to get the response status code and response text?如何使用量角器获取响应状态码和响应文本?
【发布时间】:2014-09-28 01:26:41
【问题描述】:

我正在使用 protractor 进行 e2e 测试。

我想访问一个网址,比如说:

browser.get("http://my.test.com");

并获取 http 状态代码和所有响应正文,但我找不到获取它们的方法。有什么方法可以用吗?

【问题讨论】:

  • 有一个 wdio-intercept-service 库,但我注意到它在 Protractor 上对我不起作用。有办法吗?

标签: protractor


【解决方案1】:

由于it's been resolved that selenium webdriver API won't add it 和 Protractor 依赖 Selenium 与浏览器交互,因此无法获取 http 状态代码。

您需要为此找到解决方法,例如使用 NodeJS,因为 Protractor 在其中运行一个帮助函数,该函数理解承诺,因此 Protractor 在继续之前等待 http get:

// A Protracterized httpGet() promise
function httpGet(siteUrl) {
    var http = require('http');
    var defer = protractor.promise.defer();

    http.get(siteUrl, function(response) {

        var bodyString = '';

        response.setEncoding('utf8');
        
        response.on("data", function(chunk) {
            bodyString += chunk;
        });
        
        response.on('end', function() {
            defer.fulfill({
                statusCode: response.statusCode,
                bodyString: bodyString
            });
        });

    }).on('error', function(e) {
        defer.reject("Got http.get error: " + e.message);
    });

    return defer.promise;
}

it('should return 200 and contain proper body', function() {
    httpGet("http://localhost:80").then(function(result) {
        expect(result.statusCode).toBe(200);
        expect(result.bodyString).toContain('Apache');
    });
});

其他选项可能会根据响应状态代码更改 html 服务器端,如this blog post

<h1 id="web_403">403 Access Denied</h1>

【讨论】:

  • Leo 我认为您添加的链接显示方式不同。还是这样吗?
  • 对不起,我没有时间查看这个旧帖子,如果有更好的最新答案,请填写,如果可行,我会投票:) :)
  • 没关系。确实,如果我找到一种方法,我会通知您并感谢您的回答。这是我将尝试遵循的,因为我只需要一些正文校验和。保重。
  • @LeoGallucci 能否请您在 https 和量角器发布方法方面提供帮助。
猜你喜欢
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 2022-10-13
  • 2019-04-06
  • 2015-04-29
  • 2019-04-11
  • 2016-02-19
  • 2011-07-17
相关资源
最近更新 更多