What is a headless browser?
A headless browser can access any website but unlike normal browsers (which you currently use) nothing will appear on the screen. Everything is done on the backend side invisible to the user.
What is headless testing?
Executing the web applications UI tests without opening a browsers user interface is called headless browser testing. A Headless browser will similarly act like a normal web browser. Testers will have full control over the web pages loaded into the headless browsers the only difference is you will not see a graphical user interface.
Why do we need headless browser?
Headless browser automation uses a web browser for end-to-end tests but skips loading the browsers’ UI. … headless browser automation may make it possible for you to add end-to-end tests to your testing process.
What do we want to do?
**Assuming that “Python is installed and IDE(ex.PyCharm) is ready to write the test script in python file”.
Let’s launch Chrome with and without headless mode , hit the indeed website , maximize the screen , send keys “selenium” in the search field and record the time required for this task.
Getting Started with Headless Chrome :
First, let’s import everything we’ll need to run Chrome in headless mode using Selenium.
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys
Defining Chrome
Before we set up a Chrome webdriver instance, we have to create an Options object that allows us to specify how exactly we want to launch Chrome. Let’s tell it that we want the browser to launch headless and that the window size should be set to 1920x1080
#Timer starts start = timeit.default_timer()# instantiate a chrome options object so you can set the size and headless preferencechrome_options = Options()chrome_options.add_argument("--headless")chrome_options.add_argument("--window-size=1920x1080")
Launching Chrome
Now that we have everything we need, we can jump into action! Create an instance of Chrome webdriver and pass it the the Options object we created earlier and the path to the actual ChromeDriver tool. Using the driver, go to the indeed.com, maximize the window and send keys Selenium. Don’t forget to set timer to log the time.
# go to google driver = webdriver. Chrome(chrome_options=chrome_options, executable_path="C:\\Selenium\\chromedriver.exe") driver.get("https://www.indeed.com") driver.maximize_window() #Sending selenium in search field driver.find_element_by_xpath("//*[@id='text-input-what']").send_keys("Selenium") #Timer Stops stop = timeit.default_timer() #Prints the Start and End Time to Console print('Time: ', stop - start)
After executing this we get the result in console as :
Time: 3.3800173Process finished with exit code 0
Getting Started without Headless Chrome :
We will be following the same steps except we will define the chrome browser without headless argument .
Defining and launching Chrome:
1. First import the webdriver and Keys classes from Selenium.
2. Next, create an instance of Chrome with the path of the driver.
3. Using the driver, go to the indeed.com, maximize the window and send keys Selenium. Don’t forget to set timer to log the time.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import timeit#Timer Starts
start = timeit.default_timer()
chrome_options = Options()
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="C:\\Selenium\\chromedriver.exe")
# go to Indeed.com
driver.get("https://www.indeed.com")
driver.maximize_window()
driver.find_element_by_xpath("//*[@id='text-input-what']").send_keys("Selenium")
#Timer Stops
stop = timeit.default_timer()
#Prints the Start and End Time to Console
print('Time: ', stop - start)
After executing this we get the result in console as :
Time: 4.6863921Process finished with exit code 0
Conclusion :
The recorded time for script execution with headless browser is 3.3800173 seconds and without headless browser is 4.6863921 seconds . so we can conclude it in terms of speed and data extraction .
1. Speed: Since headless browsers don’t have a UI, they are faster than real browsers .
2. Data Extraction : If your task is to scrape some data from website headless browser would do it much faster.
To rapidly test the application in various browsers and without any interruption, headless browser testing is used.
**If you are about to start your journey with headless Selenium, I recommend to use Chrome or Firefox. You can easily debug any issues by commenting — headless switch and see actual browser behavior.
Happy Coding Folks !!!
Comments