diff --git a/.gitignore b/.gitignore index 92fab75..9c4f314 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ **/.cache **/__pycache__ -**/.vscode \ No newline at end of file +**/.vscode + +.pytest_appium.egg-info \ No newline at end of file diff --git a/pytest_appium/_utils.py b/pytest_appium/_utils.py index af059cf..5404dd0 100644 --- a/pytest_appium/_utils.py +++ b/pytest_appium/_utils.py @@ -1,6 +1,7 @@ import json import urllib #.request import time +import requests def _decode_response(response): @@ -12,8 +13,9 @@ def _decode_response(response): return json.loads(data.decode(encoding)) -def get_json(url): - return _decode_response(urllib.request.urlopen(url)) +def get_json(url, headers: dict=None): + r = requests.get(url, headers=headers) + return r.json() def post_json(url, data): diff --git a/pytest_appium/conftest.py b/pytest_appium/conftest.py index 618ab49..5132c65 100644 --- a/pytest_appium/conftest.py +++ b/pytest_appium/conftest.py @@ -49,13 +49,12 @@ def session_capabilities(request, variables): return capabilities -@pytest.fixture def driver_kwargs(request, capabilities): """ """ # Assertions of capabilitys should go here #appium_user = f'{0.appium_username}:{0.appium_access_key}@' kwargs = dict( - command_executor='http://{0.appium_host}:{0.appium_port}/wd/hub'.format(request.config.option), + command_executor='http://{0.appium_host}:{0.appium_port}'.format(request.config.option), desired_capabilities=capabilities, browser_profile=None, proxy=None, @@ -64,13 +63,15 @@ def driver_kwargs(request, capabilities): return kwargs -@pytest.fixture def driver_class(request): """Appium driver class""" return webdriver.Remote +@pytest.fixture +def driver_class_fixture(request): + return driver_class(request) + -@pytest.yield_fixture def driver(request, driver_class, driver_kwargs): """Returns a AppiumDriver instance based on options and capabilities""" driver = driver_class(**driver_kwargs) @@ -137,7 +138,7 @@ def _appium_is_device_available(appium_wd_api_endpoint, desiredCapabilities={}, #'ios_device_available': appium_is_device_available_ios, } -@pytest.yield_fixture(scope='session') +@pytest.fixture(scope='session') def driver_session_(request, session_capabilities): """ Appium Session @@ -176,7 +177,7 @@ def wait_for_appium(): raise Exception(f"""Unable to connect to Appium server {appium_url}""") -@pytest.yield_fixture +@pytest.fixture def driver_session(request, driver_session_): """ Appium Session @@ -186,7 +187,7 @@ def driver_session(request, driver_session_): #driver_session_.reset() -@pytest.yield_fixture +@pytest.fixture def appium(driver_session): """Alias for driver_session""" yield driver_session @@ -213,7 +214,7 @@ def test_example(): # Filter tests that are not targeted for this platform current_platform = config._variables.get('capabilities',{}).get('platformName','').lower() def select_test(item): - platform_marker = item.get_marker("platform") + platform_marker = item.get_closest_marker("platform") if platform_marker and platform_marker.args and current_platform: test_platform_specified = platform_marker.args[0].lower() if test_platform_specified != current_platform: diff --git a/pytest_appium/html_reporting.py b/pytest_appium/html_reporting.py index ec5b66c..55e951a 100644 --- a/pytest_appium/html_reporting.py +++ b/pytest_appium/html_reporting.py @@ -32,6 +32,17 @@ def _gather_screenshot(item, report, driver, summary, extra): # add screenshot to the html report extra.append(pytest_html.extras.image(screenshot, 'Screenshot')) +def _gather_video(item, report, driver, summary, extra): + try: + video = driver.stop_recording_screen() + except Exception as e: + summary.append('WARNING: Failed to gather video: {0}'.format(e)) + return + pytest_html = item.config.pluginmanager.getplugin('html') + if video and pytest_html is not None: + # add screenshot to the html report + extra.append(pytest_html.extras.mp4(video, 'Video')) + def _gather_page_source(item, report, driver, summary, extra): try: @@ -117,10 +128,14 @@ def pytest_runtest_makereport(self, item, call): _gather_app_strings(item, report, driver, summary, extra) if 'screenshot' not in exclude: _gather_screenshot(item, report, driver, summary, extra) + if 'video' not in exclude: + _gather_video(item, report, driver, summary, extra) if 'page_source' not in exclude: _gather_page_source(item, report, driver, summary, extra) if 'logs' not in exclude: - _gather_logs(item, report, driver, summary, extra) + pass # log gathering on Android is very time-consuming, + # and seems to fail on Jenkins anyway + # _gather_logs(item, report, driver, summary, extra) item.config.hook.pytest_appium_capture_debug(item=item, report=report, extra=extra) item.config.hook.pytest_appium_runtest_makereport(item=item, report=report, summary=summary, extra=extra) if summary: