简体   繁体   中英

Run Python Selenium Tests Headless

I'm having a little trouble getting Webdriver to run on a specific hub headless with Pyvirtualdisplay. The following generic code works fine:

class TestHub4444TestClass01(unittest.TestCase):

    def setUp(self):
        self.display = Display(visible=0, size=(1920, 1080))
        self.display.start()
        self.driver = firefox.webdriver.WebDriver()

    def test_hub_4444_test_case_01(self):
        self.driver.get('http://google.com')
        time.sleep(5)


    def tearDown(self):
        self.driver.close()
        self.display.stop()

if __name__ == "__main__":
    unittest.main()

However the following opens a borwser window when I try to assign Webdriver to the hub on port 4445.

class TestHub4445TestClass01(unittest.TestCase):

    def setUp(self):
        self.display = Display(visible=0, size=(1920, 1080))
        self.display.start()
        self.driver = WebDriver(command_executor='http://localhost:4445/wd/hub', 
                                desired_capabilities={"browserName": "firefox",
                                                      "platform": "LINUX"})

    def test_hub_4445_test_case_01(self):
        self.driver.get('http://google.com')
        time.sleep(5)


    def tearDown(self):
        self.driver.close()
        self.display.stop()

if __name__ == "__main__":
    unittest.main()

Even if you are unsure of the answer, any suggestions would be greatly appreciated and I will give them a shot.

In case anyone else receives something like

WebDriverException: Message: u'Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:\nError: cannot open display: :99\nError:

Here are my commands in Ubuntu. I went to my xvfb control script.

sudo ./xvfb
export DISPLAY=:99
java -jar selenium-server-standalone-2.37.0.jar -role node -hub http://localhost:4445/grid/register -port 5560

I have my xvfb script located in the same place as my selenium server jar. Here is my xvfb script.

PIDFILE=/var/run/xvfb.pid
case "$1" in
  start)
    echo -n "Starting virtual X frame buffer: Xvfb"
    exec start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    ;;
  stop)
    echo -n "Stopping virtual X frame buffer: Xvfb"
    exec start-stop-daemon --stop --quiet --pidfile $PIDFILE
    echo "."
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
        echo "Usage: /etc/init.d/xvfb {start|stop|restart}"
        exit 1
esac

exit 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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