【发布时间】:2015-04-01 04:16:12
【问题描述】:
我正在尝试在 Postman 中编写一些测试(我正在运行 Postman Jetpacks 打包应用程序,如果它确实重要的话)并且我正面临一些不一致。
我的测试范围是根据一些预定义的值来验证一组图像的大小(宽度和高度)。
场景是这样的:我调用一个返回一些 url 的方法,然后将这些 URL 设置为环境变量,然后验证每张图片的大小。下面是我在 Postman 的“测试”选项卡中使用的代码。
tests["Status code is 200"] = responseCode.code === 200;
var data = JSON.parse(responseBody);
//test that response contains the expected attributes
tests["splash_image_url present"] = data.hasOwnProperty("splash_image_url");
tests["home_image_url present"] = data.hasOwnProperty("home_image_url");
tests["login_image_url present"] = data.hasOwnProperty("login_image_url");
tests["register_image_url present"] = data.hasOwnProperty("register_image_url");
tests["splash_logo_url present"] = data.hasOwnProperty("splash_logo_url");
tests["bar_logo_url present"] = data.hasOwnProperty("bar_logo_url");
//set each image URL as environment variable
postman.setEnvironmentVariable("splash_image_url", data.splash_image_url);
postman.setEnvironmentVariable("home_image_url", data.home_image_url);
postman.setEnvironmentVariable("login_image_url", data.login_image_url);
postman.setEnvironmentVariable("register_image_url", data.register_image_url);
postman.setEnvironmentVariable("splash_logo_url", data.splash_logo_url);
postman.setEnvironmentVariable("bar_logo_url", data.bar_logo_url);
//extract images from each URL
var splash_image_url = document.createElement("img");
splash_image_url.src = environment.splash_image_url;
var home_image_url = document.createElement("img");
home_image_url.src = environment.home_image_url;
var login_image_url = document.createElement("img");
login_image_url.src = environment.login_image_url;
var register_image_url = document.createElement("img");
register_image_url.src = environment.register_image_url;
var splash_logo_url = document.createElement("img");
splash_logo_url.src = environment.splash_logo_url;
var bar_logo_url = document.createElement("img");
bar_logo_url.src = environment.bar_logo_url;
//test the size for each picture
tests["splash_image_url width"] = splash_image_url.width === 640;
tests["splash_image_url height"] = splash_image_url.height === 960;
tests["home_image_url width"] = home_image_url.width === 640;
tests["home_image_url height"] = home_image_url.height === 960;
tests["login_image_url width"] = login_image_url.width === 640;
tests["login_image_url height"] = login_image_url.height === 960;
tests["register_image_url width"] = register_image_url.width === 640;
tests["register_image_url height"] = register_image_url.height === 960;
tests["splash_logo_url width"] = splash_logo_url.width === 310;
tests["splash_logo_url height"] = splash_logo_url.height === 80;
tests["bar_logo_url width"] = bar_logo_url.width === 155;
tests["bar_logo_url height"] = bar_logo_url.height === 40;
问题是有时在运行请求时所有或部分图片大小验证失败。如果我继续一次又一次地手动运行相同的请求,它最终会显示所有通过的测试。这种不一致使测试不可靠。
我错过了什么或做错了什么?有没有办法验证图片大小?
谢谢
【问题讨论】:
-
是否有可能测试失败,因为您得到的是 304 - 未使用空体而不是 200 进行修改?尝试将 CacheControl: no-cache 添加到请求中
-
我发现如下: - postman 的 send no-cache header 选项在设置中默认设置为 on - 响应状态如图邮递员总是 200 OK - 检查访问日志服务器端也确认状态是 200 OK 我想我们可以排除这种可能性。无论如何感谢您的建议。