Rayrun
← Back to Discord Forum

Playwright API

Hi Everyone, I need to create a API test where i am sending Authorization as Basic auth and access token as Password / content-type -> application/json-patch+json / body -> json . In the bellow images when i am doing the same with correct value in Postman i am successfully hitting the API. But i am not able to understand how to get the same result in Playwright? Can anyone pls help me ?

image.png
image.png
image.png

This thread is trying to answer question "How to create an API test using Playwright with Basic auth and access token?"

9 replies
butchmayhew
butchmayhew

I'm 6 articles deep into a guide on using Playwright for API testing, all the information is here for you to learn.

I won't have covered your specific Basic auth scenario, but the pattern I use (due to the application I was testing against) is very similar, using a token

https://playwrightsolutions.com/is-it-possible-to-do-api-testing-with-playwright-the-definitive/

Thanks @butchmayhew I going to have a read. @shublack if you don't find an answer please ask again. I have a framework that deals with multiple token access request, maybe I can help.

butchmayhew
butchmayhew
@nzcarnage: want to share that below, love seeing different impelmentations.

want to share that below, love seeing different impelmentations.

@butchmayhew: Yeah, I am currently working on an API regression suite to allow my E2E framework as well as raw endpoint tester similar to what you have done. This allows our testers to have the freedom to work with both. I will create a private repo to hit some non-sensitive APIs so people can grab some ideas from my implementation. We have several APIs but at times you only want to test one, so adding some project logic as well some GH actions, which also will be helpful to the community. I will let you know when this available for consumption.

Yeah, I am currently working on an API regression suite to allow my E2E framework as well as raw endpoint tester similar to what you have done. This allows our testers to have the freedom to work with both. I will create a private repo to hit some non-sensitive APIs so people can grab some ideas from my implementation. We have several APIs but at times you only want to test one, so adding some project logic as well some GH actions, which also will be helpful to the community. I will let you know when this available for consumption.

@butchmayhew: Hi @butchmayhew Thank you for the link it was infomative

Hi @butchmayhew Thank you for the link it was infomative

@nzcarnage @butchmayhew But i still not able to create the APi Call, Actually the Target is to Call Azure API to create A bug through playwright. When i write this Sample Code with Correct information i am gatting 203 Non-Authoritative Information Error

image.png
@shublack: But When i am doing the same thing using postman i am able to create a Bug. authorization Basic Auth/Body as Json/ Content type -> application/json-patch+json

But When i am doing the same thing using postman i am able to create a Bug. authorization Basic Auth/Body as Json/ Content type -> application/json-patch+json

butchmayhew
butchmayhew

@shublack your original screenshot showed 6 headers in postman (you may not need every single one but may be worth reviewing and including all or some of them in your playwright test if they are required with the application you are interacting with.

One thing that has gotten me in the passed with postman is the "Cookie Jar". Really useful if postman is the only tool you are using, as it will attempt to persist cookies for you automatically, and by default it is on. If you disable the cookie jar (under the request settings) and this will ensure that Postman isn't doing any auth magic behind the scenes.

Another thing you can do in postman is click on the </> code icon on the request and actually review the raw curl request and compare that against what you are sending with playwright.

image.png

@shublack I had an issue and it caught us out a few times. Our token provider requires a encrypted form, so in Insomnia it indicates that it is posting a form vs body. In Playwright you have a similar issue that you will need to POST with a form not a data element. TBH I don't remember if we got a 203 or a 400, was a while ago. Maybe try that. If that is the case, POST, PUT and PATCH have the optional data or form, so I ended up wrapping the intricacies to force the author to be explicit on the request calls, made it a lot easier to debug requests.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.