How to set a request header in behat tests? - header

I am developing a Rest API and testing it with Behat and mink-selenium2-driver (for the first time) . For security purposes, every call needs to contain a apikey in the request header.
My Problem is, i cannot set the header. My test looks like this:
Given I add "X_ApiKey" header equal to "test"
When I send a GET request to "/notice"
Then the response status code should be 200
But I keep getting a 403.
Any solutions?

In selenium it is imposible.
Need to test this on other driver, like guzzle
To my knowledge, selenium driver lead chrome, but not how it is working. Proposition to check of use others drivers like guzzle, where you can set headers is a answer, in my opinion.
No i found you could additionally other option. It's recommended you use a proxy to inject additional headers to the requests generated by the browser.
To do this i found
* http://wiremock.org/

You should use the behatch package which includes a behatch/rest context.
However, the selenium driver should only be used when you specifically need a browser, for javascript for example. In this case, as you are testing a API endpoint, using a browser will only slow you down and not bring any benefit.

It's possible to use Restler, a micro framework which can help with RESTful API testing in Behat. It support behavior Driven API testing using Behat and Guzzle.
Here is the example:
Given that "X_ApiKey" header is set to "test"
When I request "/notice"
Then the response status code should be 200
Here is another example from negotiation-format.feature file:
Scenario: One with more `q` should be selected, q = 1 when not defined
Given that "Accept" header is set to "application/json;q=0.8,application/xml"
When I request "/examples/_003_multiformat/bmi"
Then the response status code should be 200
And the response is XML
And the type is "array"

Related

How do I verify if the API is throwing 200 OK as status code in JMeter

So, I recently started learning JMeter and want to create an API validation script for a website called dummyapi.io where I got an app-id. But, I don't understand how to use this in JMeter. What does it mean to use the app-id in the API request header?
JMeter automatically considers HTTP Status Codes below 400 as successful so if your app returns status 200 it will be treated as passed.
If you want to add an extra layer of check and verify that particular this request returns 200 response code:
Add Response Assertion as a child of the request you want to validate
Configure it as follows:
Field to Test: Response Code
Pattern Matching Rules: Equals
Patterns to Test: 200
Similarly you can add additional pass/fail criteria like:
presence/absence of text in the response body/message/headers/URL
compare a JMeter Variable with anticipated value
check response duration
check response size
etc. see How to Use JMeter Assertions in Three Easy Steps article to learn more about JMeter Assertions concept

How to interact with network tab in chrome using karate DSL when doing web automation

I am writing UI automation script using karate DSL. In this at certain point I need to get value from network call in chrome. I want to interact with one of the webservice call in chrome devtools network tab and get the json response of that webservice.
I need this because I have to extract the value from that particular call and pass it on to the next step in my automation script.
I have seen the question related to sessionStorage(Is there a way of getting a sessionStorage using Karate DSL?) but I wonder how to do the same for network call using script command or any other way?
The first thing I would recommend is don't forget that Karate is an API testing tool at its core. Maybe all you need to do is manually make that call and get the response. You should be able to scrape the HTML and get the host and parameters needed.
That said - there's a new feature (only for Chrome) which is documented here: https://github.com/intuit/karate/tree/develop/karate-core#intercepting-http-requests - and is available in 0.9.6.RC2
It may not directly solve for what you want, but in a Karate mock, you should be able to set a value for use later e.g. by using a Java singleton or writing to a temp-file.
If there is something oddly more specific you need, please contribute code to Karate. Finally, there is an experimental way in which you can actually make raw requests to the Chrome DevTools session: https://github.com/intuit/karate/tree/develop/examples/ui-test#devtools-protocol-tips - it is for advanced users, but maybe you are one :)

How to use a Postman Mock Server

I have followed the guide here to create a postman mock for a postman collection. The mock seem to be successfully created, but I have no idea how to use the mock service.
I've been given a url for the mock, but how do I specify one of my requests? If I issue a GET request to https://{{mockid}}.mock.pstmn.io I get the following response:
{
"error": {
"name": "mockRequestNotFoundError",
"message": "We were unable to find any matching requests for the mock path (i.e. undefined) in your collection."
}
}
According to the same guide mentioned above the following url to "run the mock" https://{{mockId}}.mock.pstmn.io/{{mockPath}} but what exactly is mockPath?
Within my collection I have plenty of folders, and inside one of these folders I have a request with an example response. How do I access this example response through the mock? Thanks for all help in advance!
Here's the Postman Pro API, which doesnt mention a lot more than just creating reading mocks.
I had the same issue seeing an irrelevant error but finally I found the solution. Unfortunately I cannot find a reference in Postman website. But here is my solution:
When you create a Mock server you define your first request (like GET api/v1/about). So the Mock server will be created but even when you obtain your API key and put it in the header of request (as x-api-key) it still returns an error. It doesn't make sense but it turned out that defining the request is not enough. For me it only started returning a response when I added an Example for the request.
So I suggest for each request that you create, also create at least one example. The request you send will be matched with the examples you have created and the matched response will be returned. You can define body, headers and the HTTP status code of the example response..
I have no Pro Postman subscription and it worked for me using my free subscription.
Menu for adding an example or selecting one of them for editing:
UI for defining the example (See body, headers and status) :
How to go back to the request page:
Here is the correct reply I get based on my example:
If you request in the example is a GET on api.domain.com/api/foo then the mockPath is /api/foo and your mock endpoint is a GET call to https://{{mockid}}.mock.pstmn.io/api/foo.
The HTTP request methods and the the pathname as shown in the image below constitute a mock.
For ease of use the mock server is designed to be used on top of collections. The request in the examples is used as is along with response attached to it. The name of the folder or collection is not a part of the pathname and is not factored in anywhere when using a mock. Mocking a collection means mocking all the examples in within your collection. An example is a tuple of request and response.
An optional response status code if specified lets you fetch the appropriate response for the same path. This can be specified with the x-mock-response-code header. So passing x-mock-response-code as 404 will return the example that matches the pathname and has a response with status code of 404.
Currently if there are examples with the same path but different domains, and mock is unable to distinguish between them it will deterministically return the first one.
Also if you have several examples for the same query :
Mock request accept another optional header, x-mock-response-code, which specifies which integer response code your returned response should match. For example, 500 will return only a 500 response. If this header is not provided, the closest match of any response code will be returned.
Optional headers like x-mock-response-name or x-mock-response-id allow you to further specify the exact response you want by the name or by the uid of the saved example respectively.
Here's the documentation for more details.
{{mockPath}} is simply the path for your request. You should start by adding an example for any of your requests.
Example:
Request: https://www.google.com/path/to/my/api
After adding your mock server, you can access your examples at:
https://{{mockId}}.mock.pstmn.io/path/to/my/api

JMeter - injecting variables into a HTTP Request

I'm trying to work with JMeter to test some web services. So far so good, but I was wondering if you could do the following -
I make a http POST request to create a resource, and if successful the response comes back with the location of the resource in the headers. What I would like to do is take the value of this header, and use it in a http GET request to retrieve the resource. Is this possible with JMeter?
Any help is much appreciated
Use the regular expression extractor to extract the header value to a variable by using a regex. Then use the variable like any other variable in your GET request.

Jmeter : How to test a website to render a page regardless of the content

I have a requirement where the site only needs to respond to the user within certain seconds, regardless of the contents.
Now there is a option in Jmeter in HTTP Proxy Server -> URL Patterns to exclude and then to start recording.
Here I can specify gif, css or other content to ignore. However before starting the recording I have to be aware of what are the various contents that are going to be there.
Is there any specific parameter to pass to Jmeter or any other tool which takes care about loading the page only and I can assert the response code of that page and no the other contents of the page are recorded.
Thanks.
Use the standard HTTP Request sampler with DISABLED (not checked) option Retrieve All Embedded Resources from HTML Files (set via sampler's control panel):
"It also lets you control whether or not JMeter parses HTML files for
images and other embedded resources and sends HTTP requests to
retrieve them."
NOTE: You may also define the same setting via HTTP Request Defaults.
NOTE: See also "Response size calculation" in the same HTTP Request article.
Add assertions to your http samplers:
Duration Assertion: to tests if response was received within a defined amount of time;
Response Assertion: to ensure that request was successfull,
e.g.
Response Field to Test = Response Code
Pattern Matching Rules = Equals
Patterns to Test = 200
You want to run test that would ignore resources after certain number of seconds?
I don't understand, what are you trying to accomplish by doing that?
Users will still receive those resources when they request your url, so your tests wont be accurate.
I don't mean any disrespect, but is it possible that you misunderstood the requirements?
I assume that the requirement is to load all the resources in certain number of seconds, not to cut off the ones that fail to fit in that time?