邮差 - 如何检查图片大小

时间:2015-02-02 08:54:16

标签: image size postman

我正在尝试在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;

问题在于,有时在运行请求时,全部或部分图片大小验证失败。如果我一次又一次地继续手动运行相同的请求,它最终将显示所有传递的测试。这种不一致使得测试不可靠。

我遗失了什么或做错了什么?有没有一种方法来验证图片大小?

由于

1 个答案:

答案 0 :(得分:1)

非常好的问题,是一个有趣的挑战。谢谢!

1。你的问题

测试实际上在您手动运行几次后仍然有效,因为此时图像缓存

这里的主要问题是,在检查与其关联的属性之前,您不是在等待图片实际加载

2。邮递员测试结果

我通过等待图像实际加载并发现......我尝试了一个概念验证......我的测试实际上并没有显示为传递或失败。

这主要是由于测试运行的方式。邮递员在请求的上下文中使用eval(或多或少)。

在Evaluator.js @ 111 中

if (command === "runcode") {
    try {
        var result = eval(code);
        event.source.postMessage({'type': 'test_result', 'result': result, 'scriptType': scriptType}, event.origin);
    }
    catch(e) {
        console.log(e);
        event.source.postMessage({'type': 'test_error', 'errorMessage': e.message, 'scriptType': scriptType}, event.origin);
    }

}

不幸的是,对于我们来说,任何类型的回调逻辑都不会被追溯性地推回到结果链中。

2.1邮递员二次测试结果

似乎向event.source发布一组新结果不会触发一组新结果,但会完全丢弃。

我设法为此找到了解决方法。只需结束脚本:

function postmanJetpacksSupportsOnlyOneResultPerTest()
{
    event.source.postMessage({'type': 'test_result', 'result': tests, 'scriptType': 'test'}, event.origin);
}

throw 'ignore this. Enforcing failure on this test case, real values will come  by manually calling *postmanJetpacksSupportsOnlyOneResultPerTest* when you are done with the test case.';

然后只需在完成所有回调后拨打postmanJetpacksSupportsOnlyOneResultPerTest

3。邮差开发人员

我真的希望你能以某种方式包含承诺的概念。例如:

在测试跑步者中

var defered = new jQuery.Deferred();
tests['before timeout'] = true;
setTimeout(function() {
    tests['on timeout'] = true;
    defered.resolve(tests);
}, 500);
tests['after timeout'] = true;
return defered.promise();

在Evaluator.js @ 113 中:

var result = eval(code);
if (result.promise instanceof Function) {
    result.then(function(result) {
       event.source.postMessage({'type': 'test_result', 'result': result, 'scriptType': scriptType}, event.origin);
    });
} else {
    event.source.postMessage({'type': 'test_result', 'result': result, 'scriptType': scriptType}, event.origin);
}

4。一个例子

// handlers
var waiting = 0;
function errorHandler() {
    waiting--;
    tests[this.image.name + ' load'] = false;

}
function successHandler() {
    waiting--;
    tests[this.image.name + ' load'] = true;
    tests[this.image.name + ' width'] = this.image.width == this.width;
    tests[this.image.name + ' height'] = this.image.height == this.height;
}

// test case kind of
function createImageTest(name, url, width, height)
{
    // create image tag
    var image = document.createElement('img');
    // set the name
    image.name = name;
    // set error handlers
    image.onerror = errorHandler.bind({
        image: image
    });
    image.onload = successHandler.bind({
        image: image,
        width: width,
        height: height
    });
    // finally attach the src
    image.src = url;
    // waiting on either a fail or a load
    waiting++;
}

// the actual test cases
createImageTest('stackexchange logo', 'http://cdn.sstatic.net/stackexchange/img/se-logo.png', 223, 52);
createImageTest('stackoverflow logo', 'http://cdn.sstatic.net/stackoverflow/img/sprites.png', 240, 500);

// wait for all callbacks finished
(function checkFinished(){
    // still images to process
    if (waiting) {
        // check back in another 0.1 seconds
        return setTimeout(checkFinished, 100);
    }

    // ready to send result
    postmanJetpacksSupportsOnlyOneResultPerTest();
})();

// the hack from #2.1
function postmanJetpacksSupportsOnlyOneResultPerTest()
{
    event.source.postMessage({'type': 'test_result', 'result': tests, 'scriptType': 'test'}, event.origin);
}

throw 'ignore this. Enforcing failure on this test case, real values will come from init...';