Skip to main content

Posts

Selenium ActionChains using Python with examples

ActionChains are used to automate interactions like mouse movement, mouse button action, key press, context menu, drag and drop etc. These are typically complex operations which can't be performed directly with web driver elements. Using ActionChains objects, specified actions are pushed into a queue and perform operation is called to execute these one by one. Example 1 : Context Menu from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By import time CHROME_DRIVER = r " ./browser_drivers/chromedriver " driver = webdriver.Chrome( executable_path =CHROME_DRIVER) driver.implicitly_wait( 10 ) driver.get( "https://www.spicejet.com" ) time.sleep( 4 ) # Initialize ActionChains Object actionchains = ActionChains(driver) login_element = driver.find_element(By.ID, "ctl00_HyperLinkLogin" ) club_members_element = driver.find_element_by_xpath( '//a[text()="SpiceClub
Recent posts

Quick tutorial with Selenium using Python

This post will be quick beginner's tutorial for Selenium using Python binding. First step is to install "python-selenium" and get the required browser driver's. The official link has all the details required to install selenium package for python and links for getting drivers for different browsers. First step is to import the webdriver class from selenium and set the path for the browser driver. from selenium import webdriver CHROME_DRIVER = r "./browser_drivers/chromedriver" SAFARI_DRIVER = r "/usr/bin/safaridriver" In this tutorial, I am using both Chrome and Safari brower. If you are using Mac, you would need additional settings.  Run the following command on Mac, if you get the not verified error :- xattr -d com.apple.quarantine ./browser_drivers/chromedriver Next, we will create the driver object to interact with the browser. Ths can be created by calling the appropritate browser class like Chrome, Firefox etc.  Note : For Mac, safaridrive

A guide to Selenium Architecture and Basics

Selenium is one of the most popular set of tools which are used for automating cross-browser web-applications. Selenium works on common operating systems - Windows, Linux/Unix and Mac and supports all major browsers like Chrome, Firefox, Internet Explorer, Edge, Opera etc. It also supports different programming languages like C#, Java, Javascript, Python, Ruby etc. This gives you the flexibility to the write the automation script in the same language in which the application under test is developed, though this is not a requirement. You can use any language of your choice for writing automation script. Selenium Components : Selenium IDE is mainly a browser add-on/extension mainly used for recording / replaying the script. Selenium RC (Remote Control) would inject Javascript into browsers. This is now depreceated and is now replaced with Selenium Web Driver. Selenium Webdriver allows you to create test scripts using the progamming language of your choice through the language bindings

Setting up Flask Migrate with existing SQL database / Resetting Flask Migrations

Flask Migrate allows to manage SQLAlchemy database migrations for Flask applications using Alembic. One of the common user-scenaio is that you have an existing database being used in the Flask Application. If you would like to integrate Flask-Migrate, this post will help you to start with the setup. Other user-scenario that I have seen is that once you integrate Flask-Migrate, some times people accidentally delete the migrations folder or would like to reset the migrations, the same steps can be followed. Let's get started. First step is to make sure that the flask-migrate is installed and included in your flask project. from flask_sqlalchemy import SQLAlchemy from flask import Flask, render_template, redirect, url_for from flask_migrate import Migrate import os basedir = os.path.abspath(os.path.dirname( __file__ )) app = Flask( __name__ ) app.config[ 'SQLALCHEMY_DATABASE_URI' ] = os.environ.get( 'DATABASE_URL' ) or \ 'sqlite:///' + os.path.join

Starting with Pytest framework

Pytest is test framework implemented in Python used for API, Database and UI testing. Running Pytest: When we run pytest  command without any argument, it will look for the "test_*.py" OR "*_test.py" files inside directories and subdirectories and run them. All test functions inside these files must start with " test " in their names.  Let's create our first file with unit test. We'll create very basic suite to test the addition and subtraction of two numbers to understand the concepts by creating two files as shown below. Basic strtucture of any test case is : - Function name has " test " as either prefix or suffix - Perform some operations - Verify the result using "assert" # test_addition.py d ef test_add_two_numbers_1 (): x, y = 5 , 20 s = x + y assert s == 25 def test_add_two_numbers_2 (): x, y = 5 , 20 s = x + y assert s == 20 # test_subtraction.py def test_subtract_two_numbers_1 (): x,

Beginner's tutorial on OpenCV using Python

OpenCV is an open-source cross-platform computer vision library having interfaces in different languages like C++, Java, Python. In this post, we are going to look at some of the basic functionality offered by OpenCV. Let's start with understanding some basics on images.  In simpler terms, an image can be represented in a 2-D matrix of pixels. As an example, if we have to represent number 1, box/pixel which is displaying black text can be represented by 0 and others by 1. This is known as Binary Image or 2-level image . Gray Scale Image : An image having 256 levels i.e. an 8 bit gray scale image means 2^8 levels from black (0) to white (255). In grayscale images, 2-dimensional matrix will have values between 0-255. Colored Image : A colored image will have three gray scale images representing intensity of Red, Green and Blue channels. Installing OpenCV : We can install OpenCV using  pip install opencv-python Reading Images : import cv2 import numpy as np # Reading Images

Python : Warp Perspective using OpenCV

 In perspective transformation, the perspective of an image is tranformed to get a better view or insights about the required informatin. We need points of the image from which we want to gather information and also the points inside which we want to display our image. In the example below, we will extract the visitor card from the image on the left and display it as shown in the right image. # Warp Perspective import opencv2 img = cv2.imread("opencv_resources/visitor_card.png") # end-points of the image that we want to extract # use tool like ms-paint to find the end points by placing your mouse cursor # upper-left pt (point1), upper-right pt (point2), lower-left pt (point3), lower-right pt (point4) pts1 = np.float32([[100,168], [555,60], [168,450], [667,282]]) # points to be mapped with the size of the extracted image i.e. map the points with 2D image width = 400 height = 200 pts2 = np.float32([[0,0], [width,0], [0,height], [width,height]]) # Transform perspective