13 Python Automation Scripts for Developers and Testers

Ekemini samuel

Posted On: September 30, 2024

view count222196 Views

Read time12 Min Read

Automated test scripts help ensure websites and web applications function as intended, and Python is a strong choice for writing these scripts.

With its simple syntax and powerful libraries, Python makes it easy to automate tasks like detecting broken links, handling dynamic content, managing pop-ups, and more. When combined with automation testing tools like Selenium, Python automation scripts further enhance the testing process, making it even more efficient and effective.

In this blog, let’s cover some of the best Python automation scripts that can help developers and testers in automating real-life scenarios.

Why Automate Tests With Python?

Python is a top choice for test automation due to its simplicity, readability, and rich set of libraries. Its clear syntax allows testers to write scripts quickly.

  • Simple and readable syntax: Python makes writing and understanding automation scripts easier, reducing complexity and errors.
  • Rich library support: Python has powerful libraries like Selenium, making it perfect for automating tasks such as handling dynamic content, pop-ups, and WebElements.
  • Seamless integration: Python integrates well with popular automation tools like Selenium, enhancing the efficiency of the testing process.
  • Beginner-friendly yet powerful: Python’s ease of use makes it ideal for beginners, while its advanced features cater to experienced testers as well.

How to Run Python Automation Scripts?

To run the Python automation scripts, ensure Python is installed on your system (Windows, macOS, or Linux). For Python automation testing, you can run tests on local grids or use cloud-based grids such as LambdaTest for better scalability and reliability.

For this blog, let’s use an AI-driven test execution platform like LambdaTest to perform Selenium Python testing on the cloud. This will help you test websites in different browsers and operating systems.

Now, install the necessary packages using the requirements.txt file by running the below command:

After that, get your LambdaTest Username and Access Key under your Account Settings > Password & Security, which you’ll need for the .env file.

The pyunitsetup.py file contains setup and teardown methods using PyUnit for initializing and closing the Selenium WebDriver.

The environ.get() method is used to retrieve environment variables LT_USERNAME and LT_ACCESS_KEY for authentication. An instance of the EdgeOptions() method is created to configure specific options for the Microsoft Edge browser.

The LambdaTest options in lt_options are set as capabilities using the set_capability() method, and the browser is initialized as a remote WebDriver instance using webdriver.Remote() method.

Github

The setUp() method performs setup actions before each test case execution. It sets an implicit wait of 10 seconds and maximizes the browser window. The tearDown() method is called after each test case execution to perform cleanup actions.

The requests library in Python is used for checking the status code of the requests. We will be using the PyUnit framework.

You can also explore the video tutorial below to run automated tests using Python on the LambdaTest platform.

Subscribe to the LambdaTest YouTube Channel for more such tutorials.

13 Best Python Automation Scripts

Once the above prerequisites are set up, you can use the below-mentioned Python automation scripts for your specific use cases.

Finding Broken Links

Broken links on a website can lead to an unpleasant user experience and negatively impact SEO as well. Detecting broken links is essential for ensuring the integrity and functionality of a website.

The below Python automation script with Selenium finds broken links. It navigates to the LambdaTest eCommerce Playground website for detecting broken links. All links on the page are then extracted using the find_elements() method.

Each link’s status code is verified by sending a HEAD request using the requests library. Any link returning a status code other than 200 is identified and printed as a broken link.

For more details, check out this blog on finding broken links using Selenium.

Handling Date Pickers

Date pickers are commonly used in web applications for selecting dates in forms or other input fields. Automating interactions with date pickers can streamline testing processes and ensure the functionality of date-related features.

The above Python automation script navigates to the Bootstrap Date Pickers Demo page of the LambdaTest Selenium Playground. Then, it locates the date input field and sets a specific date (e.g., “02/15/2024”) by sending keys and simulates pressing the Enter key to close the date picker.

For further information, take a look at this blog on using date pickers in Selenium.

Handling Dynamic Content

Dynamic content on websites, such as lazy-loaded images or asynchronously loaded elements, can pose challenges for automated testing. Selenium with Python enables us to interact with and validate dynamic content, ensuring that web pages behave as expected under various conditions.

The below Python automation script shows how to handle dynamic content.

First, we initialize the Selenium WebDriver with the pyunitsetup file and navigate to a web page containing dynamic content such as lazy-loaded images. Then, it waits for the dynamic content to load, and once loaded, it verifies the behavior of the dynamic elements.

Handling Cookies

Cookies are small pieces of data that websites store. They are often used to remember user preferences, login information, and other browsing-related data. In web automation, it’s essential to understand how to handle cookies to simulate realistic user behavior.

This Python automation script first initializes the PyUnitTestSetup() fixture, opens a website, and retrieves all the cookies associated with it. Then, it adds a new cookie to the browser session and retrieves the updated list of cookies to verify that the new cookie has been added.

Want more details? Check out this blog on handling cookies in Selenium.

Handling Menus

Interacting with menus is another common task in web automation. Menus often contain dropdown lists, submenus, and other interactive elements.

This Python automation script shows how to handle menus using the ActionChains class.

The script locates the menu element on the webpage using XPATH and uses ActionChains to hover over the menu element, which reveals the dropdown. Once the dropdown is visible, it clicks on a specific submenu element (in this case, “Clothing”). If any errors occur during the execution of the script, we catch and handle them appropriately.

Handling iFrames

Web pages often contain iFrames (inline frames) that embed another HTML document within the current document.

This Python automation script switches between the main content and iFrames.

First, it navigates to a Simple iframe page of the LambdaTest eCommerce Playground. Then, switch to the iframe by its name using switch_to.frame() method, and perform actions inside the iframe, such as locating and printing the heading. To switch back to the main content, use switch_to.default_content() method.

You can find additional information in this blog on handling Frames and iFrames in Selenium.

Handling Modals

Modals are used in websites to display information, capture user input, or confirm actions. Handling modals is a crucial aspect of web automation to ensure smooth user interactions.

The Python automation script below handles the modal boxes.

The main code logic is wrapped within a try-except block to handle any exceptions that may occur during execution. Next, the script navigates to a Bootstrap Modal page of the LambdaTest Selenium Playground.

It locates and clicks the button responsible for opening the modal, ensuring it’s clickable through an explicit wait. Then it waits for the modal to become visible before retrieving and printing its title. The tearDown() method is invoked to close the WebDriver session.

Discover more by checking out this blog on handling modal dialog boxes in Selenium.

Handling Popups and Alerts

Popups and alerts on websites are used to convey important information or prompt user actions. Handling popups and alerts effectively is crucial for web automation to ensure smooth user interactions.

The main code logic in this script is enclosed within a try-except block to handle any potential exceptions during execution. The script opens a website featuring popups and alerts. It locates and clicks on a button that triggers a popup using an XPath locator.

Upon encountering the alert, it switches to the alert using the alert class. The script retrieves the text from the Alert, accepts it, and prints the alert text.

Get more details from these blogs on handling authentication popups in Selenium and handling alert windows in Selenium.

Handling Shadow DOM

The Shadow DOM is a part of the website that provides encapsulation by scoping elements and their CSS styles. Handling elements within the Shadow DOM is essential for testing modern web applications that use Shadow DOM for component encapsulation.

This Python automation script navigates to the Shadow DOM of the LambdaTest Selenium Playground that features elements within the Shadow DOM. Using JavaScript execution, it locates a specific element within the Shadow DOM and retrieves its content.

To learn more, refer to this blog on handling Shadow DOM in Selenium.

Switching Tabs and Windows

Efficiently managing multiple browser tabs or windows is crucial in web automation, especially when testing complex web applications. The ability to seamlessly navigate between different tabs or windows allows testers to simulate user interactions across various parts of a web application.

This script opens the LambdaTest eCommerce Playground website in the main tab using the get() method. Then, it opens the ScrapingClub website in a new tab using a window.open() method of JavaScript. After waiting for a few seconds to ensure the new tab is fully loaded, it switches to the new tab using the switch_to.window() method.

Looking for more details? Read this blog on how to switch tabs in Selenium.

Testing UI Regression

UI regression is crucial for ensuring that changes made to a web application do not adversely affect its user interface and functionality.

This Python automation script performs UI regression testing by verifying the presence and visibility of key UI elements on a web page.

It initializes the WebDriver and navigates to the LambdaTest Visual Regression Testing page. Using the WebDriverWait() method, it waits for important UI elements such as the main heading, input field, search button, navigation menu, and footer to become visible on the page. Once each element is found, it asserts its visibility.

Scraping Websites

Web scraping involves extracting data from websites, enabling the collection of specific information for various purposes, such as data analysis, research, or content aggregation. Python offers libraries like BeautifulSoup that simplify extracting data from web pages.

This script uses the BeautifulSoup library to scrape product categories from the LambdaTest eCommerce website. It sends a GET request to retrieve the HTML content of the page. Then iterates over each product link, extracts the product category, and writes it to a CSV file.

For an in-depth explanation, read this blog on web scraping with Python.

Handling Web Tables

Web tables are used to display structured data on web pages, such as lists of products or user information.

This Python automation script interacts with tables on a web page. The WebDriverWait() method is used to wait for the table to load on the web page. Once the table is present, it finds all table rows using XPath. Then, iterates over each row and prints its content.

For a comprehensive guide, read this blog on handling web tables in Selenium.

Conclusion

With the rapid advancements in technology, automation is here to stay. Python offers an extensive array of libraries and tools that empower users to automate a wide range of tasks, from simple operations to complex processes. This can be achieved by writing Python automation scripts with tools like Selenium.

So, whether you’re a beginner or an experienced developer or tester, mastering Selenium Python for writing automation scripts can significantly enhance your workflow and open doors to new possibilities.

Frequently Asked Questions (FAQs)

What is a Python automation script?

A Python automation script is a program written to perform repetitive tasks or workflows automatically, such as file handling, web scraping, or data processing. It helps streamline tasks by reducing manual intervention.

Can I automate a Python script?

Yes, you can automate a Python script by scheduling it with tools like Cron (Linux) or Task Scheduler (Windows) or integrating it with automation frameworks to run tasks at specified intervals.

How to write automated test scripts in Python?

To write automated test scripts in Python, use testing frameworks like unittest, pytest, or nose. Define test cases, assertions, and test suites to validate the functionality of your code.

Author Profile Author Profile Author Profile

Author’s Profile

Ekemini samuel

Ekemini is a skilled Golang developer and technical writer, passionate about building scalable applications and creating detailed documentation. He specializes in simplifying complex concepts and contributing to SaaS projects. With a focus on Golang, Ekemini blends his technical expertise with writing to help others learn and grow in software development.

Blogs: 1



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free