I have gotten a task for api automation, where I am asked to use Rest assured java or anything similar in js. I know about mocha framework but the task assessment will be based on how well I understand SOLID principles. With mocha I am not sure if its possible to do that since its bunch of describe an it blocks. Is there any other testing framework which uses classes or have the similar structure to rest assured. TIA.
Videos
» npm install @krisinc/node-rest-assured
» npm install rest-assured
I'd like to create a test suite for REST API I am developing and I'd like to do it in javascript. In addition, I'd like to run these tests as part of CI/CD.
What are the best JS frameworks for this purpose? Can I use Node and Jest or something like that?
I am completely new to this topic and when I did some googling I learned that many people recommend Postman but for some reason I don't like that app. Yes it's good for manual testing while I'm developing my API, but somehow I feel this tool is bloated, it has like million options that distract me. I liked it more when it was just a simple tool and freeware project several years ago, now it's multi-billion company driven by the profit.
Anyway, I don't want to couple my tests and code to the Postman and I'd just like to use some standard testing stack, like node/jest or something like that. Note: I am developer familiar with JS and .NET.
Any suggestions?
I checked your code. The thing is that XmlPath of Restassured isn't Xpath, but uses a property access syntax. If you add a body content to your sample HTML you will see that your XPath doesn't do much. The actual name of the query language is GPath. The following example works, note also the use of CompatibilityMode.HTML, which has the right config for you need:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.jayway.restassured.path.xml.XmlPath;
import com.jayway.restassured.path.xml.XmlPath.CompatibilityMode;
public class HtmlDocumentTest {
@Test
public void titleShouldBeHelloWorld() {
XmlPath doc = new XmlPath(
CompatibilityMode.HTML,
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head><title>Hello world</title></head>"
+ "<body>some body"
+ "<div class=\"content\">wrapped</div>"
+ "<div class=\"content\">wrapped2</div>"
+ "</body></html>");
String title = doc.getString("html.head.title");
String content = doc.getString("html.body.div.find { it.@class == 'content' }");
String content2 = doc.getString("**.findAll { it.@class == 'content' }[1]");
assertEquals("Hello world", title);
assertEquals("wrapped", content);
assertEquals("wrapped2", content2);
}
}
If you're using the DSL (given/when/then) then XmlPath with CompatibilityMode.HTML is used automatically if the response content-type header contains a html compatible media type (such as text/html). For example if /index.html contains the following html page:
<html>
<title>My page</title>
<body>Something</body>
</html>
then you can validate the title and body like this:
when().
get("/index.html").
then().
statusCode(200).
body("html.title", equalTo("My page"),
"html.body", equalTo("Something"));