Selenium testing without browser
Asked Answered
G

10

67

I use Selenium RC for testing. Now to perform a load test, I want to run parallel test cases. Is there any way to run them without opening a browser?

Gringo answered 29/9, 2011 at 7:1 Comment(1)
possible duplicate of Is it possible to hide the browser in Selenium RC?Passenger
N
9

To set up on Centos (do all installation as root)

Install pip Download https://bootstrap.pypa.io/get-pip.py

python get-pip.py

Installing selenium If you have pip on your system, you can simply install or upgrade the Python bindings:

pip install -U selenium

Alternately, you can download the source distribution from PyPI (e.g. selenium-2.53.1.tar.gz), unarchive it, and run:

python setup.py install

install the program: pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg

Then modify your script and get this.

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class SeleniumDemo(unittest.TestCase):

    def setUp(self):
        self.display = Display(visible=0, size=(800, 600))
        self.display.start()
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.soastastore.com/"
        self.verificationErrors = []
        self.accept_next_alert = True


    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)
Norry answered 15/4, 2016 at 20:2 Comment(0)
B
134

Chrome now has a headless mode:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
Ballew answered 27/5, 2014 at 20:9 Comment(4)
this should be the answerCurve
Simplest answers are usually the best! Thank you.Baronetage
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless 'Citric
how can i use this if I additionally want to copy something to clipboard and then paste it? I tried it but it only works when the browser opensIsomer
Y
14

Since PhantomJS has been deprecated, using headless versions of Firefox would be a viable option.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get('https://www.google.com/')
Yamauchi answered 10/3, 2020 at 23:53 Comment(1)
this work in my macbook. I wonder why it would discretely open a firefox icon (looks like an idle icon) on the right side of my Dock, but yes you can't see what is happening. So I guess, it is operating headless indeed.Jamila
I
10

Try this code:

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
Ivanovo answered 6/4, 2019 at 15:34 Comment(0)
N
9

To set up on Centos (do all installation as root)

Install pip Download https://bootstrap.pypa.io/get-pip.py

python get-pip.py

Installing selenium If you have pip on your system, you can simply install or upgrade the Python bindings:

pip install -U selenium

Alternately, you can download the source distribution from PyPI (e.g. selenium-2.53.1.tar.gz), unarchive it, and run:

python setup.py install

install the program: pyvirtualdisplay

pip install pyvirtualdisplay

yum install Xvfb libXfont Xorg

Then modify your script and get this.

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class SeleniumDemo(unittest.TestCase):

    def setUp(self):
        self.display = Display(visible=0, size=(800, 600))
        self.display.start()
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.soastastore.com/"
        self.verificationErrors = []
        self.accept_next_alert = True


    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)
Norry answered 15/4, 2016 at 20:2 Comment(0)
G
7

You can run Selenium headless, take a look at this question/answer: Is it possible to hide the browser in Selenium RC?

Especially for performance load tests, you should have a look at Apache JMeter.

Gabriello answered 29/9, 2011 at 7:55 Comment(1)
I already tried Apache JMeter for load testing Of GWT apps. But it not quite successful.Gringo
N
3

Always follow the Documentation. Here is what selenium doc says. It provide a standalone jar.

  • Download the standalone jar. And run it with command

    java -jar selenium-server-standalone.jar
    
  • Now you will see a stanalone server started.

  • Now set up your webdriver like below and rest part will be as it is.

    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities={'browserName': 'htmlunit', 'version': '2', 'javascriptEnabled': True})
    
  • Summary code will be like.

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium.webdriver.common.keys import Keys
    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', 
    desired_capabilities={'browserName': 'htmlunit', 'version': '2', 
    'javascriptEnabled': True})
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.clear()
    elem.send_keys("pycon")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    driver.close()
    
Nitrosamine answered 23/11, 2017 at 8:33 Comment(0)
B
2

It is possible, but not with the standard firefox driver / chrome / etc.

You would need to install PhantomJS. Just assign your WebDriver to an instance of phantomJS driver:

driver = webdriver.PhantomJS()

If you run your code now, no browser window will be opened.

Bridesmaid answered 7/2, 2019 at 10:15 Comment(1)
the PhantomJS development is now suspended as per @Yamauchi and yes I can confirm. phantomjs.orgJamila
A
1

You can import Options if you don't want to open a web browser.

from selenium import webdriver   # for webdriver
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser

Then in the code:

option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)

And continue with the rest of the program.

Absolutism answered 10/2, 2020 at 21:41 Comment(0)
D
1

You can simply pass an argument "headless" to test selenium without opening the browser.

op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)

This code snippet will provide you exactly what you want.

Dextran answered 2/3, 2023 at 11:9 Comment(0)
C
0

requirements:

sudo apt-get install xvfb
pip install selenium
pip install PyVirtualDisplay

download chrome driver binary from below link and paste into drivers directory: https://sites.google.com/a/chromium.org/chromedriver/downloads

code:

from selenium import webdriver
from pyvirtualdisplay import Display

with Display(visible=False, size=(800, 600)):
    browser = webdriver.Chrome('drivers/chromedriver')
    browser.get('https://www.example.com')
    print(browser.page_source)
    browser.quit()
Celina answered 11/9, 2021 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.