Web Automation Using Python Selenium. |
This tutorial will make web UI testing easy. we'll build an easy yet robust web UI test solution using Python, pytest, and Selenium WebDriver. we'll learn strategies permanently test design also as patterns permanently automation code. By the top of the tutorial, you’ll be an internet test automation champ! Your Python test project is often the inspiration for your own test cases, too.
WebDriver for Chrome Browser
WebDriver is an open-source tool for automated testing of web apps across many browsers. It provides capabilities for navigating to sites, user input, JavaScript execution, and more. ChromeDriver may be a standalone server that implements the W3C WebDriver standard. ChromeDriver is out there for Chrome on Android and Chrome on Desktop (Mac, Linux, Windows and ChromeOS).
If you are using windows operating system then you have to download WebDriver for Windows.
Installation of Selenium WebDriver
For our test project, we'll use Selenium WebDriver’s Python bindings with Google Chrome and ChromeDriver. We could use any browser, but let’s use Chrome because (a) it's a really high market share and (b) its Developer Tools will are available handy later.
After downloading the WebDriver for Chrome using the download button provided. Then, you have to extract the file. After extracting the file you will find an application called chromedriver.exe. Run that application and installed it.
Verify that ChromeDriver works from the command line:
$ chromedriver
Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
You can download the Python bindings for Selenium from the PyPI page for selenium package. However, a far better approach would be to use pip to put in the selenium package. Python 3.6 has pip available within the standard library. Using pip, you'll install selenium like this:
pip install seleniumFor Python 3 you can install given as below.
pip3 install selenium
Write your First Web Test
With WebDriver ready to go, let’s write our first web test! The test will be a simple Facebook login. Facebook is a Social Media platform. Users can enter Email and password in one file and import that file using the import function in python and it will redirect to your Facebook home page, just like you do it regularly.
Here I'm giving you sample code for practice purpose only.
The first file is myfb.py
from selenium import webdriverRemember, your executable_path is the location where you have downloaded chrome web driver and in a browser.get(enter the URL you want to test).
browser = webdriver.Chrome(executable_path='C:\Users\Rahul\Downloads\ChromeWebDriver\chromedriver.exe')
browser.get('https://www.facebook.com/')
The second file for credential.py
def getemail():The third file is automate.py
return 'youremailaddress@xyz.com'
def getpassword():
return 'yourpassword'
from selenium import webdriverTo fill user input value into webpage input text field you have to inspect the webpage and find out what data variable used in that webpage and associate that data variables with the variable that we defined in the program.
import credentials
email = credentials.getemail()
pass = cedentials.getpassword()
browser = webdriver.Chrome(executable_path='C:\Users\Rahul\Downloads\ChromeWebDriver\chromedriver.exe')
def init():
browser.get('https://wwww.facebook.com/')
def login():
browser.find_element_by_id('email').send_keys(email)
browser.find_element_by_id('pass').send_keys(pass)
browser.find_element_by_id('loginbutton').click()
init()
login()
2 Comments
Working good👍👍.
ReplyDeleteThankyou for provide good information
Helpful information thanks ✌
ReplyDelete