dayFive:XML Parsing
This commit is contained in:
parent
ae5c6152ec
commit
84238d7307
34
jsonServer/store.json
Normal file
34
jsonServer/store.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -77,17 +77,6 @@
|
||||
"Software Architecture"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "9",
|
||||
"name": "Ian Clark",
|
||||
"location": "Denver",
|
||||
"phone": "+1-555-4455",
|
||||
"courses": [
|
||||
"C++",
|
||||
"Game Development",
|
||||
"Graphics Programming"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "10",
|
||||
"name": "Jessica Martinez",
|
||||
@ -99,6 +88,17 @@
|
||||
"R Programming"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "9",
|
||||
"courses": [
|
||||
"C++",
|
||||
"Game Development",
|
||||
"Graphics Programming"
|
||||
],
|
||||
"phone": "+1-555-4455",
|
||||
"name": "Leena",
|
||||
"location": "Denver"
|
||||
},
|
||||
{
|
||||
"id": "9",
|
||||
"courses": [
|
||||
|
||||
20
src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java
Normal file
20
src/test/java/zacksolutions/DayFive/ParsingXMLResTest.java
Normal 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();
|
||||
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
31
src/test/java/zacksolutions/DayThree/CookiesDemoTest.java
Normal file
31
src/test/java/zacksolutions/DayThree/CookiesDemoTest.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,10 @@
|
||||
<suite name="All Test Suite" >
|
||||
<test name="RestAssured">
|
||||
<classes>
|
||||
<class name="zacksolutions.DayOne.HttpRequestTest">
|
||||
</class>
|
||||
<class name="zacksolutions.DayOne.HttpRequestTest"/>
|
||||
<class name="zacksolutions.DayTwo.PostReqBodyTest"/>
|
||||
<class name="zacksolutions.DayThree.CookiesDemoTest"/>
|
||||
<class name="zacksolutions.DayThree.PathandQuerryParTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user