Skip to content

Web automation - Python Selenium WebDriver, POM, Pytest, HTML reporting

Notifications You must be signed in to change notification settings

prabesh-mah/Pytest-Web-Automation

Repository files navigation

Selenium Logo

Project Description

Automated the Nepse Alpha website using Python with Selenium WebDriver, implementing the Page Object Model (POM) design pattern for enhanced maintainability and scalability. Utilized the pytest framework for testing, enabling efficient test execution and detailed HTML report generation. Incorporated various pytest plugins, including pytest-order for controlling test execution order and pytest-timeout for managing test execution time. Implemented custom markers such as smoke, regression, high_priority, medium_priority, and low_priority to effectively categorize and prioritize tests.

Requirements

Requirements Explaination in Brief:

  • undetected-chromedriver : This package is an optimized Selenium Chromedriver patch that does not trigger anti-bot services. It’s designed to not be detected by services like CloudFlare, Imperva, hCaptcha, and others.
  • pytest-order : It is a pytest plugin that allows you to customize the order in which your tests are run
  • pytest-timeout : It is a pytest plugin that interrupts and terminates tests that exceed a specified duration.
  • @pytest.mark.smoke : It is a custom marker used in the pytest framework for Python.
  • pandas: It is used to efficiently scrape HTML tables from web pages and save the extracted data into Excel files using 'to_excel()' function.

Testing Objective

  • Automate the testing of the Nepse Alpha website to ensure functionality.
  • Implement the Page Object Model (POM) design pattern for better maintainability.
  • Utilize pytest framework to identify and report bugs efficiently.

What are tested in this project

  • HTML Table Scraping
  • File Download
  • Handled Tabs
  • Search functionality with different combinations
  • Facebook Embed
  • Toggle between dark and light theme
  • About Nepse Alpha section
  • Advertise with us section
  • Form fillup
  • etc

Test Deliverables

  • Test Cases and Scripts
  • Test Execution Report
  • Defect Reports

Tested Device

  • Windows 11 Home 23H2 (Build 22631.4249)
  • 16 GB RAM, AMD Ryzen 7 5800H
  • 200 Mbps Internet Connectivity

Software Version

  • Python 3.12.2
  • Selenium WebDriver 4.25.0
  • Pytest 8.0.1
  • pytest-timeout 2.3.1
  • pytest-order 1.2.0
  • pytest-html 4.1.1
  • undetected-chromedriver 3.5.5
  • Chrome 128.0.6613.85
  • Firefox 129.0.2
  • Edge 128.0.2739.42

How To Setup This Project

Step 1: Use the command below to ensure that all softwares have been installed.

python --version
pip --version
git --version

Step 2: Then use the cd command to navigate to the directory where you wish to be download this project.

cd folder-name

Step 3: Now, use the git clone command to clone this project into that folder.

git clone https://github.com/prabesh-mah/Pytest-Web-Automation

Step 4: Once the project has been cloned, open the project using Visual Studio Code or any other IDE.

Step 5: Run the following command in the terminal to install the virtual environment, to install the essential libraries in an isolated environment.

pip install virtualenv

Step 6: Enter the command below to create a Virtual Environment. NOTE: venv is the name of virtual environment, name can be anything.

virtualenv venv

Step 7: Commands to activate and deactivate the Virtual Environment.

venv\Scripts\activate
deactivate

Step 8: After activating the virtual environment, install the necessary dependencies for this project.

pip install --no-cache-dir -r requirements.txt --upgrade

Usage

# 1: To run tests in pytest based on custom markers, you can use the -m option followed by the marker name. Below are the commands and markers that were used in this project.

pytest -m smoke 
pytest -m regression
pytest -m low_priority 
pytest -m medium_priority 
pytest -m high_priority 

# 2: To run all test methods and generate an HTML report at the end.

pytest -vs --html='report.html' --self-contained-html 

where,

  • -v : verbose, provide more detailed output.
  • -s: stdout, allow print statement to be displayed.
  • --html='report.html': This option generate an HTML report with file name report.html.
  • --self-contained-html: Generates a single HTML file instead of HTML and CSS file separately.

Full Code Execution Video

Watch the full code execution video. Here

Integration with Jenkins via. Declarative Pipeline

Wrote a declarative Jenkins pipeline that automates the process of cloning the feature branch from the GitHub repository Pytest-Web-Automation. The pipeline begins by creating a virtual environment named venv to isolate project dependencies, followed by activating the environment and installing the required Python packages from requirements.txt. It then confirms the installation of Python, pip, git, and pytest by checking their versions.

After ensuring that all necessary tools are in place, the pipeline executes one of the test using pytest and generates an HTML report saved in jenkins_reports/test_reports.html. Finally, it copies the generated test report to the user's desktop and cleans up by deleting the original report folder from the Jenkins workspace.

Jenkins Declarative Pipeline Code

pipeline {

    agent any

    stages {
        stage("Clone Repository") {
            steps {
                // Clone the GitHub repository where branch is 'feature'
                git branch: 'feature', changelog: false, poll: false, url: 'https://github.com/prabesh-mah/Pytest-Web-Automation'
            }
        } // end of stage 1

        stage("Setup Python Environment") {
            steps {
                // Create virtual envonment named 'venv'
                bat '''
                    python -m venv venv
                '''
            }
        } // end of stage 2

        stage("Install Dependencies") {
            steps {
                // Activate venv & Install required dependencies
                bat '''
                    call "venv\\Scripts\\activate.bat" ^
                    && pip install -r requirements.txt --upgrade
                '''
            }
        } // end of stage 3

        stage("Verify Dependencies") {
            steps {
                // Activate venv & Verify Install Dependencies
                bat '''
                    call "venv\\Scripts\\activate.bat" ^
                    && python --version && pip --version && git --version && python -m pytest --version
                '''
            }
        } // end of stage 4


        stage("Run Test and Generate Report") {
            steps {
                // Run the tests and generate HTML report
                bat '''
                    call "venv\\Scripts\\activate.bat" ^
                    && pytest -vs .\\tests\\HomePageTest\\playstore_redirection_test.py --html=jenkins_reports/test_reports.html
                '''
            }
        } // end of stage 5

        stage("Copy Test Report To Desktop") {
            steps {
                script {
                    // Construct the path to the desktop
                    def desktopPath = "C:\\Users\\Wicked Man\\Desktop\\"
                    def sourceReportPath = "C:\\ProgramData\\Jenkins\\.jenkins\\workspace\\GitHub\\Pytest-GitHub\\jenkins_reports"
                    def destinationReportPath = "${desktopPath}jenkins_reports"

                    // Copy the report folder to the desktop
                    bat "xcopy \"${sourceReportPath}\" \"${destinationReportPath}\" /E /I /Y"

                    // Check if the copy was successful
                    if (fileExists(destinationReportPath)) {
                        echo "Reports copied successfully to ${destinationReportPath}"

                        // Delete the original reports from the source folder
                        bat "rd /S /Q \"${sourceReportPath}\""
                        echo "Original reports deleted from ${sourceReportPath}"
                    } else {
                        error "Failed to copy reports to the desktop."
                    }
                }
            }

        } // end of stage 6

    } // end of all stages

} // end of pipeline

Integration with Jenkins Video

As for this Demo only one test case file is executed on this video i.e. fb_embed_test.py. Here

Releases

No releases published

Packages

No packages published