Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Select Date Range From DatePicker Using Selenium Python Webdriver

When testing web applications that have datepicker with date range, we need to click on the from and to date controls to return the selected date into the datepicker's from and to fields. This article explains how to select a day from the datepicker for the "from" and "to" input box using Selenium Python Webdriver. I use the site "https://jqueryui.com/datepicker/#date-range'" for the example.

 

On the site, the datepicker date range control is within an iframe, so (1) we need to define the frame and switch the webdriver to the frame. (2) find the datepicker from element and click on it to display the calendar and find a day on the calendar and click on it  (3) find the datepicker to element and click on it to display the calendar and find a day on the calendar and click on it (4) verify that datepicker to field value equals the day we selected.

Print

Python REST API Response With UI Verification

In Selenium Python web test automation, we may need to verify REST API response against UI displayed content. In other words, what the REST API returned should be the same as what the UI displayed on the webpage. In this example, I use the open weather data api for testing. We load the api url on the webpage and get the api response for the same url and compare the response with the UI content. Here is the URL;

http://api.openweathermap.org/data/2.5/weather?q=Baltimre,us&APPID=70926ddfd37fdf454548b8db13695995

When we open the above URL with the Chrome browser, we get the following content on the UI. 

 

 
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"cmc stations","main":{"temp":279.219,"pressure":1025.61,"humidity":65,"temp_min":279.219,"temp_max":279.219,"sea_level":1032.41,"grnd_level":1025.61},"wind":{"speed":4.17,"deg":255.501},"clouds":{"all":0},"dt":1456610617,"sys":{"message":0.0102,"country":"US","sunrise":1456573287,"sunset":1456613832},"id":4347778,"name":"Baltimore","cod":200

In our test, we verify that (1) in the UI content, the city name should be "Baltimore"; (2) API response should be the same as UI displayed text;

Print

Python REST API Unit Test

Using Python Unit Test and urllib2 modules, we can test REST API in test automation. OpenWeatherMap API provides current weather data and weather forecast by city name, city id, city geographic locations or city coordinates. In this example, we test the api using city name. Please remember that you need to obtain a new API Key in order to use the api for business or for large scale data retrieval. Selenium Master does not take any legal responsibilities if the API Key does not work for you. 

In our test script, we make the api call using urllib2 library and do assertion that the city name should exist in the response json string. 

Create a python file and save it as "SeleniumPythonRestApiUnitTest.py". Write the following scripts. 

Print

Selenium Python Webdriver TinyMCE Text Input

Many websites use TinyMCE for creating site contents or articles because TinyMCE is a platform independent web-based JavaScript HTML WYSIWYG editor control released as open source under LGPL. In this example, I will explain how to clear the body of the TinyMCE and input text into TinyMCE using Selenium Python Webdriver. 

When looking at the site "https://www.tinymce.com/docs/demo/full-featured/" html resources, there are three iframes, so we need to use switch_to_frame() three times to move the webdriver inside the TinyMCE active editor section. 

Create a Python file using Python IDE and save it as "SeleniumPythonWebdriverTinymce.py". Write the following script into the Python file. 

Print

Selenium Python Webdriver Menu Test

In Selenium Python webdriver, you can use Action Chains to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. Using Action Chains, you can do more complex actions like hover over and drag and drop.

When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.

In this example, you will learn how to use the method move_to_element(to_element)

move_to_element(to_element)

Moving the mouse to the middle of an element

Args: 

to_element: The WebElement to move to

Test Case

After moving mouse over to the menu item "Salzburg", verify that sub menu "Perch" should be displayed. 

Test Steps:

1. Open browser and navigate to the demo site "https://jqueryui.com/menu/"

2. Move mouse over to the menu item "Salzburg". 

3. Verify that sub menu "Perch" should be displayed

Test Script

 

Print

How to Test JSON with Selenium Python

Many web applications provide API for external clients to use in their internal applications. When testing APIs with json response, QA engineers need to verify that what UI displayed should be equal to the API response for the same request input. In this article, we use the Open Weather API as an example to test the web response for this url "api.openweathermap.org/data/2.5/weather?q=London,uk" should be the same as the API response. For verifying UI with API response, please visit related articles on this website. 

The purpose of this example is to teach you how to parse Json response and assert on the content of Json response. For Example:

(1) when we navigate to this page "api.openweathermap.org/data/2.5/weather?q=London,uk" on web browser , we will see the following response. 

{"coord":{"lon":-0.13,"lat":51.51},"sys":{"type":1,"id":5187,"message":0.1923,"country":"GB","sunrise":1414565376,"sunset":1414600727},"weather":[{"id":701,"main":"Mist","description":"mist","icon":"50n"},{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09n"},{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":288.14,"pressure":1011,"humidity":93,"temp_min":288.05,"temp_max":288.15},"wind":{"speed":4.6,"deg":240},"clouds":{"all":92},"dt":1414547621,"id":2643743,"name":"London","cod":200}

(2) when we navigate to this page "api.openweathermap.org/data/2.5/weather?q=Bristol,uk" on web browser , we will see the following response. 

{"coord":{"lon":-2.6,"lat":51.46},"sys":{"type":1,"id":5076,"message":0.0434,"country":"GB","sunrise":1414565962,"sunset":1414601327},"weather":[{"id":741,"main":"Fog","description":"fog","icon":"50n"},{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":287.43,"pressure":1013,"humidity":93,"temp_min":287.15,"temp_max":288.15},"wind":{"speed":4.1,"deg":270},"clouds":{"all":90},"dt":1414547400,"id":2654675,"name":"Bristol","cod":200}

In the above Json responses, in both cases, the name key should contain "London" or "Bristol" depending on the city we pass to the API. 

Test Case: verify that weather API JSON response in the UI should contain city name.  see the python code and execution result below. 

Print

Python Selenium Page Object Model Tutorial

In Selenium Test Automation, Page Object Model is a framework in which all page elements are defined in a SiteModel class. In other modules of the test framework, the SiteModel class will be called to interact with elements. This makes the test maintenance easy because all the UI change will only affect the SiteModel class.

With Python language, we can define all site elements in an external xml file and read element information from this file and use in the main test code. When UI changes frequently, we just need to update the xml file with the updated UI elements and some find by logic.

Test Case: authorized users are able to login Mahara Demo site. Here are test steps.

1. Navigate to demo.mahara.org and verify that web page title "Home-Mahara" displays.

2. Enter "student1" in the Username filed. 

3. Enter "Testing1" in the Password field.

4. Click on the Login button

5. Assert that Logout link is present.

Note: if the above username and password do not work, try registering one and use your own user account for testing because the demo user password may have been changed by others. 

Test Implementation

Step 1: create a xml fie and save it as "SiteElement.xml" and add the following element information.

Print

Using Python Selenium to Write Tests

Users can write test cases using Python's unittest module. In this article, we use unittest as the test framework for Python Selenium WebDriver. This test is for http://demo.mahara.org login functionality. 

Test Case: authorized users are able to login Mahara Demo site. Here are test steps. 

1. Navigate to demo.mahara.org and verify that web page title "Home-Mahara" displays. 

2. Enter "student2" in the Username filed. 

3. Enter "Testing1" in the Password field. 

4. Click on the Login button

5. Assert that Logout link is present. 

Python Code for Test Implementation

Run Python GUI (IDLE) and write the following code. 

Print

How To Install Selenium Python Webdriver

Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Most of the Python Selenium libraries are very similar to Selenium .Net or Java libraries. 

Selenium Python bindings offers developers and QA engineers  a convenient API to access Selenium WebDrivers like Firefox, Ie, Chrome, Remote etc. The current supported Python versions are 2.7, 3.2, 3.3 and 3.4.

To use Python Selenium bindings, you need to install Python language and Python Selenium bindings. This articles explains how to install Python Selenium bindings in Windows environment.

How To Install Selenium Python Webdriver

Step 1: Install Python 3.4 using the Windows binary installer package. If you are using 32bit windows, download this package "Python 3.4.0 Windows x86 MSI Installer" and install it. For 64 bit windows, download and install the package  "Python 3.4.0 Windows X86-64 MSI Installer"