Postman Sushi Selector演示-根据关键字测试失败?

时间:2018-07-17 17:50:21

标签: postman google-geocoding-api postman-collection-runner

最后进入API的领域,我发现了邮递员(Postman),这真是个了不起的游戏改变者!他们有一个很酷的演示项目,名为Sushi Selector,您可以在其中将多个API调用链接在一起,然后将它们作为一个集合运行,以创建一个应用程序,该应用程序合并来自Google Geocoding和Place Search API的功能,然后利用Twitter来发送结果。

他们在series of videos on YouTube中也有一个演示,它们的深度和流程都很出色。非常直观,易于遵循,并尝试自行扩展。我没有为旧金山建造一个寿司选择器,而是为纽黑文建造了烧烤查找器。但是,为位置搜索指定的测试代码失败了。 youtube系列中显示的代码:

let response = pm.response.json();
let choices = [];

for (let i = 0; i < response.results.length; i++) {

    let establishment = response.results[i];

    if (establishment.opening_hours.open_now && establishment.rating >= 4){
        choices.push({
            "name": establishment.name,
            "rating": establishment.rating,
            "vicinity": establishment.vicinity,
         });
    }
}
pm.environment.set("choices", JSON.stringify(choices));

运行GET API调用将带回大约10-12个烧烤关节的响应,包括几个现在都打开的烧烤关节('opening_hours.open_now'= TRUE),并且评分为4或更高,包括第一个结果

{
    "html_attributions": [],
    "results": [                  <--------------------
        {
            "geometry": {
                "location": {
                    "lat": 41.2716997,
                    "lng": -72.9742454
                },
                "viewport": {
                    "northeast": {
                        "lat": 41.27280407989272,
                        "lng": -72.97257402010727
                    },
                    "southwest": {
                        "lat": 41.27010442010728,
                        "lng": -72.97527367989272
                    }
                }
            },
            "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
            "id": "6506ce2dbea6328207adb53545109e024e774e1b",
            "name": "Uncle Willie's Smokehouse BBQ",
            "opening_hours": {                <--------------------------
                "open_now": true               <--------------------------
            },
            "photos": [
                {
                    "height": 750,
                    "html_attributions": [
                        "<a href=\"https://maps.google.com/maps/contrib/109755391798790714147/photos\">A Google User</a>"
                    ],
                    "photo_reference": "CmRaAAAAuG2Sb8o07byUTTa0ifo5Ubuq-KELAfvylsoyYNQi41tr6MVyNGsukvFsofpOuBtYvLQnJJf9vR69ow82dFsuaZwrbLecx1xzqy_ds-1FT1D-Gi0rK2IyyGh_CONyl70DEhCdXE5lhHeDN1AwSBwuNx_6GhQ9iUhCG45T85ZOsc_-HPIwOuIS-Q",
                    "width": 1200
                }
            ],
            "place_id": "ChIJp3q4ux926IkRGembozhnAj0",
            "plus_code": {
                "compound_code": "72CG+M8 West Haven, Connecticut",
                "global_code": "87H972CG+M8"
            },
            "price_level": 2,
            "rating": 4.1,                     <--------------------------
            "reference": "CmRbAAAA62c_yAa_Safw9JWbRYKjcSXdY5D-xV1XfOrT4H21ouB6OoK6d3oiqkemwqH4IYxboQmmAn1wWDGo7GnhluH2ZjQdHslYDlDrIDhXXeYiHwYMW5uEy4DFLSdLIDU7BarUEhC0bTplHAz_OIR_inV2tmqCGhRDByzFCtlu7VOOKzC4atlM4kaU6Q",
            "scope": "GOOGLE",
            "types": [
                "restaurant",
                "food",
                "point_of_interest",
                "establishment"
            ],
            "vicinity": "473 Saw Mill Rd, West Haven"
        },

每次我针对Google附近搜索API的响应运行此命令时,都会引发错误,“评估测试脚本时出错:参考错误:未定义open_now。” < / p>

我进行了上下搜索,试图找出不同的方法来尝试获得“ open_now”键,但无济于事。我找到了适用于Sushi Selector代码的公开版本的测试代码,在放入我的API密钥并将搜索结果调整为New Haven中的BBQ之后,它也失败了,并显示了相同的错误消息!

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

let choices = [];

try {

    let response = pm.response.json();

    // loop through all the results 
    for (let i = 0; i < response.results.length; i++) {

        let establishment = response.results[i];

        // check if each establishment is open now and rated well (you can set your own search criteria here)
        // add the establishments that pass your criteria to the choices array
        if (establishment.opening_hours.open_now && establishment.rating >= 4.0) {
            choices.push({
                "name": establishment.name,
                "rating": establishment.rating,
                "vicinity": establishment.vicinity
            });
        }
    }
}
catch (error) {
    throw error;
}

// set your choices as an environment variable
// remember to wrap JSON.stringify() choices since it's an array
pm.environment.set("choices", JSON.stringify(choices));

无奈的是,我将演示寿司选择器中的关键字参数重置为原始的“寿司”,并通过了测试!环境变量选择已成功填充。 然后,我尝试在我的版本中使用“ sushi”,它也有效!我再次尝试用“烧烤”或“烧烤”代替“寿司”,但测试失败了,但对“中文”,“日本语”和“巴西语”的测试效果很好。

所有这一切,为什么这些Postman测试会因为关键字参数不同而失败?为什么只为“烧烤”或“烧烤”失败

任何帮助将不胜感激。我的头很痛。

注意:我还尝试从我的版本中的if语句中删除“ Establishment.opening_hours.open_now”条件,仅保留评分比较并通过测试,然后填充“ choices”数组。我在“ choices”数组中添加了“ Establishmentation.opening_hours.open_now”状态(“ hours” = Establishment.opening_hours.open_now),并且该方法工作得很好,并根据需要填充了TRUE和FALSE。仅在If语句中使用关键字“ bbq”或“ barbecue”使用它会失败。

1 个答案:

答案 0 :(得分:1)

Google Places API返回至少一个不包含opening_hours属性的搜索结果。

在收到响应后,您可以在初始请求中向Google Places API添加可选的opennow参数,而不是根据此条件过滤搜索结果。根据{{​​3}}:

  

opennow-仅返回那些在   查询发送的时间。没有指定营业时间的地方   如果您在查询中包含此参数,则不会返回Google地方信息数据库。

我在the Google documentation中对其进行调试的方式: 我用几个不同的关键字尝试了该请求,有的抛出错误,有的没有出错。当我在for循环中添加console.log(establishment.opening_hours);时,我可以看到哪个搜索结果(如果有)记录了null的{​​{1}}值。在检查JSON响应中的特定搜索结果后,我可以看到它没有establishment.opening_hours属性。