Search code examples
seleniumgeckoselenide

Firefox runs in background in Selenide


usually, questions are in opposite way, how to make a Firefox run to be in the background. I have written some time ago some basic tests in Selenide, but when today I tried to run it (as usual) on a server, I got an error

SessionNotCreatedException

I started to look for the result and I noticed, that when I'm running now tests locally from my computer, Firefox does not appear. I can see Firefox's tasks in Task manager, I got an error with a done screenshot, but the browser does not open.

I noticed there is plenty of questions about how to run tests with the headless option, but I need something opposite, this might be a problem with SessionNotCreatedException, the server does not see the browser.

As I know Selenide runs the newest gecko driver (it's updating). I tried to set some options in the beginning:

    FirefoxOptions options = new FirefoxOptions();
    options.setCapability("marionette", false);
    options.setCapability("headless", false);

and also updated Selenide to 5.0.0, but it's still failing

EDIT: I can't use any other browsers


Solution

  • For running tests on a server generally, the server is an X window system so the way to do it is to run a virtual display.

    Using Xvfb is the best way for that! you can read about it here.

    from xvfbwrapper import Xvfb
    
    with Xvfb() as xvfb:
        # launch virtual display here.
        # start your webdrivr in the virtual display
    

    Or you can use PyVirtualDisplay link here.

    from pyvirtualdisplay import Display
    from selenium import webdriver
    
    display = Display(visible=0, size=(800, 600))
    display.start()
    
    # now Firefox will run in a virtual display. 
    # you will not see the browser.
    browser = webdriver.Firefox()
    browser.get('http://www.google.com')
    print browser.title
    browser.quit()
    
    display.stop()
    

    Note

    Make sure your server is an X Window System!

    As you can see here it doesn't work on windows.

    Hope this helps you!