Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 70a2e9d

Browse files
authored
Refactor core (#12)
Refactor AgentConnector to ServerConnector
1 parent 13f1fd3 commit 70a2e9d

File tree

12 files changed

+386
-143
lines changed

12 files changed

+386
-143
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ repos:
3131
- id: trailing-whitespace
3232
- id: end-of-file-fixer
3333
- id: debug-statements
34+
- id: mixed-line-ending
35+
fix: lr

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ addons:
77
- google-chrome-stable
88
matrix:
99
include:
10+
- python: 2.7
11+
env:
12+
- TOX_ENV=core
13+
- python: 3.5
14+
env:
15+
- TOX_ENV=core
1016
- python: 2.7
1117
env:
1218
- TOX_ENV=images

eyes_core/applitools/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .scaling import * # noqa
88
from .eyes_base import * # noqa
99
from .geometry import * # noqa
10-
from .agent_connector import AgentConnector # noqa
10+
from .server_connector import ServerConnector # noqa
1111
from .positioning import * # noqa
1212
from .match import * # noqa
1313
from .metadata import * # noqa
@@ -25,5 +25,5 @@
2525
+ positioning.__all__ # noqa
2626
+ match.__all__ # noqa
2727
+ metadata.__all__ # noqa
28-
+ ("logger", "AgentConnector") # noqa
28+
+ ("logger", "ServerConnector") # noqa
2929
)

eyes_core/applitools/core/eyes_base.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import typing as tp
66

77
from . import logger
8-
from .utils import ABC
8+
from .errors import DiffsFoundError, EyesError, NewTestError, TestFailedError
99
from .match import ImageMatchSettings
10-
from .metadata import BatchInfo
11-
from .agent_connector import AgentConnector
12-
from .errors import EyesError, NewTestError, DiffsFoundError, TestFailedError
1310
from .match_window_task import MatchWindowTask
11+
from .metadata import BatchInfo
12+
from .server_connector import ServerConnector
1413
from .test_results import TestResults, TestResultsStatus
14+
from .utils import ABC
1515

1616
if tp.TYPE_CHECKING:
1717
from .utils.custom_types import (
@@ -40,17 +40,16 @@ class FailureReports(object):
4040
class EyesBase(ABC):
4141
_DEFAULT_MATCH_TIMEOUT = 2000 # Milliseconds
4242
_DEFAULT_WAIT_BEFORE_SCREENSHOTS = 100 # ms
43-
DEFAULT_EYES_SERVER = "https://eyesapi.applitools.com"
4443

45-
def __init__(self, server_url=DEFAULT_EYES_SERVER):
44+
def __init__(self, server_url=None):
4645
# type: (tp.Text) -> None
4746
"""
4847
Creates a new (possibly disabled) Eyes instance that
4948
interacts with the Eyes server.
5049
5150
:param server_url: The URL of the Eyes server
5251
"""
53-
self._agent_connector = AgentConnector(server_url) # type: AgentConnector
52+
self._server_connector = ServerConnector(server_url) # type: ServerConnector
5453
self._should_get_title = False # type: bool
5554
self._is_open = False # type: bool
5655
self._app_name = None # type: tp.Optional[tp.Text]
@@ -209,7 +208,7 @@ def api_key(self):
209208
210209
:return: The Api key used for authenticating the user with Eyes.
211210
"""
212-
return self._agent_connector.api_key
211+
return self._server_connector.api_key
213212

214213
@api_key.setter
215214
def api_key(self, api_key):
@@ -219,7 +218,7 @@ def api_key(self, api_key):
219218
220219
:param api_key: The api key used for authenticating the user with Eyes.
221220
"""
222-
self._agent_connector.api_key = api_key # type: ignore
221+
self._server_connector.api_key = api_key # type: ignore
223222

224223
@property
225224
def server_url(self):
@@ -229,7 +228,7 @@ def server_url(self):
229228
230229
:return: The URL of the Eyes server, or None to use the default server.
231230
"""
232-
return self._agent_connector.server_url
231+
return self._server_connector.server_url
233232

234233
@server_url.setter
235234
def server_url(self, server_url):
@@ -240,10 +239,7 @@ def server_url(self, server_url):
240239
:param server_url: The URL of the Eyes server, or None to use the default server.
241240
:return: None
242241
"""
243-
if server_url is None:
244-
self._agent_connector.server_url = EyesBase.DEFAULT_EYES_SERVER
245-
else:
246-
self._agent_connector.server_url = server_url
242+
self._server_connector.server_url = server_url
247243

248244
@property
249245
@abc.abstractmethod
@@ -317,7 +313,7 @@ def close(self, raise_ex=True):
317313
(not is_new_session) and self.save_failed_tests
318314
)
319315
logger.debug("close(): automatically save session? %s" % should_save)
320-
results = self._agent_connector.stop_session(
316+
results = self._server_connector.stop_session(
321317
self._running_session, False, should_save
322318
)
323319
results.is_new = is_new_session
@@ -383,7 +379,7 @@ def abort_if_not_closed(self):
383379
if self._running_session:
384380
logger.debug("abort_if_not_closed(): Aborting session...")
385381
try:
386-
self._agent_connector.stop_session(
382+
self._server_connector.stop_session(
387383
self._running_session, True, False
388384
)
389385
logger.info("--- Test aborted.")
@@ -421,13 +417,10 @@ def _open_base(self, app_name, test_name, viewport_size=None):
421417
logger.info("\nEyes version: {}\n".format(self.full_agent_id))
422418

423419
if self.api_key is None:
424-
try:
425-
self.api_key = os.environ["APPLITOOLS_API_KEY"]
426-
except KeyError:
427-
raise EyesError(
428-
"API key not set! Log in to https://applitools.com to obtain your"
429-
" API Key and use 'api_key' to set it."
430-
)
420+
raise EyesError(
421+
"API key not set! Log in to https://applitools.com to obtain your"
422+
" API Key and use 'api_key' to set it."
423+
)
431424

432425
logger.info(
433426
"open(%s, %s, %s, %s)"
@@ -436,7 +429,7 @@ def _open_base(self, app_name, test_name, viewport_size=None):
436429

437430
if self.is_open:
438431
self.abort_if_not_closed()
439-
raise EyesError("a test is already running")
432+
raise EyesError("A test is already running")
440433

441434
self._before_open()
442435

@@ -487,7 +480,7 @@ def _start_session(self):
487480

488481
self._create_start_info()
489482
# Actually start the session.
490-
self._running_session = self._agent_connector.start_session(self._start_info)
483+
self._running_session = self._server_connector.start_session(self._start_info)
491484
self._should_match_once_on_timeout = self._running_session["is_new_session"]
492485

493486
def _reset_last_screenshot(self):
@@ -501,7 +494,7 @@ def _ensure_running_session(self):
501494
return
502495
self._start_session()
503496
self._match_window_task = MatchWindowTask(
504-
self, self._agent_connector, self._running_session, self.match_timeout
497+
self, self._server_connector, self._running_session, self.match_timeout
505498
)
506499

507500
def _before_match_window(self):
@@ -574,7 +567,7 @@ def _try_post_dom_snapshot(self, dom_json):
574567
if dom_json is None:
575568
return None
576569
try:
577-
return self._agent_connector.post_dom_snapshot(dom_json)
570+
return self._server_connector.post_dom_snapshot(dom_json)
578571
except Exception as e:
579572
logger.warning(
580573
"Couldn't send DOM Json. Passing...\n Got next error: {}".format(e)

eyes_core/applitools/core/match_window_task.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
UserInputs,
2121
MatchResult,
2222
)
23-
from .agent_connector import AgentConnector
23+
from .server_connector import ServerConnector
2424
from .match import ImageMatchSettings
2525
from .capture import EyesScreenshot
2626

@@ -36,22 +36,22 @@ class MatchWindowTask(object):
3636
(including retry and 'ignore mismatch' when needed).
3737
"""
3838

39-
_MATCH_INTERVAL = 0.5
39+
MATCH_INTERVAL = 0.5
4040

4141
MINIMUM_MATCH_TIMEOUT = 60 # Milliseconds
4242

43-
def __init__(self, eyes, agent_connector, running_session, default_retry_timeout):
44-
# type: (EyesBase, AgentConnector, RunningSession, Num) -> None
43+
def __init__(self, eyes, server_connector, running_session, default_retry_timeout):
44+
# type: (EyesBase, ServerConnector, RunningSession, Num) -> None
4545
"""
4646
Ctor.
4747
4848
:param eyes: The Eyes instance which created this task.
49-
:param agent_connector: The agent connector to use for communication.
49+
:param server_connector: The agent connector to use for communication.
5050
:param running_session: The current eyes session.
5151
:param default_retry_timeout: The default match timeout. (milliseconds)
5252
"""
5353
self._eyes = eyes
54-
self._agent_connector = agent_connector
54+
self._server_connector = server_connector
5555
self._running_session = running_session
5656
self._default_retry_timeout = (
5757
default_retry_timeout / 1000.0
@@ -186,17 +186,17 @@ def _run_with_intervals(self, prepare_action, retry_timeout):
186186
# Start the timer.
187187
start = time.time()
188188
logger.debug("First match attempt...")
189-
as_expected = self._agent_connector.match_window(self._running_session, data)
189+
as_expected = self._server_connector.match_window(self._running_session, data)
190190
if as_expected:
191191
return {"as_expected": True, "screenshot": self._screenshot}
192192
retry = time.time() - start
193193
logger.debug("Failed. Elapsed time: {0:.1f} seconds".format(retry))
194194

195195
while retry < retry_timeout:
196196
logger.debug("Matching...")
197-
time.sleep(self._MATCH_INTERVAL)
197+
time.sleep(self.MATCH_INTERVAL)
198198
data = prepare_action(ignore_mismatch=True)
199-
as_expected = self._agent_connector.match_window(
199+
as_expected = self._server_connector.match_window(
200200
self._running_session, data
201201
)
202202
if as_expected:
@@ -206,7 +206,7 @@ def _run_with_intervals(self, prepare_action, retry_timeout):
206206
# One last try
207207
logger.debug("One last matching attempt...")
208208
data = prepare_action()
209-
as_expected = self._agent_connector.match_window(self._running_session, data)
209+
as_expected = self._server_connector.match_window(self._running_session, data)
210210
return {"as_expected": as_expected, "screenshot": self._screenshot}
211211

212212
def _run(self, prepare_action, run_once_after_wait=False, retry_timeout=-1):
@@ -228,7 +228,7 @@ def _run(self, prepare_action, run_once_after_wait=False, retry_timeout=-1):
228228
# If the load time is 0, the sleep would immediately return anyway.
229229
time.sleep(retry_timeout)
230230
data = prepare_action()
231-
as_expected = self._agent_connector.match_window(
231+
as_expected = self._server_connector.match_window(
232232
self._running_session, data
233233
)
234234
result = {

0 commit comments

Comments
 (0)