From 26204382845c2bd023fee2dcca757bad0cc0b2b2 Mon Sep 17 00:00:00 2001 From: Svetlana Levinsohn Date: Sat, 8 Apr 2023 12:25:25 -0700 Subject: [PATCH] Add Docker support --- features/environment.py | 49 +++++++++++++++++++------------------- pages/base_page.py | 2 ++ run_selenium_tests.sh | 34 ++++++++++++++++++++++++++ webtests_chrome.Dockerfile | 37 ++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 25 deletions(-) create mode 100755 run_selenium_tests.sh create mode 100644 webtests_chrome.Dockerfile diff --git a/features/environment.py b/features/environment.py index 6c9c409a..810c240d 100755 --- a/features/environment.py +++ b/features/environment.py @@ -18,7 +18,7 @@ def browser_init(context, test_name): :param context: Behave context :param test_name: scenario.name """ - # service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') + # service = Service('./chromedriver') # service = Service('/Users/svetlanalevinsohn/JobEasy/12-python-selenium-automation/geckodriver') # context.driver = webdriver.Chrome(service=service) # context.driver = webdriver.Firefox(service=service) @@ -31,31 +31,30 @@ def browser_init(context, test_name): # chrome_options=options, # service=service # ) + chrome_options = webdriver.ChromeOptions() + chrome_options.add_argument("--no-sandbox") + chrome_options.add_argument("window-size=1400,2100") + chrome_options.add_argument('--disable-gpu') + chrome_options.add_argument('--disable-dev-shm-usage') + chrome_options.add_argument('--headless') - ### EventFiringWebDriver - log file ### - ### for drivers ### - # context.driver = EventFiringWebDriver( - # webdriver.Chrome(service=service), - # MyListener() - # ) - # for headless mode ### - # context.driver = EventFiringWebDriver(webdriver.Chrome(chrome_options = options), MyListener()) + context.driver = webdriver.Chrome(chrome_options=chrome_options) # for browerstack ### # Register for BrowserStack, then grab it from https://www.browserstack.com/accounts/settings - bs_user = '' - bs_key = '' - - desired_cap = { - 'browserName': 'Firefox', - 'bstack:options': { - 'os': 'Windows', - 'osVersion': '10', - 'sessionName': test_name - } - } - url = f'http://{bs_user}:{bs_key}@hub-cloud.browserstack.com/wd/hub' - context.driver = webdriver.Remote(url, desired_capabilities=desired_cap) + # bs_user = '' + # bs_key = '' + + # desired_cap = { + # 'browserName': 'Firefox', + # 'bstack:options': { + # 'os': 'Windows', + # 'osVersion': '10', + # 'sessionName': test_name + # } + # } + # url = f'http://{bs_user}:{bs_key}@hub-cloud.browserstack.com/wd/hub' + # context.driver = webdriver.Remote(url, desired_capabilities=desired_cap) context.driver.maximize_window() context.driver.implicitly_wait(5) @@ -65,13 +64,13 @@ def browser_init(context, test_name): def before_scenario(context, scenario): - # print('\nStarted scenario: ', scenario.name) + print('\nStarted scenario: ', scenario.name) logger.info(f'Started scenario: {scenario.name}') browser_init(context, scenario.name) def before_step(context, step): - # print('\nStarted step: ', step) + print('\nStarted step: ', step) logger.info(f'Started step: {step}') @@ -85,5 +84,5 @@ def after_step(context, step): def after_scenario(context, feature): - context.driver.delete_all_cookies() + # context.driver.delete_all_cookies() context.driver.quit() diff --git a/pages/base_page.py b/pages/base_page.py index e74a280b..d5359049 100644 --- a/pages/base_page.py +++ b/pages/base_page.py @@ -1,6 +1,7 @@ from support.logger import logger from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from time import sleep class Page: @@ -12,6 +13,7 @@ def __init__(self, driver): def open_url(self, url): self.driver.get(url) + sleep(2) def find_element(self, *locator): return self.driver.find_element(*locator) diff --git a/run_selenium_tests.sh b/run_selenium_tests.sh new file mode 100755 index 00000000..c84b4b78 --- /dev/null +++ b/run_selenium_tests.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Set the BASE_DIR variable to the current working directory +# (this is how you set variables in shell) +BASE_DIR=$(pwd) +# Create allure-test-results folder for allure files +mkdir -p $BASE_DIR/allure-test-results + +# Install all dependencies from requirements.txt +pip3 install --no-cache-dir -r requirements.txt + +# Run the tests: +behave -f allure_behave.formatter:AllureFormatter -o results/ features/tests/bestsellers.feature + +# This line sets the EXIT_CODE variable to the exit status of the previous command. +EXIT_CODE=$? + +# Generate allure report: +allure generate -c ./results/ -o $BASE_DIR/allure-test-results +sleep 10 # wait for report to generate + +# Compress allure report to allure-results.tgz file +cd $BASE_DIR/allure-test-results && tar czvf $BASE_DIR/allure-test-results/allure-results.tgz * + +# Send allure-results.tgz to the cloud +# This is OPTIONAL! You'll need to set up your cloud storage first, +# for example via google: https://stackoverflow.com/a/75541409/7746992 +#curl -X POST -T $BASE_DIR/allure-test-results/allure-results.tgz \ +# -H "Authorization: Bearer YOUR_TOKEN" \ +# -H "Content-Type: application/json" \ +# "https://storage.googleapis.com/upload/storage/v1/b/lana-storage/o?uploadType=media&name=allure-results.tgz" + +# Exit the script with the exit status of the previous command +exit $EXIT_CODE diff --git a/webtests_chrome.Dockerfile b/webtests_chrome.Dockerfile new file mode 100644 index 00000000..688fd68c --- /dev/null +++ b/webtests_chrome.Dockerfile @@ -0,0 +1,37 @@ +# Set the base image for this Dockerfile to the official selenium/standalone-chrome image, +# which contains a standalone Selenium server and Chrome browser. +FROM selenium/standalone-chrome + +# Change the user to root (gives admin privileges) +USER root + +# The WORKDIR instruction in a Dockerfile sets the working directory. +# This means that any file operations or commands that are executed in the container +# will be relative to the directory specified by the WORKDIR instruction. +# Any subsequent RUN, CMD, COPY, etc. instructions +# will be executed in the //usr/src/13-python-selenium-automation directory. +WORKDIR /usr/src/13-python-selenium-automation + +# Install python3-pip package +RUN apt-get -y update +RUN apt-get -y install -y software-properties-common +RUN apt-get -y install python3-pip + +# Download Allure commandline from GitHub and install it +RUN curl -o allure-2.14.0.tgz -Ls https://github.com/allure-framework/allure2/releases/download/2.14.0/allure-2.14.0.tgz +RUN tar -zxvf allure-2.14.0.tgz -C /opt/ +RUN ln -s /opt/allure-2.14.0/bin/allure /usr/bin/allure +# Verify Allure installed +RUN allure --version + +# Copy all the files from the current directory (the directory containing the Dockerfile) +# to the working directory inside the container. +COPY . . + +# chmod +x command makes the run_selenium_tests.sh file executable +# (it changes the permission of a file) +RUN chmod +x ./run_selenium_tests.sh + +# CMD command specifies the command to run when a container is started, +# which in this case is ./run_selenium_tests.sh. +CMD ["./run_selenium_tests.sh"]