Postman Sushi Selector 演示 - 基于关键字的测试失败?

Postman Sushi Selector demo - test failing based on keyword?

终于游入 APIs 的水域,我发现了 Postman,天哪,它是一个很棒的游戏规则改变者!他们有一个名为 Sushi Selector 的很酷的演示项目,您可以在其中将多个 API 调用链接在一起,然后 运行 它们作为一个集合来创建一个合并 Google 地理编码功能的应用程序和 Place Search APIs,然后利用 Twitter 发送结果。

他们在 series of videos on YouTube 中也有一个演示,其深度和流程都非常出色。非常直观且易于遵循,并尝试自己完成 运行ch。我没有为 San F运行cisco 构建 Sushi Selector,而是为 New Haven 构建了 Barbecue Finder。但是,为地点搜索提供的测试代码失败了。 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 NearbySearch API 的响应时,它会抛出一个错误,"There was an error evaluating the test script: Reference Error: open_now is not defined."

我上下搜索,并尝试找出不同的方法来尝试获取 '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));

无奈,我把demo Sushi Selector中的keyword参数重新设置为原来的'sushi',测试通过!环境变量选项已成功填充。 然后我尝试在我的版本中使用 'sushi',它也有效!我再次尝试用 'barbecue' 或 'bbq' 替换 'sushi' 并且测试会失败,但它与 'chinese'、'japanese' 和 [=48= 一起工作得很好]

尽管如此,为什么这些 Postman 测试会因为不同的关键字参数而失败?!为什么它只会在 'bbq' 或 'barbecue'

时失败

非常感谢任何帮助。我的头好痛。

注意:我也尝试从我的版本中的 if 语句中删除 'establishment.opening_hours.open_now' 条件,只留下评级比较,测试通过,并填充 'choices' 数组。我将 'establishment.opening_hours.open_now' 状态添加到 'choices' 数组 ("hours" = establishment.opening_hours.open_now) 并且效果很好,根据需要填充 TRUE 和 FALSE。仅在 If 语句中使用关键字 'bbq' 或 'barbecue' 会失败。

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

您可以在初始请求中向 Google 个地点 API 添加可选的 opennow 参数,而不是在收到响应后按此条件过滤搜索结果。根据the Google documentation:

opennow — Returns only those places that are open for business at the time the query is sent. Places that do not specify opening hours in the Google Places database will not be returned if you include this parameter in your query.

我在Postman console中调试的方式: 我用几个不同的关键字尝试了请求,有些抛出了错误,有些则没有。当我在 for 循环中添加 console.log(establishment.opening_hours); 时,我可以看到哪个搜索结果(如果有的话)记录了 establishment.opening_hoursnull 值。检查 JSON 响应中的特定搜索结果后,我发现它没有 opening_hours 属性.