Automation Testing Tutorial Using Pycharm

Aisha Lugg
Strategio
Published in
4 min readFeb 18, 2022
www.freepik.com/free-vector

Pycharm is an integrated development environment used explicitly for the Python programming language. In Software development, testing is crucial. Test Automation is the practice of running tests automatically, managing test data, and taking the results to improve the quality of the software. I will show you how to use Pycharm for Test Automation using Selenium.

Prerequisites

LET'S BEGIN

Step 1:

Head over to Pycharm and create a new project.

Step 2 :

Install ChromeDriver

After installing ChromeDriver on your local machine, unzip the file by double-clicking it in your downloads. Once you have ChromeDriver, make a folder for it in the Pycharm project.

mkdir driver

Drag ChromeDriver from your downloads file and into the folder drive on Pycharm.

Step 3:

Use the terminal in Pycharm to Install Selenium. Selenium is used for browsers for web applications. I am using Mac OS, so the command I used is below.

pip install selenium

You can check if Selenium is installed in Pycharm by going into venv →lib →python 3.9. Then, you can view the packages.

Step 4:

Create a .py file using your terminal on Pycharm. Make sure you are in your project directory. Copy the command below.

touch example.py

Tip: You can create a .py file by right-clicking on the project directory →New → Python File. This makes things a little easier, you don’t have to write .py when you name your file.

Step 5:

You will now begin to test by using the automation practice site. The Selenium Framework maintains this site. When using Selenium, there are a lot of realistic user experiences, and it allows the user to test out user interactions that rely on the modified state.

We will build a test case that automates input and submits it to the team on the Contact Us page. We can test the contact page because it is frequently used and holds data; it should run efficiently.

First, make sure the site will run

from selenium import webdriver# execute the path where you stored your chromedriver
driver = webdriver.Chrome(executable_path='driver/chromedriver')

#Opens URL an maximize window
driver.get("http://automationpractice.com/index.php")
driver.maximize_window()

If this works, move on.

If you get an error message that says, “ChromeDriver cannot be opened because the developer cannot be verified. Unable to launch the chrome browser” click here to solve your problem.

Tip: Take a look around the site and inspect the page. Go into the ‘Contact Us’ and inspect each element. You can view the elements class, id, XPath, and more. Maybe, get a little familar with “Contact Us” before you reach step 6.

Step 6:

Replace the code in your file with the code below.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import time
# execute the path where you stored your chromedriver
driver = webdriver.Chrome(executable_path='driver/chromedriver')

# Opens the URL to maximize the window driver.get("http://automationpractice.com/index.php")
driver.maximize_window()

# Finds element by xpath
contact=driver.find_element_by_xpath('//a[text() ="Contact us"]')
#clicks on "Contact Us"
contact.click()
# Finds element by id.
dropdown=driver.find_element(By.ID, 'id_contact')
# indicate where you are selecting from
drop = Select(dropdown)
# use index to select from the drop down. In this case, by selecting 1, you will be selecting the option in the drop down.
drop.select_by_index(1)
# Finds element by id.
email_input=driver.find_element(By.ID, 'email')
# Inputs email in textfield.
email_input.send_keys('testing@gmail.com')
# Finds element by id.
order=driver.find_element(By.ID, 'id_order')
# input order reference into textfield.
order.send_keys('23456789')
# Finds element by id.
mssge_input=driver.find_element(By.ID, 'message')
# input message into textfield.
mssge_input.send_keys('Hello, Congratulations. You did it ! ')
#Hault the code execution
time.sleep(2)

submit=driver.find_element(By.ID, 'submitMessage').click()

You will see a successful message once the automation is complete.

It’s time to end this!

One last thing! Let's add a code to quit chrome.

driver.quit()
https://www.freepik.com/vectors/people People vector created by storyset

YESSSS! You completed the simple steps of test automation! I hope this was helpful. If you are interested in knowing more test cases, you can see a few cases by clicking here. You can also click here to view information on locating elements.

❁ I hope you enjoyed this tutorial. Feel free to provide feedback and follow me for more. ❁

--

--

Aisha Lugg
Strategio

Hi there, I love learning about technology and sharing what I have learned with others. I also love listening to music and bike riding.