General idea
Someone asks you hard questions, like: ‘Can you run your tests on Firefox?’ or ‘Can you start it with different screen resolution?’ You have your environment ready and you want go for a next step? Here is list of most important things that you can configure in the browser to make your test automation more flexible and reliable. Before you will start make sure that you have pytest and selenium packages installed. You will also need chromedriver for Chrome and geckodriver for Firefox (you can see how to configure your environment here).
To start your browsers you need to create selenium driver:
For Chrome:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
return webdriver.Chrome(options=chrome_options, executable_path='<path_to_chromedriver_file>')
For Firefox:
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
profile = webdriver.FirefoxProfile()
return webdriver.Firefox(firefox_profile=profile, options=firefox_options, executable_path=<path_to_gecko_driver>)
Starting your engine this way will allow us to change some useful options in your browser. In reality you should always open browser for automation by using ‘with’ statement:
import inspect
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class StartChromeDriver:
def __init__(self, executable_path, result_folder):
self.chrome_options = Options()
self.executable_path = executable_path
self.driver = webdriver.Chrome(options=self.chrome_options, executable_path=self.executable_path)
self.result_folder = result_folder
def __enter__(self):
return self.driver
def __exit__(self, tp, value, tb):
if value:
current_frame = inspect.currentframe()
outer_frame = inspect.getouterframes(current_frame, 2)
caller_name = outer_frame[1][3]
self.driver.get_screenshot_as_file(os.path.join(self.result_folder, caller_name + '.png'))
self.driver.quit()
In init we are defying where is executable driver file for our browser. We also create options for engine that will allow us to change our browser later. You can also specify folder that will save screenshot of last page visited by the browser (if there was any error in the test). This will create png file on given folder with name that will be taken from test that created this class.
Then you can use it like:
def my_second_test():
with StartChromeDriver as driver:
driver.get(‘www.google.com’)
If everything work correctly we can start messing up with browser options.
Open window in full size:
Basic options that allows you to start browser maximized. It is very useful for basic tests that will use biggest screen display resolution possible on any machine that you will run your tests.
For Chrome:
chrome_options.add_argument("--start-maximized")
For Firefox:
firefox_options.add_argument("--start-maximized")
Open window with given window size:
For Chrome:
chrome_options.add_argument("--window-size=600,800")
For Firefox:
firefox_options.add_argument("--window-size=600,800")
Open browser in headless mode:
One of most useful options possible. With this one you can run your browsers in headless mode so you will not be frustrated by windows popping out in the background when you are preparing your tests.
Chrome:
chrome_options.add_argument("--headless")
Firefox:
Setting up firefox to headless mode is more complicated – you need to change it in your operating system options:
import os
os.environ['MOZ_HEADLESS'] = '1'
Change download directory for browser:
This option you can use for some tests where you want to check correctness on downloaded tests and you don’t want to make a lot of mess on your machine. Just create folder like ‘Downloads’ in your project folder and you can easily keep all downloaded files in one place (without complicated process of moving them). Variable download_path should contain path to folder that you want to use.
For Chrome:
preferences = {'download.default_directory': download_path}
chrome_options.add_experimental_option('prefs', preferences)
For Firefox:
On firefox it is a little more complicated. At first we need to set browser.download.folderList to value 2 (0 – save on desktop, 1 – save in downloads, 2 – save in last specified folder
profile.set_preference("browser.download.folderList", 2)
We cannot click browser download pop out with selenium engine so we need to disable it:
profile.set_preference("browser.download.manager.showWhenStarting", False)
We need to specify folder that we want to download files:
profile.set_preference("browser.download.dir", download_path)
We also need to specify type of files that we will allows to download – using MIME types (in this example any byte stream and pdf files
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream;application/pdf;")
We need to also disable security pop outs when downloading ‘octet-stream’ files or any other files that can potentially harm your machine.
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
If you want to use pdf files you will also need to disable default pdf reader so it will download files instead of opening them.
profile.set_preference("pdfjs.disabled", True)