/images/avatar.png

Hi! I'm Pratyaksha.

Design Patterns

Design patterns are typical solutions to commonly occurring problems in software design. They are like pre-made blueprints that can be customized to solve a recurring design problem in any code.

All patterns can be categorized by their intent, or purpose.

Creational Patterns

This pattern provides ways to create object creation in a manner that increases flexibility and reuse of code.

Factory Method

This pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Solid Design Principles

SOLID is a set design principles in object-oriented context to make maintainable, testable and extendable software.

Single Responsibility

A class should only have one responsibility. Furthermore, it should only have one reason to change.

Benefits:

  • Testing: A class with one responsibility will have far fewer test cases.
  • Lower coupling: Less functionality in a single class will have fewer dependencies.
  • Organization: Smaller, well-organized classes are easier to search than monolithic ones.

Example

Bad

A class representing a book:

Selenium Cheat Sheet

Info
This post assumes prior knowledge of python and selenium 4

Installation

Selenium: pip install selenium

Download the drivers, make sure that your browser, selenium and driver versions are compatible with each other.

Instantiate your driver

Chrome

1
2
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/webdrivers/chromedriver.exe")

With custom options

1
2
3
4
5
6
7
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
## option to not show an open browser window
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path="C:/webdrivers/chromedriver.exe", options=options)

Mozilla

1
2
from selenium import webdriver
driver = webdriver.Firefox(executable_path="C:/webdrivers/geckodriver.exe")

With custom options

1
2
3
4
5
6
7
8
9
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
# option for firefox.exe location, if firefox is not installed in default location
options.binary_location = "C:/.../firefox.exe"
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
driver = webdriver.Firefox(executable_path="C:/webdrivers/geckodriver.exe", options=options)

Edge

1
2
from selenium import webdriver
driver = webdriver.Edge(executable_path="C:/webdrivers/msedgedriver.exe")

With custom options

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.edge import service

options = webdriver.EdgeOptions()
options.use_chromium = True
options.add_argument("start-maximized")
# option for msegde.exe location, if edge is not installed in default location
options.binary_location = r"C:/../msedge.exe"
service = service.Service("C:/webdrivers/msedgedriver.exe")
driver = webdriver.Edge(service=service, options=options)

Open a website

1
driver.get("https://google.com")

Find an element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium.webdriver.common.by import By

element = find_element(By.ID, "ID")
element = find_element(By.NAME, "NAME")
element = find_element(By.XPATH, "XPATH")
element = find_element(By.LINK_TEXT, "TEXT")
element = find_element(By.PARTIAL_LINK_TEXT, "PARTIAL_TEXT")
element = find_element(By.TAG_NAME, "TAG_NAME")
element = find_element(By.CLASS_NAME, "CLASS_NAME")
element = find_element(By.CSS_SELECTOR, "CSS_SELECTOR")

Find multiple elements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium.webdriver.common.by import By

elements = find_elements(By.ID, "ID")
elements = find_elements(By.NAME, "NAME")
elements = find_element(By.XPATH, "XPATH")
elements = find_element(By.LINK_TEXT, "TEXT")
elements = find_element(By.PARTIAL_LINK_TEXT, "PARTIAL_TEXT")
elements = find_element(By.TAG_NAME, "TAG_NAME")
elements = find_element(By.CLASS_NAME, "CLASS_NAME")
elements = find_element(By.CSS_SELECTOR, "CSS_SELECTOR")

Get information from element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
element.text
# get an attribute like href, class, value etc
element.get_attribute("ATTRIBUTE")
# get property like text_length
element.get_property("PROPERTY")
# is element visible to user
element.is_displayed()
element.is_enabled()
element.is_selected()
element.screenshot("/Screenshots/foo.png")

Interactions

Button

1
element.cick()

Text

1
2
3
4
element.send_keys("lorem ipsum")
# press arrow key
element.send_keys("lorem ipsum", Keys.ARROW_DOWN)
element.clear()

List of all keys is at: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys