M test/README.md => test/README.md +2 -4
@@ 77,10 77,8 @@ methods:
from harness.harness import Harness
from harness.interface.defs import key_codes
-port_name = "/dev/ttyACM0"
-
-# init Harness object and open serial port
-harness = Harness(port_name)
+# try to init Harness object with automatic port detection
+harness = Harness.from_magic()
#get current application name
current_window = harness.get_window_name()
M test/harness/harness.py => test/harness/harness.py +2 -1
@@ 7,6 7,7 @@ from harness import utils, log
from harness.interface import CDCSerial as serial
from harness.interface.defs import key_codes, endpoint, method
from harness.utils import send_keystoke, application_keypath, send_char
+from harness.interface.error import TestError, Error
import random
@@ 37,7 38,7 @@ class Harness:
return self.connection
def get_window_name(self):
- return self.connection.get_window()
+ return self.connection.get_window_name()
def unlock_phone(self):
if self.connection.is_phone_locked():
M test/harness/interface/CDCSerial.py => test/harness/interface/CDCSerial.py +1 -0
@@ 41,6 41,7 @@ class CDCSerial:
def __del__(self):
try:
self.serial.close()
+ log.info(f"closed port {self.serial.name}")
except (serial.serialutil.SerialException, AttributeError):
pass
M test/pytest/conftest.py => test/pytest/conftest.py +34 -24
@@ 18,7 18,7 @@ simulator_port = 'simulator'
def pytest_addoption(parser):
- parser.addoption("--port", type=str, action="store", required=True)
+ parser.addoption("--port", type=str, action="store", required=False)
parser.addoption("--timeout", type=int, action="store", default=15)
parser.addoption("--phone_number", type=int, action="store")
parser.addoption("--call_duration", type=int, action="store", default=30)
@@ 49,31 49,41 @@ def sms_text(request):
@pytest.fixture(scope='session')
def harness(request):
port_name = request.config.option.port
- timeout = request.config.option.timeout
+
+ RETRY_EVERY = 0.7 # second
+ retries = request.config.option.timeout / RETRY_EVERY
if port_name is None:
- pytest.exit("no port provided!")
- assert '/dev' in port_name or simulator_port in port_name
-
- if simulator_port in port_name:
- while timeout != 0:
- try:
- file = open("/tmp/purephone_pts_name", "r")
- break
- except FileNotFoundError as err:
- time.sleep(1)
- timeout = timeout - 1
- print("waiting...")
- if timeout == 0:
- raise TestError(Error.PORT_FILE_NOT_FOUND)
-
- port_name = file.readline()
- if port_name.isascii():
- log.debug("found {} entry!".format(port_name))
- else:
- pytest.exit("not a valid sim pts entry!")
-
- harness = Harness(port_name)
+ log.warning("no port provided! trying automatic detection")
+ try:
+ harness = Harness.from_detect()
+ except TestError as e:
+ if e.get_error_code() == Error.PORT_NOT_FOUND:
+ pytest.exit("couldn't find any viable port. exiting")
+ else:
+ raise(e)
+ else:
+ assert '/dev' in port_name or simulator_port in port_name
+
+ if simulator_port in port_name:
+ while retries > 0:
+ try:
+ file = open("/tmp/purephone_pts_name", "r")
+ break
+ except FileNotFoundError as err:
+ time.sleep(RETRY_EVERY)
+ retries -= 1
+ log.info("waiting for simulator port...")
+ else:
+ raise TestError(Error.PORT_FILE_NOT_FOUND)
+
+ port_name = file.readline()
+ if port_name.isascii():
+ log.debug("found {} entry!".format(port_name))
+ else:
+ pytest.exit("not a valid sim pts entry!")
+
+ harness = Harness(port_name)
return harness
@pytest.fixture(scope='session')