Setting a test using xvfb + PyCharm + vagrant

2019-04-03 00:55发布

问题:

I have this environment:

  • PyCharm running on Mac OS X
  • Python3.4 environment running on Ubuntu Server in a vagrant instance

I want to be able to run/debug tests using PyCharm. So far I can do it but I recently added selenium to my tests now I need to wrap the python interpreter within xvfb-run remote command. I tried adding a remote external tool but I can't make it work yet. I found this guy but he doesn't explain very well how he made it. Any idea would be very appreciated :-)

回答1:

Thanks to this answer, I solved without adding an external tool. Steps:

  • Installed xvfbwrapper on remote python environment
  • Code sample:

    from selenium.webdriver.firefox.webdriver import WebDriver
    from django.contrib.staticfiles.testing import StaticLiveServerTestCase
    from xvfbwrapper import Xvfb
    
    class UITestCase(StaticLiveServerTestCase):
        fixtures = ['data.json']
    
        @classmethod
        def setUpClass(cls):
            cls.vdisplay = Xvfb()
            cls.vdisplay.start()
            cls.selenium = WebDriver()
            cls.selenium.implicitly_wait(3000)
            super(UITestCase, cls).setUpClass()
    
        @classmethod
        def tearDownClass(cls):
            cls.selenium.quit()
            cls.vdisplay.stop()
            super(UITestCase, cls).tearDownClass()
    
        def test_list(self):
            self.selenium.get('%s%s' % (self.live_server_url, '/#/app'))
    
            count = len(self.selenium.find_elements_by_xpath('//*[@id="data"]/tbody/tr'))
            self.assertEqual(count, 2)
    
  • No changes needed to your test configuration (assuming it already ran successfully)