0

I was trying to implement some testing in Django, and I needed to check the data on the web page with a string, but I couldn't retrieve data from the web page. so, how can I get data(html) from web page?

my tests.py

from rest_framework.test import APITestCase

class TicketTestCase(APITestCase):

    def test_user_details_on_ticket_id(self):

        response = self.client.get(f"/api/ticket/1")
        print(response.content) # this returns empty data in bytes

        # I want to check this
        #self.assertEquals(response.data, "helloworld")
16
  • Why don't you print out response.status_code and make sure it is what you expect. Commented Aug 31, 2020 at 1:06
  • @RedCricket because response.status_code just prints the status code and I want the actual data to verify Commented Aug 31, 2020 at 2:16
  • "data(html)" ... DRF return the data as JSON, What do you mean by HTML here? Commented Aug 31, 2020 at 3:41
  • @ArakkalAbu I didn't knew that it returns the data in JSON. Can you please tell me how can I get the data in JSON? Commented Aug 31, 2020 at 3:47
  • I think the self.assertEquals(response.data "helloworld") will do the job. Have you tried that? Commented Aug 31, 2020 at 3:48

1 Answer 1

1

What you're doing is correct. You can retrieve data from web page by using response.content . The problem is that you're missing a slash at the end of the url, so the url should be

 response = self.client.get(f"/api/ticket/1/")

instead of

 response = self.client.get(f"/api/ticket/1")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.