E2E automated testing with Python and Selenium

E2E automated testing with Python and Selenium

Hello! I'm Takahashi, I joined the development department just recently.
It's been about two weeks since we started our automation work. Every time I go forward, I hit a wall, and I am struggling every day.
In this article.Python and SeleniumI will write about the environment construction and basic usage to operate Chrome using

Click here for table of contents

  • 1. what is Selenium?
  • 2. environment construction
  • 3. let's actually move it!

What is Selenium?

Selenium is a browser automation tool. It allows you to test the behavior of your website by automatically manipulating the browser.
Screen operations such as clicking, keyboard input, and screen information acquisition can be performed automatically.

▼ According to the official Selenium.dev website (English only)

Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that.

Boring web-based administration tasks can (and should) also be automated as well.

Official Website:.selenium

and is the de facto standard tool for test automation of web applications.

In addition to testing, the main uses of the software are task automation and website crawling.
It also works well with Python, which seems to be a standard combination for those who don't have much programming experience but want to automate tests.

environment creation

In order to use Selenium to automatically manipulate the browser, you need to install the following

  • web browser
      ┗Chrome, Firefox, IE, Opera etc.
  • WebDriver
      A module that exposes an API for manipulating the┗ browser.
  • Selenium
      A library that communicates with the┗ WebDriver and manipulates the browser from the program

In this section, I'll show you how to create an environment for using Selenium with Python directly on your local PC.

Build the environment locally

This section describes how to set up an environment for running Selenium locally on Windows.

This is the configuration for this method.

note (supplementary information) symbolFrom Selenium official website

The browser and WebDriver are all running locally, and Selenium connects to the local driver.

Check your Chrome version

To decide which version of WebDirver to install, check the version of Chrome you have installed. In my environment 87.0.4280.66 had been installed.

Install WebDriver

Download the WebDriver binary for Chrome.
Go to the following site and download the binary of the driver closest to your browser version

https://github.com/danielkaiser/python-chromedriver-binary

Install the Python Selenium Binding

The Python Selenium bindings are installed with pip.

$ pip install selenium

I'll move it.

In a local environment, you can run Selenium with code like the following

Import #webdriver
from selenium import webdriver

1Configure the options of TP3T WebDriver
options = webdriver.ChromeOptions()
options.add_argument('--headless'.)

print('connectiong to remote browser...')
Chrome = webdriver.Chrome(executable_path=r "your/path/to/chromedriver.exe")
driver = Chrome

driver.get('https://python.org')
print(driver.current_url)

# Close the browser
driver.quit()

Running the above code will launch Chrome on your PC.
options.add_argument('--headless') part is commented out, you can see the browser screen and confirm that it is working.

Basic usage

Now that we're ready to build an environment for using Selenium, let's see how we'll actually use it.

What I want to do.

Now, let's run Chrome with Selenium and do the following

  1. Accessing the toppage search bar on Python.org
    https://pyhton.org
  2. Type "list" in the search bar and click the "Go" button
  3. Learn the "list comprehension" link on the destination page.

Running Selenium in Python

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

Define the driver by setting the Path of #driver.
Chrome = webdriver.Chrome(executable_path=r "your/path/to/chromedriver.exe")
driver = Chrome

# This time I commented it out because I want to see the behavior on a real browser.
# This allows you to run the test without launching a browser
# options = webdriver.ChromeOptions()
# options.add_argument('--headless')

# 1. Go to the Python.org home page and maximize your browser
driver.get('https://pyhton.org')
driver.maximize_window()
print(driver.current_url)
# > https://pyhton.org

# 2. Find the search field and click on the field. Clear the field and type "list", then click "GO" and do nothing for 1 second until you get to the next page.
search_field = driver.find_element(By.ID, 'id="id-search-field"')
search_field.click()
search_field.clear()
search_field.send_keys("list".)
go_button = driver.find_element(By.ID, 'id="submit"')
go_button.click()
time.sleep(1)
print(driver.current_url)
# > https://www.python.org/search/?q=list&submit=


# 3. get the URL in the title of the list comprehensions
list_comprehension_link = driver.find_elements(By.XPATH, "//ul[@class='list-recent-events menu']/li[13]//a")
print(list_comprehension_link.get_attribute('href'.))
# > https://www.python.org/dev/peps/pep-0202/

# x. Exit the browser
driver.quit()

The above is a brief overview.

In my next post, I'll explain the details of the code I wrote above.

en_USEnglish