From 413287fd67c62cd6ecba219e134d2fc87841b295 Mon Sep 17 00:00:00 2001 From: Spencer Phillip Young Date: Tue, 11 Dec 2018 18:47:08 -0800 Subject: [PATCH 1/2] Patch/test urls (#67) * open relative site instead of absolute url * remove hard-coded test url --- tests/features/baseUrl.feature | 4 ---- tests/features/baseUrlWithParameterTransformation.feature | 2 ++ tests/features/isExisting.feature | 2 +- tests/features/sampleSnippets.feature | 4 ---- tests/features/transformation_fixture.feature | 2 +- tests/features/wait.feature | 2 +- 6 files changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/features/baseUrl.feature b/tests/features/baseUrl.feature index d27a87c..edec2fe 100644 --- a/tests/features/baseUrl.feature +++ b/tests/features/baseUrl.feature @@ -2,10 +2,6 @@ Feature: Base URL configuration As a developer I should be able to change the base URL for opening pages. - Scenario: Default base is http://localhost:8000/ - When I open the site "/" - Then I expect that the url is "http://localhost:8000/" - Scenario: Change the base url to http://127.0.0.1:8000/ Given the base url is "http://127.0.0.1:8000/" When I open the site "/page.html" diff --git a/tests/features/baseUrlWithParameterTransformation.feature b/tests/features/baseUrlWithParameterTransformation.feature index 0193b64..b3c8f07 100644 --- a/tests/features/baseUrlWithParameterTransformation.feature +++ b/tests/features/baseUrlWithParameterTransformation.feature @@ -3,6 +3,8 @@ Feature: Base URL configuration supports parameter transformation I should be able to change the base URL for opening pages And I should be able to replace hardcoded values with parameters. + # {BASE_URL} / {ALT_BASE_URL} are transformed by transformer provided in environment.py + # As long the URLs are valid, this test should still work even if the test url is not available at those urls. Scenario: Default base is http://localhost:8000/ When I open the site "/" Then I expect that the url is "{BASE_URL}/" diff --git a/tests/features/isExisting.feature b/tests/features/isExisting.feature index b39a398..24d0a8f 100644 --- a/tests/features/isExisting.feature +++ b/tests/features/isExisting.feature @@ -4,6 +4,6 @@ Feature: Github test And check if some elements are existing and others are not Scenario: open URL - Given I open the url "https://github.com/webdriverio/cucumber-boilerplate" + Given I open the url "https://github.com/spyoungtech/behave-webdriver" Then I expect that element ".octicon-mark-github" does exist And I expect that element ".some-other-element" does not exist diff --git a/tests/features/sampleSnippets.feature b/tests/features/sampleSnippets.feature index b259595..594fdc5 100644 --- a/tests/features/sampleSnippets.feature +++ b/tests/features/sampleSnippets.feature @@ -2,7 +2,6 @@ Feature: Sample Snippets test As a developer I should be able to use given text snippets - #@Isolate Scenario: open URL Given the page url is not "http://webdriverjs.christian-bromann.com/" And I open the url "http://webdriverjs.christian-bromann.com/" @@ -146,7 +145,6 @@ Feature: Sample Snippets test And I expect that element ".red" is not 103px broad And I expect that element ".red" is not 103px tall - # For some reason this test is failing when running it in the Travis VM @Pending Scenario: check offset Given I open the url "http://webdriverjs.christian-bromann.com/" @@ -165,7 +163,6 @@ Feature: Sample Snippets test When I click on the element ".checkbox_notselected" Then I expect that checkbox ".checkbox_notselected" is checked - # This will fail in PhantoJS due to a security warning @Pending Scenario: set / read cookie Given I open the url "http://webdriverjs.christian-bromann.com/" @@ -175,7 +172,6 @@ Feature: Sample Snippets test And I expect that cookie "test" contains "test123" And I expect that cookie "test" not contains "test1234" - # This will fail in PhantoJS due to a security warning @Pending Scenario: delete cookie Given I open the url "http://webdriverjs.christian-bromann.com/" diff --git a/tests/features/transformation_fixture.feature b/tests/features/transformation_fixture.feature index 33d9d26..15e14fb 100644 --- a/tests/features/transformation_fixture.feature +++ b/tests/features/transformation_fixture.feature @@ -4,4 +4,4 @@ Feature: Using a transformation fixture from feature file Scenario: transform step from environment variable Given the base url is "{ENV_BASE_URL}" When I open the site "/page.html" - Then I expect that the url is "http://127.0.0.1:8000/page.html" \ No newline at end of file + Then I expect that the url is "{ENV_BASE_URL}page.html" diff --git a/tests/features/wait.feature b/tests/features/wait.feature index 57e2cf4..e883450 100644 --- a/tests/features/wait.feature +++ b/tests/features/wait.feature @@ -3,7 +3,7 @@ Feature: Test waiting for actions I want to be able to test if delayed actions are being performed Background: - Given I open the url "http://localhost:8000/" + Given I open the site "/" And I pause for 1000ms Scenario: Test if element becomes checked after 2000 ms From 9baca862574dd09bc521d4bd208700882879d0e2 Mon Sep 17 00:00:00 2001 From: Spencer Young Date: Tue, 11 Dec 2018 18:48:52 -0800 Subject: [PATCH 2/2] remote driver & docker --- Dockerfile | 14 ++++++++++++++ docker-compose.yml | 36 +++++++++++++++++++++++++++++++++++ tests/features/environment.py | 17 ++++++++++++++--- 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..aabd286 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.7 + +COPY . . +RUN pip install --no-cache-dir -r ./requirements.txt +RUN pip install --no-cache-dir pytest mock + +ENV BEHAVE_WEBDRIVER=Remote +ENV HUB_URL=http://localhost:4444/wd/hub +ENV DEMO_URL=http://demo-site +ENV ENV_BASE_URL=http://demo-site + +ENV CAPS="Chrome" + +CMD behave tests/features diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..fe143d6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3.7' + +services: + hub: + image: selenium/hub:3.141.59-copernicium + ports: + - "4444:4444" + + chrome: + image: selenium/node-chrome:3.141.59-copernicium + environment: + HUB_HOST: hub + HUB_PORT: 4444 + deploy: + replicas: 1 + entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME" /opt/bin/entry_point.sh' + depends_on: + - hub + - demo-site + + firefox: + image: selenium/node-firefox:3.141.59-copernicium + environment: + HUB_HOST: hub + HUB_PORT: 4444 + deploy: + replicas: 1 + entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME" /opt/bin/entry_point.sh' + depends_on: + - hub + - demo-site + + demo-site: + image: nginx + volumes: + - ./tests/demo-app:/usr/share/nginx/html:ro diff --git a/tests/features/environment.py b/tests/features/environment.py index ace88f4..0ce4540 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -8,6 +8,7 @@ from behave_webdriver.driver import Chrome, ChromeOptions from functools import partial from behave_webdriver.fixtures import transformation_fixture, fixture_browser +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from behave_webdriver.transformers import FormatTransformer from behave.fixture import use_fixture import behave_webdriver @@ -17,7 +18,12 @@ def get_driver(**kwargs): args = [] kwargs.setdefault('default_wait', 5) Driver = behave_webdriver.utils._from_env(default_driver='Chrome') - if Driver == behave_webdriver.Chrome: + + if Driver == behave_webdriver.Remote: + caps_name = os.environ.get('CAPS').upper() + caps = getattr(DesiredCapabilities, caps_name).copy() + + elif Driver == behave_webdriver.Chrome: opts = ChromeOptions() opts.add_argument('--no-sandbox') # for travis build kwargs['chrome_options'] = opts @@ -28,14 +34,19 @@ def get_driver(**kwargs): else: ex_path = shutil.which(Driver._driver_name) or pwd_driver_path kwargs['executable_path'] = ex_path + if Driver == behave_webdriver.Remote: + del kwargs['executable_path'] + kwargs['command_executor'] = os.environ.get('HUB_URL') or "http://hub:4444/wd/hub" + kwargs['desired_capabilities'] = caps if os.environ.get('BEHAVE_WEBDRIVER_HEADLESS', None) and hasattr(Driver, 'headless'): Driver = Driver.headless return Driver, kwargs - #context.behave_driver = context.BehaveDriver() def before_all(context): driver, kwargs = get_driver() + if driver == behave_webdriver.Remote: + context.base_url = os.environ.get('DEMO_URL') or 'http://demo-site' context.BehaveDriver = partial(driver, **kwargs) use_fixture(fixture_browser, context, webdriver=driver, **kwargs) use_fixture(transformation_fixture, context, FormatTransformer, BASE_URL='http://localhost:8000', ALT_BASE_URL='http://127.0.0.1:8000') @@ -46,7 +57,7 @@ def before_tag(context, tag): def before_feature(context, feature): - if "fresh_driver" in feature.tags: + if "fresh_driver" in feature.tags and context.behave_driver.__class__ != behave_webdriver.Remote: context.behave_driver.quit() context.behave_driver = context.BehaveDriver() context.behave_driver.default_wait = 5