How to automate web testing with Selenium and Python
Introduction to Python and Selenium
A well-liked open-source web testing framework for automating browser-based applications is Selenium. It supports a number of programming languages, including Ruby, Python, and Java. In order to interact with web elements on a web page, such as clicking buttons, filling out forms, and switching between pages, Selenium offers a simple-to-use API.
Python is a well-liked option for automation jobs because it is a flexible programming language with a sizable standard library. Selenium, pytest, unittest, and other frameworks and packages are available in Python for web testing.
Constructing the Environment
We need to set up our environment with the required software and dependencies before we can begin automated web testing. The following steps should be taken to prepare our environment:
1.Install Python: Get Python from its official website and install it. Choose the appropriate version for your operating system.
Install pip: The Python package manager pip is used to manage and install Python packages. Although pip is typically included with Python, you may verify that it is set up by using the following command at a command prompt or terminal:
Version 1 of pip.
If pip isn't already installed, you can do it by going through the steps on the official page.
2. Install Selenium: Make use of pip to set up the Selenium package. At the terminal or command prompt, type the following command:
selenium install pip
Installing a WebDriver is the first step in using Selenium to communicate with web browsers. For the web browser we wish to automate, the correct WebDriver needs to be installed. Below are the download links for WebDriver for well-known web browsers:
• Chrome: downloads are available at sites.google.com/a/chromium.org
Firefox users should visit https://github.com/mozilla/geckodriver/releases.
• WebDriver: https://developer.microsoft.com/en-us/microsoft-edge
Be sure to download the WebDriver for your operating system and web browser in the correct version.
Create a Test Case
To begin, let's create a straightforward test case for web testing automation. Our test case will be written using the Python unittest framework. The steps for writing our test case are as follows:
1. Upload the required modules:
unittest from selenium import bring in webdriver
2. Establish a class for the test case:
MyTestCase class (unittest.TestCase):
self.driver = webdriver.Chrome in def setUp(self) ()
Define test google search with self:
self.driver.find element by name("q").
self.driver.find element by name("btnK").
self.driver.quit def tearDown(self) ()
MyTestCase is a class that we define that derives from unittest.
TestCase. Also, we specify the three methods tearDown, test google search, and setUp.
Before to running each test method, the setup method is invoked. This function opens the website we want to test while creating an instance of the WebDriver. In this example, we're use the Chrome WebDriver.
• test google search: The real test case is contained in this function. By entering the search term "selenium" and pressing the search button, we do the sample Google search.
• tearDown: Following the execution of each test method, this method is invoked. This approach closes the web browser.
3. Establish a test suite:
If "__main" is the case, then suite is unittest.
3. Using unittest.TextTestRunner with verbosity set to 2.
run(suite) Using unittest to load our test case, we define a test suite in this part. TestLoader(). loadTestsFromTestCase(MyTestCase). After that, we use unittest to run the test suite. TextTestRunner(verbosity=2).run(suite). The output's level of information is specified by the verbosity argument.
Execute the test case, then:
Save the test case as test google search.py in your computer. Run the subsequent command after that at your terminal or command prompt:
Test Google Search in Python 3. The output of the test case should appear in your terminal or command prompt.
Web Interaction Automation
With Selenium, we interacted with web components like input fields and buttons in the preceding example. Selenium offers a number of ways to interact with web elements, such as:
Locate an element by its ID attribute using find element by id.
• find element by name: Locate an element based on its attribute name
Locate an element using an XPath expression or a CSS selector with the find element by xpath and find element by css selector functions.
Here is an illustration of how to use Selenium to interact with an input field:
Webdriver is imported from Selenium.
driver.find element by name search box ("q")
search box.send keys
search box.submit() \sdriver.quit()
The input field is located using the find element by name method in this example, the word "selenium" is entered using the send keys method, and the form is submitted using the submit function.
Managing browser tabs and windows
We may need to deal with several browser windows or tabs when automating web testing. Selenium offers a number of ways to manage browser tabs and windows, including:
• switch to.window: Change the browser's active window or tab
• window handles: Get a list of all open browser tabs or windows.
• current window handle: Get the handle of the currently open tab or browser window.
Here is an illustration of how to use Selenium to open a different browser window:
Webdriver is imported from Selenium.
Driver is defined as webdriver.Chrome(), driver.get("https://www.google.com"), and driver.execute script("window.open('https://www.python.org')").
Using Selenium and Python to Automate Web Testing Here are some best practises to adhere to when automating web testing with these two frameworks:
1. Make use of the Page Object Model: This design pattern separates the test code from the actual implementation of the web page. It contains the functionality of the web page in a Page Object, giving the test code a clear and reusable interface to communicate with the online page.
2. Version Control System : Use a version control system, such as Git, to track changes to your code and interact with others. It also gives you the option, if necessary, to go back to an earlier version of your code.
3. Employ a continuous integration system: You may automate code testing and deployment using a continuous integration system like Jenkins or Travis CI. Every time you post changes to your code repository, it may automatically run your tests and report whether they passed or failed.
4. Employ wait statements: Online sites can load slowly and their elements might not be instantly accessible. Use wait statements to wait for the page or element to load in order to prevent errors brought on by such delays. You can utilise the wait statements provided by Selenium, such as WebDriverWait and expected conditions, to wait for elements to become visible or clickable.
5. Make advantage of logging: Logging is a helpful tool for recording details about a test run, such as problems or exceptions that arise. You can use it to debug problems and find the root of failures.
6. Make use of parameterization: Using parameterization, you can run the same test with several input values. This might assist you in testing your application's behaviour in various settings and identifying edge cases.
7. Use assertions: By using assertions, you can verify that your application is acting as intended. Assertions can be used to verify that components are present, that text is shown appropriately, or that actions have the desired effect.
You can quickly and effectively detect flaws and regressions in your web application by automating web testing with Selenium and Python. We discussed the fundamentals of Selenium and how to create tests using Python and Selenium in this article. We also spoke about some guidelines for automated web testing. By using these methods and technologies, you can make your web application more dependable and high-quality while also shortening the overall development process.
Selenium Testing Course in Vizag