dayFive:XML Parsing

This commit is contained in:
dadgam3er 2024-10-10 11:03:26 -04:00
parent ae5c6152ec
commit 84238d7307
7 changed files with 209 additions and 13 deletions

34
jsonServer/store.json Normal file
View File

@ -0,0 +1,34 @@
{
"store": {
"books": [
{
"Author": "Harper Lee",
"Category": "Fiction",
"SerialNum": "BK001",
"Price": 10.99,
"Name": "To Kill a Mockingbird"
},
{
"Author": "George Orwell",
"Category": "Dystopian",
"SerialNum": "BK002",
"Price": 9.99,
"Name": "1984"
},
{
"Author": "J.R.R. Tolkien",
"Category": "Fantasy",
"SerialNum": "BK003",
"Price": 13.99,
"Name": "The Lord of the Rings"
},
{
"Author": "F. Scott Fitzgerald",
"Category": "Classic",
"SerialNum": "BK004",
"Price": 8.99,
"Name": "The Great Gatsby"
}
]
}
}

View File

@ -77,17 +77,6 @@
"Software Architecture" "Software Architecture"
] ]
}, },
{
"id": "9",
"name": "Ian Clark",
"location": "Denver",
"phone": "+1-555-4455",
"courses": [
"C++",
"Game Development",
"Graphics Programming"
]
},
{ {
"id": "10", "id": "10",
"name": "Jessica Martinez", "name": "Jessica Martinez",
@ -99,6 +88,17 @@
"R Programming" "R Programming"
] ]
}, },
{
"id": "9",
"courses": [
"C++",
"Game Development",
"Graphics Programming"
],
"phone": "+1-555-4455",
"name": "Leena",
"location": "Denver"
},
{ {
"id": "9", "id": "9",
"courses": [ "courses": [

View File

@ -0,0 +1,20 @@
package zacksolutions.DayFive;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class ParsingXMLResTest {
@Test
public void testXMLRes(){
given()
.when()
.get("https://api.nbp.pl/api/exchangerates/tables/a/")
.then()
.statusCode(200)
.header("Content-Type", "application/json; charset=utf-8")
.log().all();
}
}

View File

@ -0,0 +1,93 @@
package zacksolutions.DayFour;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
public class ParsingJSONResDataTest {
@Test
void JSONResData(){
given()
.contentType(ContentType.JSON)
.when()
.get("http://localhost:3000/store/")
.then()
.statusCode(200)
.header("Content-Type", "application/json")
.body("books[2].Name", equalTo("The Lord of the Rings"));
}
@Test
void returnBooks(){
// Create JSON object for the book data
JSONObject data = new JSONObject();
data.put("Author", "George R. R. Martin");
data.put("Category", "Fantasy");
data.put("SerialNum", "D34C");
data.put("Price", 14.99);
data.put("Name", "A Song Of Ice and Fire");
//post the book to the server
given().contentType("text/plain; charset=utf-8")
.body(data.toString())
.when().post("http://localhost:3000/store/books")
.then().statusCode(201)
.body("Price", equalTo(14.99f))
.log().all();
}
@Test
void getTitleJSONOBJECT(){
Response res =
given()
.contentType(ContentType.JSON)
.when()
.get("http://localhost:3000/store/");
JSONObject jsonObject = new JSONObject(res.asString());
int length = jsonObject.getJSONArray("books").length();
boolean status = false;
for (int i = 0; i < length; i++) {
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
//System.out.println("Books number " + i + " is " + title);
if (title.equalsIgnoreCase("1984")){
status = true;
break;
}
}
Assert.assertTrue(status);
/*
List<String> booksTitle = new JsonPath(res.asString()).getList("Name");
for (String title : booksTitle) {
System.out.println(title);
}
*/
}
@Test
void getPrices(){
Response res = given()
.contentType(ContentType.JSON)
.when().get("http://localhost:3000/store/");
JSONObject jsonObject = new JSONObject(res.asString());
int numOfBooks = 0;
for (int i = 0; i < jsonObject.getJSONArray("books").length(); i++) {
String title = jsonObject.getJSONArray("books").getJSONObject(i).get("Name").toString();
double price = jsonObject.getJSONArray("books").getJSONObject(i).getDouble("Price");
if (price >10.00){
System.out.println(title);
numOfBooks ++;
}
}
System.out.println(numOfBooks);
}
}

View File

@ -0,0 +1,31 @@
package zacksolutions.DayThree;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
public class CookiesDemoTest {
//@Test
void getCookies(){
given()
.when().get("https://www.google.com/")
.then().cookie("AEC","").log().all();
}
@Test(priority = 2)
void getCookiesInfo() {
Response res = given()
.when().get("https://www.google.com/");
String cookieVal = res.getCookie("AEC");
System.out.println("The value of AEC cookie is: " + cookieVal);
Map<String,String> cookiesValue = res.getCookies();
for (String key : cookiesValue.keySet()) {
String cookiesVal = res.getCookie(key);
System.out.println( key + " ===> " + cookiesVal);
}
}
}

View File

@ -0,0 +1,16 @@
package zacksolutions.DayThree;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
public class PathandQuerryParTest {
@Test
void testQuerryPath(){
given().pathParams("mypath", "students")
.queryParam("id", 9)
.when()
.get("http://127.0.0.1:3000/{mypath}")
.then().statusCode(200).log().all();
}
}

View File

@ -3,8 +3,10 @@
<suite name="All Test Suite" > <suite name="All Test Suite" >
<test name="RestAssured"> <test name="RestAssured">
<classes> <classes>
<class name="zacksolutions.DayOne.HttpRequestTest"> <class name="zacksolutions.DayOne.HttpRequestTest"/>
</class> <class name="zacksolutions.DayTwo.PostReqBodyTest"/>
<class name="zacksolutions.DayThree.CookiesDemoTest"/>
<class name="zacksolutions.DayThree.PathandQuerryParTest"/>
</classes> </classes>
</test> </test>
</suite> </suite>