java – 如何将参数传递给Rest-Assured
有人可以在这种情况下帮助我:
当我调用这项服务时,http://restcountries.eu/rest/v1/,我得到了几个国家的信息. 但是当我想获得像芬兰这样的特定国家信息时,我会调用网络服务http://restcountries.eu/rest/v1/name/Finland来获取与国家相关的信息. 要自动执行上述方案,如何在Rest-Assured中参数化国家/地区名称?我在下面试过,但没有帮助我. RestAssured.given(). parameters("name","Finland"). when(). get("http://restcountries.eu/rest/v1/"). then(). body("capital",containsString("Helsinki")); 解决方法
正如文档所述:
但在你的情况下,似乎你需要路径参数而不是查询参数. 然后,还有多种方式来传输路径参数. 这里有几个例子 使用pathParam()的示例: // Here the key name 'country' must match the url parameter {country} RestAssured.given() .pathParam("country","Finland") .when() .get("http://restcountries.eu/rest/v1/name/{country}") .then() .body("capital",containsString("Helsinki")); 使用变量的示例: String cty = "Finland"; // Here the name of the variable have no relation with the URL parameter {country} RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/name/{country}",cty) .then() .body("capital",containsString("Helsinki")); 现在,如果您需要调用不同的服务,您还可以像这样参数化“服务”: // Search by name String val = "Finland"; String svc = "name"; RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",svc,val) .then() .body("capital",containsString("Helsinki")); // Search by ISO code (alpha) val = "CH" svc = "alpha" RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",containsString("Bern")); // Search by phone intl code (callingcode) val = "359" svc = "callingcode" RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",containsString("Sofia")); 之后您还可以轻松使用JUnit @RunWith(Parameterized.class)为单元测试提供参数’svc’和’value’. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |