In this post you will see how to write automation test scripts in headless mode using this configuration in 2018 ( On the web you can find huge number of snippets which are not working today):
- latest python 3.7
- Chromedriver 2.41
- selenium (3.14.0)
- Ubuntu 18.04
- Chrome 69
- Katalon studio
Youtube tutorial on the article:
First of all headless tests are tests without running the browser UI, which in this case means that there's no browser UI or no GUI. Headless tests are useful when performance or resources are limited.
The first step of headless tests with python is to install selenium module by:
step 1 python and selenium module
pip install -U selenium
We assume that you have installed latest version of python if not then you can have a look here: Install latest python in Ubuntu. I recommend to be used virtual environment for best experience when you are working with python. You can check it here: Python virtual environments
step 2 Chrome and chromedriver
Another assumption for this article is that you have installed Chrome/Chromium - if not you can check these articles:
Then you need to visit: Chromedriver Downloads and download the latest chrome version. After this you need to extract the driver to appropriate location. You need version: chromedriver_linux64.zip. You can unzip it by:
step 3 Run headless tests with Chrome
The final steps is to setup the code for the tests. Here you have several changes depending on your needs:
- driver path
If you downloaded the chrome driver in Downloads and extracted to chromedriver_linux64. Then your driver path is:
And you don't need to change the script:
from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') driver = webdriver.Chrome('/home/user/Downloads/chromedriver_linux64/chromedriver', chrome_options=chrome_options, service_args=['--verbose', '--log-path=/tmp/logs/chromedriver.log']) driver.get('https://google.org') print(driver.title)
step 4 Customize the headless script
If you want to record specific sequence of actions than I recommend you to use: Katalon Studio - at least in 2018. Some considered it as a successor of selenium IDE which was discontinued. Using the Katalon Studio you can record sequence of actions and export the script. You have short article here: Selenium/Katalon how to locate element by text.
Conclusion: You can use this script with extremely low configuration and using even Ubuntu server 18 (which doesn't have graphical interface). This test are perfect for small sites and regression tests of web applications. Have in mind that for some tests is best to be used real browser and headless mode is not applicable everywhere.
If you have any questions or problems please do share them and I'll try to help you.
Locate element, enter search text and submit
This example show you how to open Youtube, search for a given text and submit the query and finally return the page title. You can see also the difference between headless and normal test mode:
import time from selenium import webdriver import os chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') driver = webdriver.Chrome( os.path.abspath(os.curdir) + '/drivers/chromedriver_linux64/chromedriver', chrome_options=chrome_options, service_args=['--verbose', '--log-path=/tmp/chromedriver.log']) time.sleep(10) driver.get('https://youtube.com') print(driver.title) time.sleep(3) driver.find_element_by_xpath("//*[@id='search']").send_keys("Guido van Rossum") time.sleep(3) driver.find_element_by_xpath("//*[@id='search-icon-legacy']").submit() time.sleep(3) print(driver.title)