Hands-On Test Automation: Using Selenium With Python
Sauce AI for Test Authoring: Move from aim to execution in minutes.|xBack to ResourcesBlogPosted July 13, 2023
Hands-On Test Automation: Using Selenium With Python
Learn about using Selenium with Python for test automation. Find out how to set up your testing environment, including instalment and running Selenium.
Why Choose Python for Test Automation?
Python is one of the old programming languages. It has a strong worldwide community that has already released quite a few packages for test automation, such aspytest, unittest, doctest, etc. Python can be used in multiple frameworks, code editors, and integrated development environments (IDEs).
The reasons this language managed to last so long and nevertheless keep evolving are its compactness, simplicity, and universality. The syntax is very visceral, and coder can select to use grade or functions because Python is both object-oriented and functional.
For test automation, it offers two more perks. Python works perfectly with command-line tools, enable you to build continuous integration/continuous delivery (CI/CD) pipelines. This language executes compile and run steps in one leap, hence, becoming the number one choice for beginners.
Not to mention that it works withSelenium, currently one of the most used framework for testing web coating.
What is Selenium?
Seleniumis a testing suite or set of creature. It is open-source and was developed specifically for automated cross-browser examination of web applications. Selenium became so popular that Apple still incorporate it into Safari starting from macOS El Capitan.
One of Selenium ’ s tools is WebDriver. It is an application programming interface (API) and a collection of libraries that access different web browser (using driver proxies) and perform actions from the testing script. Therefore, one can say that Selenium and WebDriver help your testing script—for instance, one compose in Python—to talk to the web browser.
Selenium is not circumscribed to running Python-based scripts. It has integrated language bindings that map functions and objects from one programme language with uncommitted part and objective in another. For representative, Selenium has aclick () command that simulates user clicksand allow you touse Cascading Style Sheets (CSS) selectorsto point the automated exam to where it should utilise this bidding on a web page.
How to Install and Run Selenium and Python for Test Automation
You will need a few portion to run your Python-based automated tests:
Python 3
A code editor
Selenium ’ s WebDriver for Python
A driver placeholder
We will concenter on the macOS installment in this tutorial. Two ground are behind this choice: (1) macOS is quite popular among developers compared to Windows, (2) the installation is like to how you would have make it on Linux.
In effect, we & # x27; re taking care of two platforms at once.
Installation and Setup Steps
All these measure should be done in your command-line tool. On macOS, it is typically Terminal.
1. Download and install a codification editor. We enjoyVisual Studio, and we even indite another tutorial for C # programmer that explainshow to use WebDriver in C # and Optic Studio.
2. Python installing:
brewage install python
3. Selenium/WebDriver facility:
pip3 install selenium
In case it drop an error, try these commands:
curl -O https: //bootstrap.pypa.io/get-pip.pysudo python3 get-pip.py
4. Driver proxy installation:
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
Go to the leaning of browser drivers for WebDriver, find the download link of the up-to-the-minute release for your browser version, and save the ZIP file.
After you unzip the downloaded archive, you get a booklet with binary files. Those are extra executables that help Selenium talk with your browser. You can save the booklet anyplace on your PC, but then you will get to switch the current directory in your command tool every time you require to start the driver.
A more commodious way is to add the leaflet to your system ’ s PATH environmental variable so the binaries can be depart from any directory.
Create a directory and place the folder thither. It can be something like:
/opt/WebDriver/bin
Go to Terminal and eccentric in (for ChromeDriver):
$ export PATH= & quot; $ PATH: /path/to/chromedriver & quot;
The precise PATH variable-changing bid for early browser can be foundhere.
Close all active Terminal window.
Open a new one.
Type in an example binary file name:
chromedriver
If the message “ Starting ChromeDriver … ” appears, so you did everything aright.
Running the Tests
Selenium needs a .py file with the code playscript saved locally on your machine where it can access it. After creating such a script utilize the code editor of your choice, make certain it can establish a browser session habituate the ChromeDriver. This can be perform with the two simple lines that you need to put on top of your own codification:
from selenium.webdriver import Chrome
with Chrome () as driver:
# your codification depart here with an indent
We will wedge to the ChromeDriver example. If you had a fault in between, do the following:
Open a new Terminal window.
Type in:
chromedriver
Then go to the directory where you saved your .py file:
cd ~/users/user/automated_tests/test.py
Finally, execute the following command:
python3 test.py
This will part the trial. Breathe out, you ’ ve made it!
A Cloud-Based Testing Platform
To test complex applications or run a few exam in parallel, you need grave figure power. Moving your automated tests to the cloud helps your developers avoid scalability issues. This way, they can shift their aid to bug and performance alone.
In one of our Getting Started tutorial, you can learn about the Sauce Labs architecture and how it enables. If you demand to examine an application that runs on a private waiter and requires a placeholder, you can use our Sauce Connect Proxy. The frame-up is a zephyr, but if you require a elaborated explanation, you will find the certification.
For Python newbies, we shared an automated exam script that interact with our website in the Chrome browser. You can find it onour GitHub page. No need to vary it.
Conclusion
In this place, we show you how to work with the Selenium-Python combination. If you need a refresh or upgrade of your noesis of these languages, check out our complimentary guidebook. Then, start using theSauce Labs cloud solutionfor prove mechanization.
Share this situation
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free