Iam using this simple function to get and print out the value at an XML response:
@Test
void validateXMLResponse() {
String book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.title");
System.out.println(book);
}
But iam getting this error in the console:
java.lang.ClassCastException: class io.restassured.internal.path.xml.NodeChildrenImpl cannot be cast to class java.lang.String (io.restassured.internal.path.xml.NodeChildrenImpl is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
Since you have more than book node in your xml, you should use corresponding index to define which book's title you want to get. Like this: "bookstore.book[0].title". Or iterate through index to get all the books titles.
@Test
public void validateXMLResponse() {
String book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().body().path("bookstore.book[0].title");
System.out.println(book);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments