django硒LiveServerTestCase [英] django selenium LiveServerTestCase
问题描述
当我运行
./ manage.py test functional_tests
它加载一个页面标题:问题加载页面。身体:无法连接... function_tests.py:
从selenium import webdriver
from django .test import LiveServerTestCase
class GeneralFunctionalTests(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait (3)
def tearDown(self):
self.browser.quit()
def test_can_navigate_site(self):
self.browser。 get('http:// localhost:8000')
在self.browser.title中显示'Django'
我尝试使用classmethod设置和撕下去:
@classmethod
def setUpClass(cls)
super(MySeleniumTests,cls).setUpClass()
cls.browser = WebDriver()
...
结果是一样的。
但是我可以使用 self.browser.get('http://example.com')
加载任何其他网页。
硒是最新的。
谢谢!
您做错了什么?
LiveServerTestCase
在端口 8081上运行实时服务器
默认情况下,您尝试访问端口 8000
上的URL。现在,由于没有服务器监听端口8000,浏览器无法加载页面。
默认情况下,实时服务器的地址是
localhost:8081
和$
可以在测试期间使用self.live_server_url
进行访问。
您需要做什么?
选项1:更改网址
您可以将网址更改为 8081
端口。
def test_can_navigate_site(self):
self.browser.get('http:// localhost:8081')#更改端口
assert' Django'in self.browser.title
选项-2:使用实时服务器url
您可以在测试用例中使用 live_server_url
作为@yomytho 也指出。
def test_can_navigate_site (self):
/ pre>
self.browser.get(self.live_server_url)#使用live server url
在self.browser.title中显示'Django'
选项3:在端口8000上运行实时服务器
您可以通过
- liveserver
选项将端口号作为8000
传递给测试命令,以在端口上运行liveserver 8000。$ ./manage.py test --liveserver = localhost:8000#在港口8000运行维护者
I have an issue with selenium and LiveServerTestCase. When I run
./manage.py test functional_tests
it loads a page "Title: Problem loading page. Body: Unable to connect..."functional_tests.py:
from selenium import webdriver from django.test import LiveServerTestCase class GeneralFunctionalTests(LiveServerTestCase): def setUp(self): self.browser = webdriver.Chrome() self.browser.implicitly_wait(3) def tearDown(self): self.browser.quit() def test_can_navigate_site(self): self.browser.get('http://localhost:8000') assert 'Django' in self.browser.title
I tried to setUp and tearDown using classmethod:
@classmethod def setUpClass(cls): super(MySeleniumTests, cls).setUpClass() cls.browser = WebDriver() ...
Result is the same. But I could load any other pages in web with
self.browser.get('http://example.com')
. Selenium is up to date.Thank You!
解决方案What you are doing wrong?
LiveServerTestCase
runs the live server on port8081
by default and you are trying to access the url on port8000
. Now, since there is no server listening on port 8000, the browser is not able to load the page.From the
LiveServerTestCase
docs:By default the live server’s address is
localhost:8081
and the full URL can be accessed during the tests withself.live_server_url
.What you need to do instead?
Option-1: Changing the url
You can change the url to point to
8081
port.def test_can_navigate_site(self): self.browser.get('http://localhost:8081') # change the port assert 'Django' in self.browser.title
Option-2: Using the live server url
You can use the
live_server_url
in your test case as @yomytho has also pointed out.def test_can_navigate_site(self): self.browser.get(self.live_server_url) # use the live server url assert 'Django' in self.browser.title
Option-3: Running the live server on port 8000
You can pass the port number as
8000
to the test command via the--liveserver
option to run the liveserver on port 8000.$ ./manage.py test --liveserver=localhost:8000 # run liveserver on port 8000
这篇关于django硒LiveServerTestCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!