Sign in

Software Engineer, Python Enthusiast, Darbuka Player, https://coskundeniz.github.io/

How to use Selenium with Python’s unittest

In the previous post, we explored the page object model for writing a more maintainable and reusable code. We will see the usage of Selenium with Python’s unittest module in this post.

Unit tests are used to test the smallest testable parts of a program. Python’s unit testing framework provides test management features like setting pre/postconditions, checking the result with the expected output, and generating a report of test executions.

unittest module consists of five functionality

  • Test Loader: Loads test cases and suites.
  • Test Case: Implements the interface needed by the test runner.
  • Test Suite: Groups set of test cases.

Page Object Model for better structured and maintainable code

In the previous post, we explored managing window position and dimensions and also taking screenshots with Selenium. We will see Page Object Model(POM) to create more structured and maintainable code in this post.

Until now, we have used mostly functions and single modules for our examples. If you try to manage a web page with lots of pages or components with a wide range of interactions among them, things might get complicated with this approach.

This is a general problem and as always there is a common solution for this common problem with a design pattern called “Page Object Model”.


Farewell to 2020 with Python

If you are a person who likes to send new year message cards to your friends or family, maybe you want to do it by programming this time. Let’s start…

Prepare Your Environment

We need to create a virtual environment before starting to isolate dependencies from the rest of the system.

  • If you are using pip
python -m venv env
source env/bin/activate
pip install yagmail
  • If you are using pipenv
pipenv install yagmail
pipenv shell

yagmail is a Python package created to interact with Gmail accounts to make sending emails easier.

Implementation & Explanation

The program will have both command line parameters and interactive mode support.


Managing windows and taking screenshots

In the previous post, we looked at controlling mouse actions. We will see how to manage window position and dimensions and also take screenshots in this post.

In part 6 of this series, we explored window handles and switching between windows. This post will complete the window related discussion mostly.

Window Management

Screen resolution can impact how web applications render, so Selenium WebDriver provides mechanisms for moving and resizing the browser window.

get/set window size

get_window_size retrieves the size of the browser window in pixels as a dictionary with width and height keys.

width = driver.get_window_size().get("width")
height = driver.get_window_size().get("height")

set_window_size restores the window and sets…


How to control the mouse

In the previous post, we looked at working with select elements. We will look at controlling the mouse actions in this post.

We had briefly looked at some mouse actions in the Keyboard and Action Chains post. We will use action chains in the examples throughout this post too.

Selenium enables us to emulate all kinds of mouse actions like a double-click, right-click, mouse over, drag&drop, etc. These actions performed with the help of ActionChains class.

Every action called on the ActionChains object is stored in a queue and executed in order with the perform call. …


How to interact with select elements

In the previous post, we looked at how to manage different kinds of alert dialogs. This post will be about working with select elements on a web page.

The <select> tag is used to create a drop-down list of options. It is usually used in a form to collect user input.

Selenium provides a Select class in the support package to manage select elements easily.

In order to start, we need to create a Select instance with a select element. If you don’t give an element with the select tag, UnexpectedTagNameException is raised.

from selenium.webdriver.support.select import Select# ...element…


How to handle alert dialogs

In the previous post, we looked at working with cookies. We will explore the handling of alert dialogs in this post.

An alert is a pop-up window. It gets triggered due to some action performed by the user or automatically due to some system settings. Their purpose is to give some information to the user, take permission, or take some input from the user.

Selenium WebDriver Alert API provides methods to handle interactions with these pop-up message boxes.

We can categorize the alerts into the following three types:

  1. Simple Alert
  2. Confirmation Alert
  3. Prompt Alert

Simple Alert

A simple alert shows a custom…


How to manage cookies with Selenium

In the previous post, we looked at how to execute JavaScript code with the help of Selenium. We will explore cookie management in this post of the series.

What is a cookie and what are they used for? Cookies are key-value pairs followed by zero or more attribute-value pairs stored on the client-side. It is a small piece of data sent from the web application and stored in the web browser, while the user is browsing that web page. They usually store information about users, their preferences, and past activities. …


How to execute JavaScript code with Selenium

In this post, we will look at how to execute JavaScript code on the page with Selenium.

Document Object Model(DOM) can access and manipulate all the elements on a web page with JavaScript. You can inspect an element on a web page and see the available methods using developer tools of your browser of choice. You can run JavaScript code with Selenium if a certain action can not be performed using regular Selenium commands.

To accomplish this, Selenium WebDriver will inject the JavaScript statement into the browser and the script will perform the job.

There are two methods for the…


Customizing events

In the previous post, we explored how to pass options to the Selenium WebDriver instance to set the various preferences for the browser.

EventFiringWebDriver class is a wrapper for a WebDriver instance that supports firing events.

You can use this to take some actions before or after certain events like finding an element, navigating to a url, clicking an element, or quitting the browser.

It takes driver, and event_listener as arguments. event_listener is an instance of a class that subclasses the AbstractEventListener class.

Following methods of the AbstractEventListener class should be implemented fully or partially.

* before_navigate_to(self, url, driver) *…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store