Selenium Python Pytest | Learn in Just 06 Easy Steps – Devstringx
Why Selenium?
- Selenium is the best open-source tool widely used for Automation testing.
- Highly compatible with different IDES.
- Supports multiple OSs like Windows, Mac, Unix, Linux Etc.
- Supports multiple browsers like Chrome, Firefox, Safari, etc.
- Supports a wide range of languages like JAVA, Python, C#, JS, etc.
- Can implement for both mobile and web applications.
- Loaded selenium Suites.
- Easy to Implement.
- Reusability and Add-ons.
Why Python?
- Python works on different platforms like windows, Linux, Mac, etc.
- It has syntax similar to the English language.
- Using python we can achieve goals with fewer lines of code.
- Python runs on an interpreter system, which means that the code can execute as soon as it is written.
IDE (Integrated Development Environment)
The IDE we are going to use is PyCharm which was developed by JetBrains as an IDE for Python. Pycharm is a hybrid platform. This is because PyCharm exclusively designs for Python, and all of its plugins, extensions, and features focus on making Python Coding more efficient.
Steps to Setup Python
Here we share 06 easy steps to setup Python
1) Download Python
- Windows: http://python.org/download/.
Note: If you are using Linux, MacOS X, or Unix operating Systems then python will install by default with OS
2) Installation of the PIP tool
Pip is a package management system that is used to install and manage software packages written in Python pip is a repeating abbreviation that can stand for either “Pip Installs Packages” or “Pip Installs Python”
3) Selenium Installation
- Installation of Selenium
Use the Below command on PIP to install Selenium Package
- Pip install selenium
This command will add the Selenium WebDriver library on our device with all modules and classes that we need to create automated scripts using Python.
4) Path Setup in OS
- Next, we have to set Python Home in System Variables.
- Copy the path where python.exe is present.
- Search for Environment Variables and Edit it.
- Click Ok.
- Go to the location where Python install and go to scripts.
- Open it and check for PIP. Copy the file path. Paste it in Environment Variables.
- Click Ok.
- Open the command prompt where python.exe is present.
- Enter the python –version command and click enter.
5) IDE Setup (Installation of Pycharm Editor)
- Install PyCharm Editor.
- Open it and create a new project.
- Open in a new window.
- Click on the interpreter and point Python Interpreter to the Locally installed interpreter.
- Click on the interpreter and check if selenium is available and click ok.
6) Install Pytest
Py-Test is a python testing framework that originated from the PyPy project. It can use to write various software tests, including unit, integration, functional, and end-to-end tests. Its features include fixtures, parameterized testing, and assert rewriting.
It works similarly to TestNG and Junit.
To install PyTest: Open the command prompt and type pip install PyTest and click enter.
Locators Syntax
find_element(By.ID, “id”) find_element(By.Name, “name”) find_element(By.TAG_NAME, “tag name”) find_element(By.XPATH,”xpath”) find_element(By.CSS_SELECTOR,”css selector”)
Service is an inbuilt method in python in which we pass the driver location similar to setProperty() in java.
For Validation, We use the Assert Keyword
assert<condition> assert<condition>,<error message>
Below is a code in which we open amazon.com and search for the desired product and validate the title of the page and close it.
Note– If the code passes with no error, then the exit code should be 0 in the console else it should be 1.
Related Articles:-