Can we use API Calls in Selenium Pytest Framework?

Viewed 28

I am trying to automate a test case wherein I first login to a website and then modify a drop down. I have automated the part until where I reach the correct page. The issue is, this drop down option is part of a table. The table has many similar elements. Also a few table tags are used as the first column is locked and others are horizontally scrollable. I am finding it very difficult to locate the correct drop down option to click. When I check in the developer options, the drown modification is actually a POST request. I know we can use API testing with pytest, but is it possible to integrate this within existing selenium framework? Can I create a framework where in test_navigate will navigate me to the necessary page (pure selenium). Then test_modify_dropdown will use api call to send the POST request and modify the option. And then i can continue with further test_three?

All this in pytest by the way.

1 Answers

You should simply be able to use the python requests module. A post request could look like this:

import requests

url = "url/for/your/api"

myobj = {'somekey': 'somevalue'} #json in request payload

x = requests.post(url, json = myobj)

print(x.text) #and/or get whatever data you want from the response.
Related