'Flask pytest: FAILED tests/test_data_response.py::test_post_data_response - TypeError: Object of type HTTPResponse is not JSON serializable
Flask pytest fails with an error. TypeError: Object of type HTTPResponse is not JSON serializable.
self = <json.encoder.JSONEncoder object at 0x10648f610>, o = <http.client.HTTPResponse object at 0x10849db70>
Default message with the error. Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation.
def default(self, o):
try: iterable = iter(o)
except TypeError: pass
else: return list(iterable) –
THIS LINE IS CAUSING AN ERROR from PYTEST.
search_page=urlopen(url_for('discovery_bp.search_fund', _external=True))
If I pass the hardcoded URL, it works fine but when I pass the route, I get an error TypeError: Object of type HTTPResponse is not JSON serializable
rouets file
@discovery_bp.route('/', methods=['GET', 'POST'])
def search_fund():
"""
Given function creates a text field for search box &
renders on index.html then
retrieves input data from the user, run that data through
function query_fund from data.py to grab the fund
& renders back onto index.html
"""
form = SearchForm()
if form.validate_on_submit():
query = form.search.data.split(" ")
fund_results = query_fund(query, f"{FUND_STORE_API_HOST}/{FUND_ENDPOINT}")
return render_template("search.html", query =query,
fund_results = fund_results,
form=form)
return render_template("search.html", form=form)
Selenium class
@dataclass
class SeleniumElements:
search_page: str
response_page: str
selenium_id: str
selenium_class: str
search_data: str
driver: str
def element_search_max_time(self,sec):
return self.driver.implicitly_wait(sec)
def get_data(self):
self.driver.get(self.search_page)
self.element_search_max_time(30)
to_search = self.driver.find_element(By.ID, self.selenium_id)
to_search.click()
to_search.send_keys(self.search_data)
submit = self.driver.find_element(By.CLASS_NAME,self.selenium_class)
submit.click()
self.element_search_max_time(30)
fund_response = requests.get(self.search_page)
assert fund_response.status_code == 200
self.element_search_max_time(30)
round_store_href_link = self.driver.find_element(
By.XPATH, '//*[@id="main-content"]/div[4]/div/h1/a')
round_store_href_link.click()
self.element_search_max_time(30)
rounds_response = requests.get(self.response_page)
assert rounds_response.status_code == 200
This is the test file & I am trying to use the live service method from pytest so I can grab the route instead of hardcoded URL.
@pytest.mark.usefixtures("selenium_chrome_driver")
@pytest.mark.usefixtures('live_server')
def test_post_data_response():`enter code here`
search_response = SeleniumElements(
search_page=urlopen(url_for('discovery_bp.search_fund', _external=True)),
response_page=RESPONSE_PAGE,
selenium_id=SEARCH_BOX_ID,
selenium_class=SUBMIT_BUTTON_CLASS,
search_data=SEARCH_KEYWORD,
driver =chrome_driver)
search_response.get_data()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
