django selenium测试表单提交和重定向

时间:2017-12-06 21:45:32

标签: django selenium

我试图在django中使用selenium进行测试,但不确定如何测试视图是否会将表单数据放入请求会话并正确地重定向到下一页? 在测试会话值时,我收到以下错误 (可能是因为我正在检查django客户端上的会话值而不是selenium):

错误:

Failure
Traceback (most recent call last):
  File "/Users/yunti/PycharmProjects/switcher5/switcher5/users/tests/test_functional.py", line 48, in test_postcode_signup
    self.assertEqual(postcode, self.client.session.get('postcode'))
AssertionError: 'example_postcode' != None

测试:

class MySeleniumTests(StaticLiveServerTestCase):


    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        options = webdriver.ChromeOptions()
        options.add_argument('headless')
        try:
            cls.selenium = webdriver.Chrome(chrome_options=options)
        except:
            raise Exception("couldn't start headless client")

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


    def test_postcode_signup(self):
        postcode = 'example_postcode'
        self.selenium.get(f"{self.live_server_url}/users/postcode/")
        postcode_input = self.selenium.find_element_by_id('id_postcode')
        postcode_input.send_keys(postcode)
        self.selenium.find_element_by_tag_name('button').click()
        #self.assertRedirect()
        #self.selenium.session? 
        self.assertEqual(postcode,self.client.session.get('postcode'))

0 个答案:

没有答案