"Headless" browser testing, in which you use a command line interface or network communications to automate a web page rather than a graphical user interface, is available for the Chrome browser when running browser tests on Sauce Labs. In your test script, you need to set the desired capabilities chromeOptions
with an argument for '--headless'
, as shown in this example Python script.
import unittest
from selenium import webdriver
import os
class Selenium2OnSauce(unittest.TestCase):
def setUp(self):
user = os.environ.get('SAUCE_USERNAME')
key = os.environ.get('SAUCE_ACCESS_KEY')
caps = {}
caps['browserName'] = "Chrome"
caps['platform'] = "Windows 7"
caps['version'] = "57"
caps['name'] = 'Chrome switches - headless'
caps['chromeOptions'] = {'args': ['--headless']}
self.driver = webdriver.Remote(
desired_capabilities=caps,
command_executor="http://%s:%s@ondemand.saucelabs.com:80/wd/hub" % (user, key),
)
self.driver.implicitly_wait(30)
def test_sauce(self):
self.driver.get('http://google.com')
print(self.driver.title)
def tearDown(self):
print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
self.driver.quit()
if __name__ == '__main__':
unittest.main()