我正在使用 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>更多推荐
如何使用量角器获取响应状态码和响应文本?
发布评论