如何使用量角器获取响应状态码和响应文本?

编程入门 行业动态 更新时间:2024-10-27 16:26:04
本文介绍了如何使用量角器获取响应状态码和响应文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm using protractor for e2e testing.

我想访问一个网址,例如:

I want to visit a url, say:

browser.get("my.test");

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

And get the http status code and all the response body text, but I can't find a way to get them. Is there any methods I can use?

推荐答案

获取 http 状态代码是不可能的,因为 已解决 selenium webdriver API 不会添加它 并且 Protractor 依赖 Selenium 与浏览器交互.

Getting the http status code it's not possible since it's been resolved that selenium webdriver API won't add it and Protractor depends on Selenium for interacting with the browser.

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

You'll need to find a workaround for this, e.g. using NodeJS given that Protractor runs inside it with a helper function that understand promises so Protractor waits for the http get before continuing:

// 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("localhost:80").then(function(result) { expect(result.statusCode).toBe(200); expect(result.bodyString).toContain('Apache'); }); });

其他选项可能会根据响应状态代码相应地更改 html 服务器端,如 这篇博文

Other option might be changing the html server side accordingly to the response status code as in this blog post

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

更多推荐

如何使用量角器获取响应状态码和响应文本?

本文发布于:2023-11-23 03:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:量角器   如何使用   文本   状态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!