Skip to main content

_python

TL;DR - Show Me The Code

1-upload-a-file/code/python/upload.py
import os
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
def driver_setup_teardown():
driver = webdriver.Firefox()
yield driver
driver.quit()

def test_file_upload(driver_setup_teardown):
driver = driver_setup_teardown
filename = 'some-file.txt'
file = os.path.join(os.getcwd(), filename)
driver.get('https://the-internet.herokuapp.com/upload')
driver.find_element(By.ID, 'file-upload').send_keys(file)
driver.find_element(By.ID, 'file-submit').click()
uploaded_file = driver.find_element(By.ID, 'uploaded-files').text
assert uploaded_file == filename, "uploaded file should be %s" % filename

Code Walkthrough

Importing Libraries

Lines 1 to 4 are pulling in our requisite libraries for interacting with the operating system (e.g., import os), our testing framework (e.g., import pytest), driving the browser with Selenium (e.g., from selenium import webdriver), and a locator strategy to find the elements (e.g. from selenium.webdriver.common.by import By).

Setup and Teardown

Lines 6 to 10 are setting up and tearing down the browser instance. The @pytest.fixture decorator is used to create a fixture that sets up and tears down the browser. The yield keyword is used to pause the fixture until the test is completed. After the test is completed, the fixture resumes and executes the teardown code (in this case, driver.quit()).

The Test

Lines 12 to 20 are the test itself.

The fixture that sets up and tears down the browser is used as an argument in the test test_file_upload(driver_setup_teardown), which allows the test to have access to the browser provided by the fixture.

After declaring the test method (e.g., def test_file_upload(driver_setup_teardown):) we store driver_setup_teardown in a local variable (e.g., driver = driver_setup_teardown). This way we don't have to litter our test method with driver_setup_teardown and can call driver instead. Next we specify the file we'd like to upload. There's already a text file called some-file.txt in the current working directory, so we grab both the file name and its full path, storing both of these values in variables (e.g., filename and file respectively).

Next we visit the page with the upload form, input the string value of file (e.g., the full path to the file plus the filename with its extension), and submit the form. After the file is uploaded to the page it will display the filename it just processed. We use this text on the page to perform our assertion (making sure the uploaded file is what we expect).

Executing the Test

Before executing the test, we need to install the required libraries. We can do this by running pip install -r requirements.txt from the command-line. The requirements.txt file contains the libraries we need to install.

Toggle to see the requirements.txt file.
1-upload-a-file/code/python/requirements.txt
selenium==4.26.1
pytest

After installing the required libraries, we can run the test by executing pytest upload.py from the command-line.