简体   繁体   English

如何使Selenium和django使用同一个数据库

[英]How to make Selenium and django use the same database

I'm a newbie in the use of Selenium.我是使用 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.如果我理解正确,当我运行我的测试套件(./manage.py 测试)时,django 将创建一个新的数据库来运行测试套件。 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.但是如果我在这些测试套件中包含 Selenium 测试,似乎 Selenium 连接到我的服务器并访问我的真实数据库,这使我的测试失败。

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.我正在尝试做的是能够将真实数据库与 django 测试一起使用,或者能够将测试数据库与 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 :您需要使用live_server_url而不是TestCase ,使用即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 .您可以在Obey testing goat中找到一些很好的练习帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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