From c1b1d0ad28386e601dc406b42b83a2c4320baa2a Mon Sep 17 00:00:00 2001 From: Ajaydeep Grewal Date: Tue, 18 Jan 2022 17:37:10 -0500 Subject: [PATCH 1/4] WIFI-4921:Adding code for checking the status of the device and also rechecking if the device is not available Signed-off-by: Ajaydeep Grewal --- tests/e2e/interOp/conftest.py | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/e2e/interOp/conftest.py b/tests/e2e/interOp/conftest.py index ceda118bb..4309579a6 100644 --- a/tests/e2e/interOp/conftest.py +++ b/tests/e2e/interOp/conftest.py @@ -11,6 +11,8 @@ import pytest import logging import re import allure +import requests +from xml.etree import ElementTree as ET sys.path.append( os.path.dirname( @@ -400,6 +402,9 @@ def setup_perfectoMobile_android(request): # 'bundleId' : request.config.getini("appPackage-android"), } + if not is_device_Available_timeout(request, capabilities['model']): + assert False, "Unable to get device." + driver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', capabilities) @@ -504,6 +509,9 @@ def setup_perfectoMobileWeb(request): 'securityToken': request.config.getini("securityToken"), } + if not is_device_Available_timeout(request, capabilities['model']): + assert False, "Unable to get device." + rdriver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', capabilities) @@ -567,6 +575,10 @@ def setup_perfectoMobile_iOS(request): 'useAppiumForHybrid': 'false', } + # Check if the device is available + if not is_device_Available_timeout(request, capabilities['model']): + assert False, "Unable to get device." + driver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', capabilities) @@ -631,3 +643,57 @@ def setup_perfectoMobile_iOS(request): yield -1 else: yield driver, reporting_client +# Does a HTTP GET request to Perfecto cloud and gets response and information related to a headset +def response_device(request, model): + securityToken = request.config.getini("securityToken") + perfectoURL = request.config.getini("perfectoURL") + url = f"https://{perfectoURL}.perfectomobile.com/services/handsets?operation=list&securityToken={securityToken}&model={model}" + print("URL:" + url) + resp = requests.get(url=url) + return ET.fromstring(resp.content) + +# Get an attribute value from the handset response +def get_attribute_device(responseXml, attribute): + try: + return responseXml.find('handset').find(attribute).text + except: + print(f"Unable to get value of {attribute} from response") + return "" + +# Checks to see if a particular handset is available +def is_device_available(request, model): + try: + responseXml = response_device(request, model) + except: + print("Unable to get response.") + raise Exception("Unable to get response.") + device_available = get_attribute_device(responseXml, 'available') + print("Result:" + device_available) + if device_available == 'false': + allocated_to = get_attribute_device(responseXml, 'allocatedTo') + print("The device is currently allocated to:" + allocated_to) + return False + return True + +# Rechecking for a busy device. By default we are polling the device every 30 secs for 5 iterations for a total of 5x30=150sec(2.5 Mins) +def is_device_Available_timeout(request, model): + counter_time = 0 + deviceRecheckingInterval = 30 + deviceRecheckingTotalInterations = 5 + try: + device_available = is_device_available(request, model) + except: + print("Unable to get attributes from the response.") + + while counter_time < deviceRecheckingTotalInterations: + if not device_available: + print(f"Device not available.Rechecking after {deviceRecheckingInterval} seconds") + time.sleep(deviceRecheckingInterval) + counter_time = counter_time + 1 + device_available = is_device_available(request, model) + else: + print("Device is available and we can instantiate the driver") + return True + + print("Device was not available even after checking for 5 mins") + return False \ No newline at end of file From a57c65590505d203f81d9bd6175dbe31c1d9be40 Mon Sep 17 00:00:00 2001 From: Ajaydeep Grewal Date: Tue, 18 Jan 2022 18:12:51 -0500 Subject: [PATCH 2/4] Modifying the error message when the device is not available Signed-off-by: Ajaydeep Grewal --- tests/e2e/interOp/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/e2e/interOp/conftest.py b/tests/e2e/interOp/conftest.py index 4309579a6..963d0ac03 100644 --- a/tests/e2e/interOp/conftest.py +++ b/tests/e2e/interOp/conftest.py @@ -648,7 +648,6 @@ def response_device(request, model): securityToken = request.config.getini("securityToken") perfectoURL = request.config.getini("perfectoURL") url = f"https://{perfectoURL}.perfectomobile.com/services/handsets?operation=list&securityToken={securityToken}&model={model}" - print("URL:" + url) resp = requests.get(url=url) return ET.fromstring(resp.content) @@ -695,5 +694,5 @@ def is_device_Available_timeout(request, model): print("Device is available and we can instantiate the driver") return True - print("Device was not available even after checking for 5 mins") + print(f"Device was not available even after checking for {(deviceRecheckingInterval * deviceRecheckingTotalInterations)} seconds") return False \ No newline at end of file From 30539f002422e08c5dc43ddfb49170cad9bddfcc Mon Sep 17 00:00:00 2001 From: Ajaydeep Grewal Date: Mon, 31 Jan 2022 13:16:38 -0500 Subject: [PATCH 3/4] Exit pytest if device is unavailable even after retrying. Signed-off-by: Ajaydeep Grewal --- tests/e2e/interOp/conftest.py | 48 ++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/tests/e2e/interOp/conftest.py b/tests/e2e/interOp/conftest.py index 963d0ac03..bb02292e7 100644 --- a/tests/e2e/interOp/conftest.py +++ b/tests/e2e/interOp/conftest.py @@ -13,6 +13,7 @@ import re import allure import requests from xml.etree import ElementTree as ET +from time import gmtime, strftime sys.path.append( os.path.dirname( @@ -403,7 +404,8 @@ def setup_perfectoMobile_android(request): } if not is_device_Available_timeout(request, capabilities['model']): - assert False, "Unable to get device." + print("Unable to get device.") + pytest.exit("Exiting Pytest") driver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', @@ -510,7 +512,8 @@ def setup_perfectoMobileWeb(request): } if not is_device_Available_timeout(request, capabilities['model']): - assert False, "Unable to get device." + print("Unable to get device.") + pytest.exit("Exiting Pytest") rdriver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', @@ -577,7 +580,8 @@ def setup_perfectoMobile_iOS(request): # Check if the device is available if not is_device_Available_timeout(request, capabilities['model']): - assert False, "Unable to get device." + print("Unable to get device.") + pytest.exit("Exiting Pytest") driver = webdriver.Remote( 'https://' + request.config.getini("perfectoURL") + '.perfectomobile.com/nexperience/perfectomobile/wd/hub', @@ -674,25 +678,27 @@ def is_device_available(request, model): return False return True -# Rechecking for a busy device. By default we are polling the device every 30 secs for 5 iterations for a total of 5x30=150sec(2.5 Mins) +# Checks whether the device is available or not.If the device is not available rechecks depending upon the +# 'timerValue' and 'timerThreshold' values.With the current parameters it will check after:10,20,40,80 mins. def is_device_Available_timeout(request, model): - counter_time = 0 - deviceRecheckingInterval = 30 - deviceRecheckingTotalInterations = 5 - try: - device_available = is_device_available(request, model) - except: - print("Unable to get attributes from the response.") - - while counter_time < deviceRecheckingTotalInterations: - if not device_available: - print(f"Device not available.Rechecking after {deviceRecheckingInterval} seconds") - time.sleep(deviceRecheckingInterval) - counter_time = counter_time + 1 + device_available = is_device_available(request, model) + timerValue = 10 + timerThreshold = 80 + if not device_available: + while(timerValue <= timerThreshold): + print("Last checked at:" + strftime("%Y-%m-%d %H:%M:%S", gmtime())) + print(f"Waiting for: {timerValue} min(s)") + time.sleep(timerValue*60) + print("Checking now at:" + strftime("%Y-%m-%d %H:%M:%S", gmtime())) device_available = is_device_available(request, model) + if(device_available): + return True + else: + timerValue = 2 * timerValue + + if(timerValue > timerThreshold): + return False else: - print("Device is available and we can instantiate the driver") return True - - print(f"Device was not available even after checking for {(deviceRecheckingInterval * deviceRecheckingTotalInterations)} seconds") - return False \ No newline at end of file + else: + return True \ No newline at end of file From 735f75362981bfcb3b75ccd733cfbecc8c9da329 Mon Sep 17 00:00:00 2001 From: haricharan-jaka Date: Mon, 7 Feb 2022 10:55:50 +0530 Subject: [PATCH 4/4] Changed the timer value to 5 mins Signed-off-by: haricharan-jaka --- tests/e2e/interOp/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/interOp/conftest.py b/tests/e2e/interOp/conftest.py index bb02292e7..c8dfa1d17 100644 --- a/tests/e2e/interOp/conftest.py +++ b/tests/e2e/interOp/conftest.py @@ -682,7 +682,7 @@ def is_device_available(request, model): # 'timerValue' and 'timerThreshold' values.With the current parameters it will check after:10,20,40,80 mins. def is_device_Available_timeout(request, model): device_available = is_device_available(request, model) - timerValue = 10 + timerValue = 5 timerThreshold = 80 if not device_available: while(timerValue <= timerThreshold): @@ -694,7 +694,7 @@ def is_device_Available_timeout(request, model): if(device_available): return True else: - timerValue = 2 * timerValue + timerValue = timerValue + 5 if(timerValue > timerThreshold): return False