简体   繁体   中英

How to make Selenium and django use the same database

I'm a newbie in the use of Selenium.

And I got a problem using it. If I understand correctly, when I run my test suite (./manage.py test), django will create a new database for run the test suite. But if I include Selenium test in those test suite, it seems that Selenium connect to my server and access to my real database, which make my test failed.

What I'm trying to do is either be able to use the real database with django test or be able to use test database with Selenium.

Is there a solution to my problem?

from django.test import TestCase
from .models import Visit
#SELENIUM
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService

# Create your tests here.
from django.test import TestCase

# Faker
from faker import Faker

fake = Faker(['fr'])

def test_connect(driver):
    driver.get("http://demo.localhost:8000/")
    title = driver.title
    
    username = driver.find_element(by=By.NAME, value="login")
    password = driver.find_element(by=By.NAME, value="password")
    submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button[type='submit']")

    username.send_keys("demo")
    password.send_keys("demo")
    submit_button.click()
    driver.implicitly_wait(1)
    driver.find_element(by=By.XPATH, value="//p[contains(text(),'Aucun dossier ou fichier')]")        

class VisitCreationTestCase(TestCase):

    def setUp(self) -> None:
        service = ChromeService(executable_path=ChromeDriverManager().install())
        self.driver = webdriver.Chrome(service=service)
        test_connect(self.driver)

    def tearDown(self) -> None:
        self.driver.quit()

    def test_login(self):

        self.driver.find_element(by=By.PARTIAL_LINK_TEXT, value="Créer une visite").click()

        form = self.driver.find_element(by=By.ID, value="createVisitForm")

        name = form.find_element(by=By.NAME, value="name")
        description = form.find_element(by=By.NAME, value="description")
        
        company_name = fake.company()
        name.send_keys(company_name)
        
        company_description = fake.text()
        description.send_keys(company_description)
        
        submit = form.find_element(by=By.CSS_SELECTOR, value="input[type='submit']")
        submit.click()
        # assert Visit.objects.filter(name=company_name, description=company_description).exists()
        print(Visit.objects.filter(name=company_name, description=company_description).exists())
        return

You are connecting to the real server:

driver.get("http://demo.localhost:8000/")

No wonder that you are using real database, because you are entering real site. You need to use live_server_url and instead of TestCase , use ie StaticLiveServerTestCase :

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

class VisitCreationTestCase(StaticLiveServerTestCase):
    def check_connect(self, driver):
        driver.get(self.live_server_url)
        ...

    def setUp(self) -> None:
        ...
        self.check_connect(self.driver)

    def test_login(...):
        ...
        

You can find some nice practice help with Obey testing goat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM