我变得绝望,因为我似乎无法找到一个解决方案,我认为,在那里的每个人都会使用。
我想用selenium测试一个简单的登录,用一个live_server url测试pytest。根据pytest-django文档,一个名为live_server的简单工具应该可以完成这个任务(https://pytest-django.readthedocs.io/en/latest/helpers.html#live-server)。
不幸的是,当我通过这个夹具的测试,并试图访问我的网站,我得到:
本地主机拒绝连接
例如,在这方面:
def test_selenium(self, live_server, create_staff_user_in_db):
browser = webdriver.Remote(
command_executor='http://selenium:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)
browser.get(live_server.url)利用这个问题,Selenium unable to login to Django LiveServerTestCase,我试图使用一个不同的服务器-夹具,然后连接我和我可以看到我的网站在我的VNC查看器。
@pytest.fixture
def test_server() -> LiveServer:
addr = socket.gethostbyname(socket.gethostname())
server = LiveServer(addr)
yield server
server.stop()但有了这一点,我现在无法登录到我的网站,如果我创建用户在数据库与我的夹具。尽管看起来用户实际上是被创建的。所以我现在的测试看起来是:
def test_selenium(self, test_server, create_staff_user_in_db):
browser = webdriver.Remote(
command_executor='http://selenium:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)
browser.get(f"{live_server.url}/login")
input_username = browser.find_element_by_name('username')
input_password = browser.find_element_by_name('password')
input_username.send_keys('testuserstaff')
input_password.send_keys('mypasswordstaff')
browser.find_element_by_xpath(
'//*[@id="page-top"]/div[1]/div/div/div/div/form/button'
).click()我的用户夹具是:
@pytest.fixture()
def create_staff_user_in_db():
User = get_user_model()
staff_user = User.objects.create_user(
username="testuserstaff",
password="mypasswordstaff",
)
staff_user.is_staff = True
staff_user.save()
return staff_user测试访问我的登录页面,但无法登录。我百分之百肯定我使用的是正确的证书。
如果打印出来进行调试,我还可以验证我的用户是否在我的数据库中:
print(u.username) ==> testuserstaff
print(u.password) ==> igetthehashofthepassword
print(User.objects.count()) ==> 1 因此,我假设由pytest创建的db中填充了我传递的这个用户。现在,不知怎么的,我的实时服务器要么没有使用数据库,要么没有识别这些固定装置。
它必须连接到live_server/test_server夹具。如果有任何帮助,我将感激不尽。我觉得我离得太近了,但我似乎找不出它为什么不登录。
我还尝试过什么:( 1)登录到我的开发数据库中的用户。没有成功。所以我想知道:那么使用什么数据库呢?
2)尝试建立pytest-selenium库。如果我试图加载一个页面,就会得到selenium.common.exceptions.SessionNotCreatedException: Message: Unable to create session
3)我试图使用StaticLiveServerCase,但这不是一种选择,因为我需要将固定装置作为一个参数传递。
4)我在网上到处搜索,什么也找不到。
也很高兴知道:
我正在一个码头环境中运行这个(如果它有帮助的话,我很乐意分享)。
所以我的堆栈基本上是: Docker,Django,Pytest,selenium
再一次,我真的很感激你的帮助。提前谢谢你
编辑:
我的船坞-撰写文件:
version: '3'
volumes:
local_postgres_data: {}
local_postgres_data_backups: {}
services:
django:
build:
context: .
dockerfile: ./compose/local/django/Dockerfile
image: mywebsite_local_django
depends_on:
- postgres
volumes:
- .:/app
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
ports:
- "8000:8000"
command: /start
selenium:
image: selenium/standalone-chrome-debug
ports:
- 4444:4444
- 5900:5900
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: mywebsite_production_postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
env_file:
- ./.envs/.local/.postgres发布于 2020-03-30 08:27:20
将注释转换为一个答案:默认情况下,测试避免运行db查询以不减慢测试运行速度,因此User.objects.create_user不会写入数据库。为了确保db提交,在测试中使用@pytest.mark.django_db(transaction=True)标记或在固定装置中使用transactional_db夹具:
@pytest.fixture()
def create_staff_user_in_db(transactional_db):
staff_user = User.objects.create_user(...)
...我假设django的
live_server夹具自动使用transactional_db,但test_server不使用?
确切地说- live_server意味着事务模式,但是test_server的自定义驱动并不意味着。显式请求test_server中的transactional_db夹具应该修复它:
@pytest.fixture
def test_server(request) -> LiveServer:
request.getfixturevalue("transactional_db")
...顺便说一句,如果您只想动态地应用活动服务器地址,就不需要定义您自己的夹具;编写配置值就足够了。示例:
# conftest.py
def pytest_configure(config):
config.option.liveserver = socket.gethostbyname(socket.gethostname())然后,这个值将被内置的live_server夹具使用。将conftest.py放入您的项目或测试根dir,以使pytest足够早地找到适合应用的钩子。
https://stackoverflow.com/questions/60744437
复制相似问题