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"));
}
Tuesday, 7 May 2019
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.
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.
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.
Saturday, 1 December 2018
Open Browser with out downloading driver.exe file || Quick webdriver setup
Dependency:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
Code:
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
OR
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
OR
WebDriverManager.iedriver().version("3.0.0").setup();
driver = new InternetExplorerDriver();
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
Code:
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
OR
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
OR
WebDriverManager.iedriver().version("3.0.0").setup();
driver = new InternetExplorerDriver();
Friday, 12 October 2018
Java + SOAP API
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.3.8</version>
</dependency>
package com.google.soap;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Base64;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class Final {
public static void main(String[] args) throws SOAPException {
String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/ </soapenv:Body></soapenv:Envelope>";
SOAPConnection connection = null;
String authorization = Base64.getEncoder()
.encodeToString(("uname" + ":" + "pwd").getBytes(Charset.forName("UTF-8")));
String endPointURLString = "http://googlec.om/abc";
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
connection = soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage request = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(str.getBytes()));
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("Authorization", "Basic " + authorization);
headers.addHeader("SOAPAction", "getSum");
headers.addHeader("Content-Type", "multipart/related; type=\"text/xml\";");
request.saveChanges();
SOAPMessage response = connection.call(request, endPointURLString);
System.out.println(getResspString(response));
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
connection.close();
}
}
public static String getResspString(SOAPMessage response) {
final StringWriter sw = new StringWriter();
try {
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()),
new StreamResult(sw));
} catch (TransformerException e) {
throw new RuntimeException(e);
}
return sw.toString();
}
}
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.3.8</version>
</dependency>
package com.google.soap;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Base64;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class Final {
public static void main(String[] args) throws SOAPException {
String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/ </soapenv:Body></soapenv:Envelope>";
SOAPConnection connection = null;
String authorization = Base64.getEncoder()
.encodeToString(("uname" + ":" + "pwd").getBytes(Charset.forName("UTF-8")));
String endPointURLString = "http://googlec.om/abc";
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
connection = soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage request = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(str.getBytes()));
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("Authorization", "Basic " + authorization);
headers.addHeader("SOAPAction", "getSum");
headers.addHeader("Content-Type", "multipart/related; type=\"text/xml\";");
request.saveChanges();
SOAPMessage response = connection.call(request, endPointURLString);
System.out.println(getResspString(response));
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
connection.close();
}
}
public static String getResspString(SOAPMessage response) {
final StringWriter sw = new StringWriter();
try {
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()),
new StreamResult(sw));
} catch (TransformerException e) {
throw new RuntimeException(e);
}
return sw.toString();
}
}
Thursday, 11 October 2018
JAVA + SOAP API CALL
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
public static void main2(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);
String strXMLFilename = "C:\\Users\\KKasthuri\\Documents\\req.xml";
File input = new File(strXMLFilename);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
String url = "http://abc.com/xyx/proj";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "getmthods");
((EntityEnclosingMethod) method).setRequestEntity(entity);
client.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}
public static void main4(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);
String url = "http://google.com/test";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "MyMethod");
method.setRequestHeader("Content-Type", "text/xml");
((EntityEnclosingMethod) method).setRequestEntity(new StringRequestEntity(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://yieldstar.com/ws/AppExchange/v1\"><soapenv:Header/><soapenv:Body></soapenv:Body></soapenv:Envelope>"));
client.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
public static void main2(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);
String strXMLFilename = "C:\\Users\\KKasthuri\\Documents\\req.xml";
File input = new File(strXMLFilename);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
String url = "http://abc.com/xyx/proj";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "getmthods");
((EntityEnclosingMethod) method).setRequestEntity(entity);
client.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}
public static void main4(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);
String url = "http://google.com/test";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "MyMethod");
method.setRequestHeader("Content-Type", "text/xml");
((EntityEnclosingMethod) method).setRequestEntity(new StringRequestEntity(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://yieldstar.com/ws/AppExchange/v1\"><soapenv:Header/><soapenv:Body></soapenv:Body></soapenv:Envelope>"));
client.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}
Saturday, 22 September 2018
Subscribe to:
Posts (Atom)