Automation Testing for a Systems Analyst: Using Selenium and Writing a Python Script

On September 19, I will hold a webinar: “Python for a System Analyst. Introduction to Practice”, where I will share in detail how to learn and start using Python, and also in practice I will analyze how to implement Python in your work. You can register in my telegram channel.

Let's start with the problem. One of the most frequent problems I encounter both as a system analyst and as a developer is finding cases or reproducing errors to work through and fix them. Moreover, this also concerns testing the implemented functionality.

Let me give you an example from practice. I received a request from a client where an error occurred while filling out a credit application after sending it. Since I cannot reproduce this case in the production environment under the client, to solve it I need to find a test client under which I can repeat the error and debug the functionality. In this case, I played both roles at once. If we consider this problem from the point of view of a system analyst, then I either need to also find a case to reproduce and analyze the error, or immediately give the task to development, which would not be entirely correct.

In such a situation, there is a need for automation. Since in order to find a problem case, you need to fill out and send credit applications manually, which have a huge number of fields. This creates a large amount of repetitive manual work that will not bring joy to either the system analyst or the developer.

One of my colleagues shared with me the idea of ​​using a tool like Selenium in my work. Remembering how to fill out credit applications, I realized that this is exactly the solution that would be perfect for a systems analyst in similar situations.

Let's move on to practice and see what is needed to use Selenium in work and what steps need to be taken to solve problems.

What algorithm will we follow:

  1. Recording actions via browser and Selenium IDE

  2. Exporting Python Code

  3. Writing a script for testing

  4. Analysis of results

To use Selenium in your work, you need to install Selenium IDE, I use the corresponding plugin in Google Chrome.

With Selenium IDE, you can record actions that will be performed for manual testing. In my case, it is filling out a credit application. I set the recording, fill out the application, send it and stop the recording.

Next, you need to export the resulting record as code. I export it in Python pytest format.

Now let's look at the fragment of the source code we exported. It describes the commands we executed when recording.

self.driver.get("http://127.0.0.1:5000/createcase")
self.driver.set_window_size(834, 661)
self.driver.find_element(By.ID, "creditProgram").click()
dropdown = self.driver.find_element(By.ID, "creditProgram")
dropdown.find_element(By.XPATH, "//option[. = 'Программа 2']").click()
self.driver.find_element(By.ID, "loanAmount").click()
self.driver.find_element(By.ID, "loanAmount").send_keys("2")
self.driver.find_element(By.ID, "loanPeriod").click()
self.driver.find_element(By.ID, "loanPeriod").send_keys("2")
self.driver.find_element(By.ID, "lastName").click()
self.driver.find_element(By.ID, "lastName").send_keys("2")
self.driver.find_element(By.ID, "firstName").click()
self.driver.find_element(By.ID, "firstName").send_keys("2")

The find_element function accesses page elements by their name in the markup, and then, using the click and send_keys functions, the actions that we performed manually are reproduced: clicking, filling, etc.

The next step is to write a script using the code we've received. Since our goal is automation, we need the script to work across multiple test cases and repeat manual actions for us.

First, we need to prepare the data for testing. Let's create a text file where we write down the data for each test client line by line.

4683 59 Bender Robin Karen 1930-03-02 7644 583899 1976-08-31 Perez 65851663310 College Moscow,Russia Moscow,Russia +79862451427
3538 59 Summers Margaret Daryl 1938-04-02 7650 858357 1951-07-03 Johnson 80712994342 University Moscow,Russia Moscow,Russia +79862451427
6916 15 Lin Jonathan Tammy 1948-02-19 7998 692107 1980-11-02 Smith 67809605761 College Moscow,Russia Moscow,Russia +79862451427
7138 34 Rowe Veronica Rachel 2011-10-11 3806 985024 1956-01-14 Adams 80497237448 College Moscow,Russia Moscow,Russia +79862451427
7415 25 Roach Kathy Adam 1909-04-15 9958 161510 1955-07-12 Browna 41966891142 University Moscow,Russia Moscow,Russia +79862451427
3171 14 Gates Brian Heather 1945-03-26 4286 981708 1957-02-10 Myers-Fuentes 16902406110 University Moscow,Russia Moscow,Russia +79862451427
8367 37 Phillips Kristina Erik 1929-07-19 8668 745842 2008-04-30 Anderson 89852425904 University Moscow,Russia Moscow,Russia +79862451427
2610 24 Hamilton Belinda Maria 2003-09-28 7529 424465 1964-04-14 Velasquez 35237801767 College Moscow,Russia Moscow,Russia +79862451427
6768 26 Hamilton Corey Devon 1985-11-04 8542 455879 1921-03-12 Ritter 86023564566 University Moscow,Russia Moscow,Russia +79862451427
9605 53 Green Ashley Paula 1930-02-04 8167 442035 1945-02-12 Baker 47394411324 School Moscow,Russia Moscow,Russia +79862451427

Let's move on to writing the script itself. We write a cycle that will go through the file with test data line by line. Then we split each line into separate values ​​using line.strip().split(' ').

with open('credit_info.txt', 'r') as file:
    for line in file:

        loanAmount, loanPeriod, lastName, firstName, middleName, dob, \
        passportSeries, passportNumber, passportIssueDate, passportIssuedBy, \
        snils, education, registrationAddress, residentialAddress, phone \
        = line.strip().split(' ')

Now let's take the code obtained as a result of export from Selenium. Remove the reference to self. Substitute the values ​​taken from the text file instead of those that we entered manually.

For example, instead of driver.find_element(By.ID, “loanAmount”).send_keys('manually entered value') you should get driver.find_element(By.ID, “loanAmount”).send_keys(loanAmount). A fragment of the resulting code is presented below.

driver.find_element(By.ID, "loanAmount").click()
driver.find_element(By.ID, "loanAmount").send_keys(loanAmount)
driver.find_element(By.ID, "loanPeriod").click()
driver.find_element(By.ID, "loanPeriod").send_keys(loanPeriod)

Let's take a closer look at our code. In addition to accessing page elements and filling in values, the code contains logic for working with the browser.

The driver = webdriver.Chrome() line opens the Chrome browser to work with it through the written program. The driver.get(“http://127.0.0.1:5000/createcase”) line contains the address of the page from which we begin testing. The driver.set_window_size(834, 661) function is required to set the screen size and is not mandatory in our case. At the end of the script, there is a driver.quit() line that closes the browser and is required in the script, as well as driver = webdriver.Chrome(). A code fragment from these lines is below.

driver = webdriver.Chrome()

driver.get("http://127.0.0.1:5000/createcase")
driver.set_window_size(834, 661)
driver.quit()

As a result, we get a script ready for testing. First, we export the necessary modules from the Selenium library. Then we perform all the actions described above. The final code is presented below.

from selenium import webdriver
from selenium.webdriver.common.by import By

with open('credit_info.txt', 'r') as file:
    for line in file:

        loanAmount, loanPeriod, lastName, firstName, middleName, dob, \
        passportSeries, passportNumber, passportIssueDate, passportIssuedBy, \
        snils, education, registrationAddress, residentialAddress, phone \
        = line.strip().split(' ')

        #часть экспорта из Selenium IDE
        driver = webdriver.Chrome()

        driver.get("http://127.0.0.1:5000/createcase")
        driver.set_window_size(834, 661)
        driver.find_element(By.ID, "creditProgram").click()
        dropdown = driver.find_element(By.ID, "creditProgram")
        dropdown.find_element(By.XPATH, "//option[. = 'Программа 2']").click()
        driver.find_element(By.ID, "loanAmount").click()
        driver.find_element(By.ID, "loanAmount").send_keys(loanAmount)
        driver.find_element(By.ID, "loanPeriod").click()
        driver.find_element(By.ID, "loanPeriod").send_keys(loanPeriod)
        driver.find_element(By.ID, "lastName").click()
        driver.find_element(By.ID, "lastName").send_keys(lastName)
        driver.find_element(By.ID, "firstName").click()
        driver.find_element(By.ID, "firstName").send_keys(firstName)
        driver.find_element(By.ID, "middleName").click()
        driver.find_element(By.ID, "middleName").send_keys(middleName)
        driver.find_element(By.ID, "dob").click()
        driver.find_element(By.ID, "dob").send_keys(dob)
        driver.find_element(By.ID, "passportSeries").click()
        driver.find_element(By.ID, "passportSeries").send_keys(passportSeries)
        driver.find_element(By.ID, "passportNumber").click()
        driver.find_element(By.ID, "passportNumber").send_keys(passportNumber)
        driver.find_element(By.ID, "passportIssueDate").click()
        driver.find_element(By.ID, "passportIssueDate").send_keys(passportIssueDate)
        driver.find_element(By.ID, "passportIssuedBy").click()
        driver.find_element(By.ID, "passportIssuedBy").send_keys(passportIssuedBy)
        driver.find_element(By.CSS_SELECTOR, "form").click()
        driver.find_element(By.ID, "snils").click()
        driver.find_element(By.ID, "snils").send_keys(snils)
        driver.find_element(By.ID, "education").click()
        driver.find_element(By.ID, "education").send_keys(education)
        driver.find_element(By.ID, "registrationAddress").click()
        driver.find_element(By.ID, "registrationAddress").send_keys(registrationAddress)
        driver.find_element(By.ID, "residentialAddress").click()
        driver.find_element(By.ID, "residentialAddress").send_keys(residentialAddress)
        driver.find_element(By.ID, "phone").click()
        driver.find_element(By.ID, "phone").send_keys(phone)
        driver.find_element(By.ID, "agreement").click()
        driver.find_element(By.CSS_SELECTOR, ".button").click()

        driver.quit()

I will also touch on the topic of scaling this solution, which intersects with its advantages. Since Python is one of the main languages ​​for data analysis, the described solution can be modified for additional tasks. The data obtained during testing can be analyzed using various approaches. Using the Seleniumwire library, we can access not only page elements, but also the API. This library will help us process the requests sent and the responses received as a result of testing the page. For example, in the case of a credit application, this could be a request to create an application or a request to receive data when filling it out.

The data obtained after processing requests can be analyzed using various quantitative metrics, scripts can be written for processing, or graphs can be built to visualize the results obtained.

In conclusion, I would like to say that the introduction of automation and development into your work opens up many new approaches to solving problems. The ability to write code or apply specialized testing skills for a systems analyst is not mandatory in most cases. But immersion in the system, the ability to apply new approaches and the ability to solve an expanded range of problems will always be your advantages.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *