我正在尝试在电子商务网站上执行一个放心 API E2E 流程,其中包括登录、添加产品,然后为该产品创建订单,但是我无法执行实际创建订单的最后一步对于该特定产品 - 它抱怨“产品 ID 错误”。
这是我的主类的代码
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.specification.RequestSpecification;
import pojo.EcommerceLoginRequest;
import pojo.EcommerceLoginResponse;
import pojo.EcommerceOrderDetail;
import pojo.EcommerceOrders;
import static io.restassured.RestAssured.given;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ECommerceAPITest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Login
RequestSpecification req = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com").setContentType(ContentType.JSON).build();
EcommerceLoginRequest loginRequest = new EcommerceLoginRequest();
loginRequest.setUserEmail("[email protected]");
loginRequest.setUserPassword("786Immy110.1");
RequestSpecification reqLogin = given().log().all().spec(req).body(loginRequest);
EcommerceLoginResponse loginResponse = reqLogin.when().post("/api/ecom/auth/login").then().log().all().extract().response().as(EcommerceLoginResponse.class);
System.out.println(loginResponse.getToken());
String token = loginResponse.getToken();
System.out.println(loginResponse.getuserId());
String userId = loginResponse.getuserId();
//Add Product
RequestSpecification addProductBaseReq = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com")
.addHeader("authorization", token)
.build();
RequestSpecification reqAddProduct = given().log().all().spec(addProductBaseReq).param("productName", "screenshot")
.param("productAddedBy", userId).param("productCategory", "fashion")
.param("productSubCategory", "shirts").param("productPrice", "11500")
.param("productDescription", "Lenova").param("productFor", "men")
.multiPart("productImage", new File("/Users/imtiyaz/Desktop/Screenshot 2023-08-25 at 00.22.26.png")); //multiPart is used when sending multimedia - files, images, video etc.
String addProductResponse = reqAddProduct.when().post("/api/ecom/product/add-product")
.then().log().all().extract().response().asString();
JsonPath js = new JsonPath (addProductResponse);
String productId = js.get("productId");
//Create order for the Added Product
RequestSpecification createOrderBaseReq = new RequestSpecBuilder().setBaseUri("https://rahulshettyacademy.com")
.addHeader("authorization", token).setContentType(ContentType.JSON)
.build();
EcommerceOrderDetail orderDetail = new EcommerceOrderDetail();
orderDetail.setCountry("India");
orderDetail.setProductOrderId(productId);
List<EcommerceOrderDetail>orderDetailList = new ArrayList<EcommerceOrderDetail>();
orderDetailList.add(orderDetail);
EcommerceOrders orders = new EcommerceOrders();
orders.setOrders(orderDetailList);
RequestSpecification createOrderReq = given().log().all().spec(createOrderBaseReq).body(orders);
String responseAddOrder = createOrderReq.when().post("/api/ecom/order/create-order").then().log().all().extract().response().asString();
System.out.println(responseAddOrder);
}
}
正如您在我放置“orderDetail.setProductOrderId(productId);”的行中看到的那样在前面的步骤中将其作为 String 对象放置时,我使用相同的 ProductId 变量。
下面是 EcommerceOrderDetail.java 类(pojo 类)的代码:
package pojo;
public class EcommerceOrderDetail {
String country;
String productOrderId;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProductOrderId() {
return productOrderId;
}
public void setProductOrderId(String productOrderId) {
this.productOrderId = productOrderId;
}
}
“EcommerceOrders.java”类的代码:
package pojo;
import java.util.List;
public class EcommerceOrders {
private List<EcommerceOrderDetail> orders; //The value of this 'orders' is nothing but list because it is an array inside 'orders' and in that array there is the 'Country' and 'productOrderId'
public List<EcommerceOrderDetail> getOrders() {
return orders;
}
public void setOrders(List<EcommerceOrderDetail> orders) {
this.orders = orders;
}
}
上传图片的产品是否已使用此代码添加?你在UI中验证过吗? 可以分享一下错误日志吗?