Tuesday 7 May 2019

Interview | RestAssured | Gpath | Groovy GPath in REST Assured,

Easy way to query Json Response object using - inbuilt feature of Rest Assured - Gpath

API :http://www.test.com/teams/88
Resp:
{
  "id": 88,
  "name": "Selenium"
}


@Test
public void gpath_extractSingleValue_findSingleTeamName() {
Response response = RestAssured.get("teams/88");
    String teamName = response.path("name");
    System.out.println(teamName);
}

A quick note on the .path() method. How did REST Assured know to use JSONPath, instead of XMLPath? We didn’t specify it! That’s some of the magic of GPath in REST Assured


@Test
public void gpath_extractSingleValue_findSingleTeamName_responseAsString() {
    String responseAsString = get("teams/88").asString();
    String teamName = JsonPath.from(responseAsString).get("name");
    System.out.println(teamName);
}

@Test
public void gpath_extractSingleValue_findSingleTeamName_getEverythingInOneGo() {
    String teamName = get("teams/88").path("name");
    System.out.println(teamName);
}

Other Samples:
Fist Child - response.path("teams.name[0]");
Last Child - response.path("teams.name[-1]");
All Childs -  ArrayList<String> allTeamNames = response.path("teams.name");
All Parents Data - ArrayList<Map<String,?>> allTeamData = response.path("teams");
Query - object.teams.find { it.name == 'Leicester City FC' }

RestAssuredAssertion :

@Test
public void gpath_extractSingleValue_findSingleTeamName_useAssertion() {
    given().
    when().
            get("teams/66").
    then().
            assertThat().
            body("name", equalTo("Manchester United FC"));
}

Interview | How can you solve StaleElementReferenceException

How can you solve StaleElementReferenceException

Answer : ExpectedConditions.refreshed

Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.


public WebElement waitForElementToBeRefreshedAndClickable(WebDriver driver, By by) {
    return new WebDriverWait(driver, 30)
            .until(ExpectedConditions.refreshed(
                    ExpectedConditions.elementToBeClickable(by)));
}

So basically, this is a method that waits until a DOM manipulation is finished on an object.

When the DOM has manipulated, and say after clicking a button, adds a class to that element. If you try to perform an action on said element, it will throw StaleElementReferenceException since now the  WebElement returned now does not represent the updated element.

You'll use refreshed when you expect DOM manipulation to occur, and you want to wait until it's done being manipulated in the DOM.

Interview | Can we have multiple finally blocks

Answer is NO.



try (1)
catch (0 or more)
finally (0 or 1)


- try block must be followed by either catch or finally block.
- When an exception occurs, that exception occurred is handled by catch block associated with it.
- Java catch block is used to handle the Exception. ...
- This ensures that the finally block is executed even if an unexpected exception occurs.