Skip to Main Content
 

Main Menu

 

Automate web testing using ORDS Selenium python

Hi Everyone
In this blog post I will show that how we can automate and test websites with the help of selenium. To demonstrate this blog post, I have developed Demo blog project in eclipse using PyDev. I have also created a demo website project using Oracle application express (APEX).

I have divided this blog post in three parts and created three different packages under project directory.
See code on GitHub

pydev_eclipse

1. Apex In this package I will interact with APEX website with the help of Selenium chrome driver. This will automatically starts the browser and navigate to the APEX website and get the objects from the webpage. Click here to read more about Selenium

2. Api In this package I will be calling ORDS api with the help of python Request and Json libraries and get the api objects from the database.

3. TestCases In this package I will write testcases and compare the data between APEX website and ORDS Api.

Lets start with the APEX web application. I have created an application with one login page and one demo page. Below you see a login page.
loginPage

This page appears after login. On this page you see six id's and six data objects
DemoTable

In the above pictures I have manually opened the chrome browser, wrote url in the web browser and provided credentials to login on the page. Now I will show you how we can automate this process with the help of selenium and we will fetch the values of id's and data objects from the webpage.
from
Apex.Config import Config
from
selenium import webdriver
from
selenium.webdriver.common.keys import Keys
from
selenium.webdriver.common.by import By
from
selenium.webdriver.support.ui import WebDriverWait
from
selenium.webdriver.support import expected_conditions as EC
from
selenium.common.exceptions import TimeoutException
class
Web:
    
def
load_apex_application(self):
        user_name =
'shazib'

        password =
'password'

        driver = webdriver.Chrome(
'C:/Users/mypc/selenium/chromedriver'
)
        driver.get(
'URL'
)
        element = driver.find_element_by_id(
'P9999_USERNAME'
)
##inspect the website

        element.send_keys(user_name)
        element = driver.find_element_by_id(
'P9999_PASSWORD'
)
        element.send_keys(password)
        element.send_keys(Keys.RETURN)
        delay = 1 # seconds
        
try:
            myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID,             self.conf.region_id)))
            id = []
            data = []
            
####fetch id elements

            id_1 = driver.find_element_by_id(
'ID_1'
)
            value = id_1.get_attribute(
'ID_1'
)
            id.append(value)
            id_2 = driver.find_element_by_id
ID_2'
)
            value = id_2.get_attribute(
'ID_2'
)
            id.append(value)
            
#####fetch data elements

            data_1 = driver.find_element_by_id(
'DATA_1'
)
            value = data_1.get_attribute(
'DATA_1'
)
             data.append(value)
            data_2 = driver.find_element_by_id(
'DATA_2'
)
            value = data_2.get_attribute(
'DATA_2'
)
             data.append(value)
            
print
(id, data)
            
#####return all values

            driver.quit();
             return id, data
       
except
TimeoutException:
            
print
("Page does not load")


Once you will run the above code. It will automatically starts the chrome browser as you can see I have given path to the selenium chrome driver, url to the website, username and password to login. You can find element_id by inspecting your website. See in the below picture it shows that Chrome is being operated by automated software.

auto_login

After login it gets the value of id and data from the demo page and print it in the console output. You can also see in the above code that I am printing and returning id [] and data [] array after appending values in it.

auto_login


Now the second part of this blog where I will fetch ORDS api in python using request and json libraries. I have already created a corresponding demo api in ORDS. Lets test the api in postman.

ords_api

Api has exact same records which I am using in my web application it has six id's and six data rows in the database. To consume the api in python, I have created two classes Request and ProcessJson. Below is generic Request class to call ORDS api

import
requests
import
json
class
Request:
    reply_status =
404
#custom code instead of exception handling

    items = 'No data found in the given endpoints'
    
def
__init__(self, endpoints):
        self.endpoints = endpoints
    
def
get_api(self):
        response = requests.get(self.endpoints)
        Request.reply_status = response.status_code
        
if
Request.reply_status == 200:
        dumps = json.dumps(response.json())
        items = json.loads(dumps)
        
else:

        items = Request.items
        
return
items, Request.reply_status


As you can see clearly that I am returing the api items and the reply_status in the above code.
Now I will show how we can extract values from the Json reply. See below ProcessJson class

from
Api.Request import Request
from
Apex.Config import Config
class
ProcessJson:
    endpoint = conf.api_endpoints
##api end points https://demo

    
def
get_all_rows(self):
        demo = Request(self.endpoint)
        json, status_code = demo.get_api()
        
print
(
'Get demo Api from Oracle Rest Data Services'
)
        
print
(json[
'items'
])
        
print
(
'api reply '
+ str(status_code))
        
print
(
'printing all rows in the Api'
)
                
if
status_code == 200:
        
for
items in json[
'items'
]:
             id, data = items[
'id'
], items[
'data'
]
            
print
(id, data)
             items = json[
'items'
]
        
return
items
    
def
get_all_values(self, json_array):
        id = []
        data = []
         value = json_array[0][
'id'
]
        
if
value == 1:
            
print
('')
            
print
('')
            
print
(
'==============================='
)
            
print
(
'extract values from the Api reply'
)
            
print
(
'==============================='
)
            
for
i in range(6):
                 id.append(json_array[i][
'id'
])
                 data.append(json_array[i][
'data'
])
            
return
id, data
        
else
:
            
print
(404,
"Demo api not found"
)

See the below Json, api reply_code and extracted values out of Json both id's and data.

ords_api

The great think I like about ORDS is that we can keep business logic inside the database. We can write code in Oracle PL/SQL and create logic before we build our API and we can skip alot of unnecessary code in other MVC frameworks.


So, Lets see what we have now

1. We have fetched id and data from APEX web application using Selenium chrome driver
2. We have also fetched id and data from ORDS demo api using python Request and json libraries.


Now we will write simple testcase and compare the results from the APEX web application objects and the ORDS demo Api.

import
unittest>
class
Testing(unittest.TestCase):
    
def
test_id(self):
         api_id = self.api_id[0]
##id from ORDS API id = 1

         web_id = int(self.web_id[0])
##id from APEX web application id = 1

        
print
(
' ')

        
print
(
' '
)
        
print
(
'========================================= '
)
        
print
(
'TestCase check id 1'
)
        
print
(
'========================================= '
)
         self.assertEqual(api_id, web_id)
    
def
test_data(self):
        api_data = self.api_data[0]
##data from ORDS demo API data = title one

         web_data = self.web_data[0]
##data from APEX web application data = title one

        
print
(
' '
)
        
print
(
' '
)
        
print
(
'========================================= '
)
        
print
(
'TestCase check data title one'
)
        
print
(
'========================================= '
)
         self.assertEqual(api_data, web_data)
if
__name__ == '__main__':
     unittest.main()


See the below results of two testcases where I am comparing api id, web id and api data,web data

testcase

vidro selenium

Web Hits
 
 

Feedback