Python 为什么我的单元测试工作在Chrome驱动程序上,而不是PhantomJS上?
我在写我的第一个单元测试 我使用Chrome驱动程序编写测试,如下所示:Python 为什么我的单元测试工作在Chrome驱动程序上,而不是PhantomJS上?,python,unit-testing,selenium,phantomjs,selenium-chromedriver,Python,Unit Testing,Selenium,Phantomjs,Selenium Chromedriver,我在写我的第一个单元测试 我使用Chrome驱动程序编写测试,如下所示: from selenium import webdriver from selenium.webdriver.common.keys import Keys import unittest class PythonOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(r"C:\chromedri
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(r"C:\chromedriver_win32\chromedriver")
def test_search_in_python_org(self):
driver = self.driver
driver.get(r"http://www.python.org")
self.assertIn("Python", driver.title)
el = driver.find_element_by_name("q")
el.send_keys("pycon")
el = driver.find_element_by_id("submit")
el.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
在终端上运行它,它就工作了。终端返回:
.
----------------------------------------------------------------------
Ran 1 test in 7.873s
OK
但如果我替换
self.driver = webdriver.Chrome(r"C:\chromedriver_win32\chromedriver")
与
代码如下
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.PhantomJS(r"C:\phantomjs-2.1.1\bin\phantomjs")
def test_search_in_python_org(self):
driver = self.driver
driver.get(r"http://www.python.org")
self.assertIn("Python", driver.title)
el = driver.find_element_by_name("q")
el.send_keys("pycon")
el = driver.find_element_by_id("submit")
el.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
我的终端给我一个错误:
E
======================================================================
ERROR: test_search_in_python_org (__main__.PythonOrgSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_python_org_search.py", line 24, in test_search_in_python_org
el.click()
File "C:\Program Files (x86)
self._execute(Command.CLICK_ELEMENT)
File "C:\Program Files (x86)
return self._parent.execute(command, params)
File "C:\Program Files (x86)
self.error_handler.check_response(response)
File "C:\Program Files (x86)
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not
"Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50294",
67e-433b649e227d\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click",
elative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"",
","queryKey":{},"chunks":["click"]},
46/click"}}
Screenshot: available via screen
----------------------------------------------------------------------
Ran 1 test in 6.214s
FAILED (errors=1)
有人能告诉我为什么我不能在PhantomJS中使用
点击?使用PhantomJS时,屏幕截图是最有用的调试工具之一。我修改了您的脚本以记录故障点的屏幕截图:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.PhantomJS()
def test_search_in_python_org(self):
driver = self.driver
driver.get(r"http://www.python.org")
self.assertIn("Python", driver.title)
el = driver.find_element_by_name("q")
el.send_keys("pycon")
el = driver.find_element_by_id("submit")
try:
el.click()
except:
screenshot = driver.get_screenshot_as_png()
with open('screenshot.png', 'wb') as w:
w.write(screenshot)
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
当我运行它时,我得到了
您将注意到,该页面已呈现为移动浏览器。使用phantomjs时,应设置浏览器窗口大小:
def setUp(self):
self.driver = webdriver.PhantomJS()
self.driver.set_window_size(800, 1000)
完成此操作并比较chrome和phantom渲染后,您将看到phantom没有正确加载到submit按钮中:
解决方法之一是发送ENTER键,而不是单击按钮:
def test_search_in_python_org(self):
driver = self.driver
driver.set_window_size(800, 1000)
driver.get(r"http://www.python.org")
self.assertIn("Python", driver.title)
el = driver.find_element_by_name("q")
el.send_keys("pycon")
el.send_keys(Keys.ENTER)
screenshot = driver.get_screenshot_as_png()
with open('screenshot2.png', 'wb') as w:
w.write(screenshot)
使用上述代码,我现在可以看到结果:
值得注意的是,这就是为什么包括我在内的许多人不使用PhantomJS进行测试的原因。我发现使用Chrome和/或Firefox要好得多,可以在自己的无头模式下运行,也可以使用虚拟帧缓冲区(比如Xvfb)
我个人在基于云的linux实例上进行测试,并使用来管理虚拟显示。安装了这两个组件后,pyvirtualdisplay将完全管理您的虚拟显示会话。要将其与上述脚本一起使用,我将执行以下操作:
from pyvirtualdisplay import Display
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.display = Display(visible=False, size=(1200, 1500))
self.display.start()
self.driver = webdriver.Chrome()
def test_search_in_python_org(self):
# Do test
def tearDown(self):
self.driver.close()
self.display.stop()
尽管如此,我没有使用Windows的经验,但我相信该操作系统也有类似的配置非常感谢。看来我可以从我的电脑上删除幻影。你能给我一些关于在无头模式下使用Chrome的例子、材料或文档吗?@PaichengWu我在回答中添加了一些额外的细节
from pyvirtualdisplay import Display
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.display = Display(visible=False, size=(1200, 1500))
self.display.start()
self.driver = webdriver.Chrome()
def test_search_in_python_org(self):
# Do test
def tearDown(self):
self.driver.close()
self.display.stop()