Skip to content

Commit

Permalink
Add test for app percy + automate
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmay-browserstack committed Jan 29, 2024
1 parent 048bf36 commit 4ea05b8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
5 changes: 2 additions & 3 deletions percy/lib/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ def post_failed_event(self, error):
log(e, on_debug=True)
return None

def post_poa_screenshots(self, name, session_id, command_executor_url, capabilities, desired_capabilities, options=None, sync=None):
def post_poa_screenshots(self, name, session_id, command_executor_url, capabilities, desired_capabilities, options=None):
body = {
'sessionId': session_id,
'commandExecutorUrl': command_executor_url,
'capabilities': dict(capabilities),
'sessionCapabilites':dict(desired_capabilities),
'snapshotName': name,
'options': options,
'sync': sync
'options': options
}

body['client_info'] = Environment._get_client_info()
Expand Down
5 changes: 1 addition & 4 deletions percy/lib/percy_automate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
IGNORE_ELEMENT_ALT_KEY = 'ignoreRegionAppiumElements'
CONSIDER_ELEMENT_KEY = 'consider_region_appium_elements'
CONSIDER_ELEMENT_ALT_KEY = 'considerRegionAppiumElements'
SYNC = 'sync'

class PercyOnAutomate:
def __init__(self, driver):
Expand Down Expand Up @@ -40,18 +39,16 @@ def screenshot(self, name: str, **kwargs):

ignore_region_elements = [element.id for element in options.get(IGNORE_ELEMENT_KEY, [])]
consider_region_elements = [element.id for element in options.get(CONSIDER_ELEMENT_KEY, [])]
sync = options.get(SYNC, None)
options.pop(IGNORE_ELEMENT_KEY, None)
options.pop(CONSIDER_ELEMENT_KEY, None)
options.pop(SYNC, None)

return CLIWrapper().post_poa_screenshots(
name,
metadata.session_id,
metadata.command_executor_url,
metadata.capabilities,
metadata.session_capabilities,
{ **options, "ignore_region_elements": ignore_region_elements, "consider_region_elements" : consider_region_elements, "sync": sync }
{ **options, "ignore_region_elements": ignore_region_elements, "consider_region_elements" : consider_region_elements }
)
except Exception as e:
log(f'Could not take Screenshot "{name}"')
Expand Down
19 changes: 19 additions & 0 deletions tests/test_app_percy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class TestAppPercy(unittest.TestCase):
comparison_response = {'link': 'https://snapshot_url', 'success': True}
sync_response = {'link': 'https://snapshot_url', 'data': 'sync-data', 'success': True}

@patch('appium.webdriver.webdriver.WebDriver')
def setUp(self, mock_webdriver):
Expand Down Expand Up @@ -171,3 +172,21 @@ def test_invalid_driver(self):
with self.assertRaises(Exception) as e:
AppPercy(Mock())
self.assertIsInstance(e.exception, DriverNotSupported)

@patch.object(CLIWrapper, 'post_screenshots', MagicMock(return_value=sync_response))
@patch.object(GenericProvider, '_write_screenshot', MagicMock(return_value='path-to-png-file'))
@patch.object(AppAutomate, 'execute_percy_screenshot_begin', MagicMock(return_value={'deviceName': 'Google Pixel 4',
'osVersion': '12.0',
'buildHash': 'abc',
'sessionHash': 'def'
}))
@patch.object(AppAutomate, 'execute_percy_screenshot_end', MagicMock(return_value=None))
@patch.object(Metadata, 'session_id', PropertyMock(return_value='unique_session_id'))
@patch.dict(os.environ, {"PERCY_DISABLE_REMOTE_UPLOADS": "true"})
def test_android_on_app_automate(self):
with patch('percy.metadata.AndroidMetadata.remote_url', new_callable=PropertyMock) as mock_remote_url:
mock_remote_url.return_value = 'url-of-browserstack-cloud'
app_percy = AppPercy(self.mock_android_webdriver)
self.assertEqual(app_percy.screenshot('screenshot 1', sync = True), 'sync-data')
self.assertTrue(isinstance(app_percy.metadata, AndroidMetadata))
self.assertTrue(isinstance(app_percy.provider, AppAutomate))
22 changes: 18 additions & 4 deletions tests/test_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def mock_screenshot(fail=False):
status=(500 if fail else 200),
)

def mock_poa_screenshot(fail=False):
def mock_poa_screenshot(fail=False, sync=False):
body = '{ "success": ' + ("true" if not fail else 'false, "error": "test"') + "}"
if sync: body = '{ "success": "true", "data": "sync-data" }'
httpretty.register_uri(
httpretty.POST,
"http://localhost:5338/percy/automateScreenshot",
body=(
'{ "success": ' + ("true" if not fail else 'false, "error": "test"') + "}"
),
body=body,
status=(500 if fail else 200),
)

Expand Down Expand Up @@ -287,5 +287,19 @@ def test_posts_screenshot_poa(self):
self.assertEqual(s2['options']['ignore_region_elements'], ['Dummy_id'])
self.assertEqual(s2['options']['consider_region_elements'], ['Consider_Dummy_id'])

def test_posts_screenshot_poa_with_sync(self):
mock_healthcheck(type="automate")
mock_poa_screenshot(False, True)

driver = Mock(spec=WebDriver)
driver.session_id = 'Dummy_session_id'
driver.capabilities = { 'key': 'value' }
driver.desired_capabilities = { 'key': 'value' }
driver.command_executor = Mock()
driver.command_executor._url = 'https://hub-cloud.browserstack.com/wd/hub'
self.mock_webdriver.capabilities = { 'key': 'value' }

self.assertEqual(percy_screenshot(driver, 'Snapshot 3', options = {'sync': True}), 'sync-data')

if __name__ == "__main__":
unittest.main()

0 comments on commit 4ea05b8

Please sign in to comment.