簡體   English   中英

如何針對測試數據庫運行Selenium測試?

[英]How can I run Selenium tests against a test database?

我喜歡在提交前運行測試。 我是Selenium的新手,我不了解如何運行測試並且不更改數據庫。

我的本地數據庫有許多相同的問題。

有什么辦法可以讓我運行這些測試,而又沒有將數據庫還原到tearDown上的原始狀態?

from selenium import webdriver  
from django.utils import unittest  
from selenium.webdriver.support.ui import WebDriverWait  

class TestAuthentication(unittest.TestCase):  
    scheme = 'http'  
    host = 'localhost'  
    port = '4444'  


    def setUp(self):  
        self._driver = webdriver.Firefox()  
        self._driver.implicitly_wait(5)  

    def login_as_Bryan(self):  
        self._driver.get('http://localhost:8000/account/signin/')  
        user = self._driver.find_element_by_id('id_username')  
        user.send_keys("Bryan")  
        password = self._driver.find_element_by_id('id_password')  
        password.send_keys('***************')  
        submit = self._driver.find_element_by_id('blogin')  
        submit.click()  

    def test_user_should_be_able_to_login_manually(self):  
        self.login_as_Bryan(self)  
        message = self._driver.find_element_by_class_name('darkred')  
        self.assertEqual("Welcome back Bryan, you are now logged in", message.text)  

    def test_Bryan_can_post_question(self):  
        self.login_as_Bryan()   
        self._driver.find_element_by_link_text("ask a question").click()  
        self._driver.find_element_by_id('id_title').send_keys("Question should succeed")  
        self._driver.find_element_by_id('editor').send_keys("This is the body text.")  
        self._driver.find_element_by_id('id_tags').send_keys("test")  
        self._driver.find_element_by_class_name("submit").click()  
        self.assertTrue(self._driver.find_element_by_link_text("Question should succeed"))  

    def tearDown(self):  
        self._driver.quit()  

問題不是Selenium,而是您的執行環境。 這取決於您如何啟動應用程序。

通常,您需要引導您的應用程序啟動,以便它指向一個臨時數據庫,僅在該測試期間使用。 執行測試后,應刪除該數據庫。

另外,您可以在實際網站中提供UI機制,以清除/刷新測試數據庫。 在這種情況下,您仍然需要一個測試數據庫,但是不需要在每次測試執行時都刪除/重新創建它。

您可以使用django-selenium ,它在測試數據庫上運行測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM