python - Django LiveServerTestCase live server shuts down prematurely - Stack Overflow

I've been trying to run some functional tests (LiveServerTestCase) with Selenium for a Django app.

I've been trying to run some functional tests (LiveServerTestCase) with Selenium for a Django app. For demonstration it's a note taking app. When I'm trying to simulate note creation, it requires authentication, so in the setup it involves going into admin dashboard, inserting credentials into the form fields, then submitting.

After that, I programmatically redirect to the page where I am supposed to write notes, but as soon as I click on submit, it induces a forbidden error. It says that the instance running the test is an AnonymousUser, ergo, the submission does not follow through. Do keep in mind that submission is in the end of the testcase, so I can't help but presuppose that the server ends prematurely. Is there a flag or configuration that I'm missing as to why this occurs?

An example code of the testcase:

class NoteTakingTest(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        chrome_options = Options()        
        cls.selenium = WebDriver(options=chrome_options)
        cls.selenium.implicitly_wait(10)

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        return super().tearDownClass()

    def setUp(self):
        super().setUp()        
        user = User.objects.create_superuser(email="[email protected]", password="admin")

        self.selenium.get(f"{self.live_server_url}/admin/login")        
        email = self.selenium.find_element(By.ID, "id_username")
        password = self.selenium.find_element(By.ID, "id_password")
        email.send_keys("[email protected]")
        password.send_keys("admin")
        form = self.selenium.find_element(By.ID, "login-form")
        form.submit()


    def access(self, url):        
        self.selenium.get(self.live_server_url + url)

    def test_pipeline_page_access(self):        
        pipeline_url = reverse("notes")
        self.access(pipeline_url)
        self.assertFalse(self.selenium.title.startswith("404"))
        
        wait = WebDriverWait(self.selenium, 10)

        submit_button = wait.until(EC.visibility_of_element_located((By.ID, f'note-submit-btn')))
        self.selenium.execute_script("arguments[0].scrollIntoView();", submit_button)
        # Clicking on the submit button triggers a POST request to /notes
        submit_button.click()

An example code of the testcase:

class NoteViewSet(viewsets.ModelViewSet):
    """
    A viewset that provides the standard actions
    """
    serializer_class = NoteSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        if not self.request.user.is_authenticated:
            raise PermissionDenied("You are not authorized to create a note.")        
        
        updated_object = serializer.save()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745624728a4636738.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信