最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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()
发布评论

评论列表(0)

  1. 暂无评论