TESTEROPS

A pragmatic approach to QA and OPS

Maximising Browser using Selenium

Maximising the browser. I think this is the most basic step and one of the most included step in test automation using Selenium. This is one of the fundamental steps that I think each and every test automation script/framework out there would contain.

By default when you open a Selenium script, a simple one such as getting the webpage, it opens in a certain default resolution. In most cases it is 800x600 resolution. But I think so 99.9% of times, we do not want this resolution, since most systems – desktop or laptops, do not have this resolution. Today in the times of 4k and 8k videos, this resolution hardly seems to satisfy our needs.

So what do we do? Maximise the browser to certain resolution or to a maximum screen resolution. Almost all of us who are versed with Selenium know how to do it using the maximise() method. But the beauty of Selenium is that it offers us multiple ways of doing something. So let’s see in how many different ways can we maximise the browser or change the resolution of the browser.

For the sake of simplicity, I’m considering Chrome browser for this and I’ve choosen Selenium Python bindings for this since I love to play around with Python.

  • Using the default maximise() method

This is the first method that comes to mind – tried and tested. This is the de-facto standard method to open the browser in the maximum screen resolution using Selenium.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc)
driver.maximize_window()
driver.get("https://selectorshub.com/xpath-practice-page/")
driver.implicitly_wait(20)
driver.quit()
view raw maximize1.py hosted with ❤ by GitHub

  • Using a pre-defined screen co-ordinates

Second method is the one where we can use a specific co-ordinate combination of x and y coordinates to set the size of the browser using the set_window_size() method. For eg, we can pass coordinates like 1400×900 in the method to open it to that resolution

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc,options=options)
driver.set_window_size(1400,900)
driver.get("https://selectorshub.com/xpath-practice-page/")
wait = WebDriverWait(driver,30)
driver.quit()
view raw maximize3.py hosted with ❤ by GitHub

  • Using the Chrome options --start-maximized

The ChromeOptions class gives us a lot of useful flags that we can use to alter/change the behaviour of the Chrome browser in Selenium. You can find the complete list of Chrome Options here.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
options.add_argument('–start-maximized')
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc,options=options)
driver.get("https://selectorshub.com/xpath-practice-page/")
wait = WebDriverWait(driver,30)
driver.quit()
view raw maximize2.py hosted with ❤ by GitHub

  • Using the Chrome options --kiosk

Similar to the --start-maximized flag, there is another flag called the --kiosk mode, that opens the browser in maximized mode in Chrome. However, unlike the former, the latter opens the browser in full screen mode, which you can see after running this code piece below

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument('–kiosk')
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc,options=options)
driver.get("https://selectorshub.com/xpath-practice-page/")
driver.implicitly_wait(20)
driver.quit()
view raw maximize4.py hosted with ❤ by GitHub

The above pic is the one with --start-maximized, while the below is with --kiosk

  • Using the javascript method to get the screen coordinates and then open browser to that resolution.

This is similar to the second approach that we discussed. But here instead of hard-coding the resolution, we use the js method to return the available width and height of the screen and then use the set_window_size() method to open the browser to that resolution.

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc)
max_width, max_height = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
print(max_width,max_height)
time.sleep(3)
driver.set_window_size(max_width,max_height)
driver.get("https://selectorshub.com/xpath-practice-page/")
driver.implicitly_wait(20)
driver.quit()
view raw maximize5.py hosted with ❤ by GitHub

  • Using the --start-fullscreen flag in ChromeOptions

Similar to the --kisok mode, we can also use the --start-fullscreen flag in the ChromeOptions class to open a browser in a complete full screen. Here the url bar would not be visible.. the screen almost appears same as the one in the --kiosk mode.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument('–start-fullscreen')
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc,options=options)
driver.get("https://selectorshub.com/xpath-practice-page/")
driver.implicitly_wait(20)
driver.quit()
view raw maximize6.py hosted with ❤ by GitHub

So as you can see there are more than 1 approach to maximising the browser or changing the resolution of the browser to a desired resolution. However almost all of these are tried and tested on Chrome only. I’ve not included Firefox or Edge ( most would work on Edge since it already has a Chromium core).

If someone knows about the ways we can do these in Firefox, please feel free to message me, comment on the blog or post on my LinkedIn.

I maintain this github repo, where I have added Python code for some no-so-commonly asked Selenium questions. Feel free to go through them.

%d bloggers like this: