Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Selenium tests against a test database?

I like to run tests before I commit. I'm new to Selenium and I don't understand how to run the tests and not change the database.

My local database has dozens of identical posted questions.

Is there any way that I can have have these tests run and not have the database restored to it's original state on 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()  
like image 937
BryanWheelock Avatar asked Nov 04 '22 07:11

BryanWheelock


1 Answers

The issue is not so much Selenium as it is your execution environment. It depends on how you fire up your application.

In general, you need to bootstrap your application launch so that it points to a temporary database for use only during that test. After the test execution, you should delete that database.

Alternately, you can provide a UI mechanism in your actual website to clear / refresh the test database. In that case, you still need a test database, but you don't need to delete/recreate it with every test execution.

like image 58
Stephen Gross Avatar answered Nov 15 '22 06:11

Stephen Gross