Skip to content

Commit

Permalink
Waiting logic in testing stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-verma committed Jun 28, 2022
1 parent 362df30 commit b9e6156
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 14 deletions.
Empty file.
63 changes: 63 additions & 0 deletions arjuna-samples/arjex/test/pkg/waiter/check_waiter_concept.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This file is a part of Arjuna
# Copyright 2015-2021 Rahul Verma

# Website: www.RahulVerma.net

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random

from arjuna import *

from arjuna.core.poller.conditions import *
from arjuna.core.error import WaitableError

def is_expected_number_generated_1(num):
retval = random.randint(1, 20)
log_info("Generated:", retval)
return retval == num

def expected_number_generated_1(num):
return Conditions.true_condition(is_expected_number_generated_1, num)

@test
def check_random_waiter_1(request):
expected_number_generated_1(5).wait()

@test
def check_random_waiter_2(request):
expected_number_generated_1(51).wait(max_wait=10)

class ExpectedNumNotGenerated(WaitableError):

def __init__(self, num):
super().__init__(f"Expected number {num} not generated despite waiting.")

def is_expected_number_generated_2(num):
retval = random.randint(1, 20)
log_info(retval, num, retval==num)
if retval != num:
raise ExpectedNumNotGenerated(num)
else:
return True

def expected_number_generated_2(num):
return Conditions.true_condition(is_expected_number_generated_2, num)

@test
def check_random_waiter_3(request):
expected_number_generated_2(5).wait()

@test
def check_random_waiter_4(request):
expected_number_generated_2(51).wait(max_wait=10)
28 changes: 24 additions & 4 deletions arjuna/core/poller/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def wait(self, *, max_wait=60, poll_interval=0.5):
ctime = time.time()
if(ctime > end_time):
break
raise ArjunaTimeoutError(self.__class__.__name__, str(e) + etrace)
if e is not None:
raise ArjunaTimeoutError(self.__class__.__name__, str(e) + etrace)
else:
raise ArjunaTimeoutError(self.__class__.__name__, "Wait time over.")

def execute(self):
try:
Expand All @@ -89,13 +92,30 @@ def is_met(self):

class BooleanCondition(Condition):

def __init__(self, dynamic_caller, expectation_type=True):
def __init__(self, dynamic_caller, expected=True):
super().__init__(dynamic_caller)
self.__expectation_type = expectation_type
self.__expectation_type = expected

def is_met(self):
try:
self.execute()
return self.__expectation_type == self.get_call_result()
except WaitableError as we:
raise we
except Exception as e:
raise ConditionException("Unexpected exception in boolean condition checking: " + str(e) + traceback.format_exc())
raise ConditionException("Unexpected exception in boolean condition checking: " + str(e) + traceback.format_exc())

class Conditions:

@classmethod
def __create_caller(cls, target, *vargs, **kwargs):
from arjuna.core.poller.caller import DynamicCaller
return DynamicCaller(target, *vargs, **kwargs)

@classmethod
def true_condition(cls, target, *vargs, **kwargs):
return BooleanCondition(cls.__create_caller(target, *vargs, **kwargs), True)

@classmethod
def false_condition(cls, target, *vargs, **kwargs):
return BooleanCondition(cls.__create_caller(target, *vargs, **kwargs), False)
3 changes: 1 addition & 2 deletions arjuna/interact/gui/auto/automator/automator_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ def __init__(self, automator):
self.__automator = automator

def AlertIsPresent(self):
caller = DynamicCaller(self.__automator.get_alert_handler().is_alert_present)
return BooleanCondition(caller, True)
return Conditions.true_condition(self.__automator.get_alert_handler().is_alert_present)
11 changes: 4 additions & 7 deletions arjuna/interact/gui/auto/condition/element_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import types

from arjuna.core.poller.caller import DynamicCaller
from arjuna.core.poller.conditions import BooleanCondition, CommandCondition
from arjuna.core.poller.conditions import BooleanCondition, CommandCondition, Conditions

class Handler(metaclass=abc.ABCMeta):

Expand All @@ -42,16 +42,13 @@ def __init__(self, element):
super().__init__(element)

def IsSelected(self):
caller = DynamicCaller(self.element.is_selected)
return BooleanCondition(caller, True)
return Conditions.true_condition(self.element.is_selected)

def IsVisible(self):
caller = DynamicCaller(self.element.is_visible)
return BooleanCondition(caller, True)
return Conditions.true_condition(self.element.is_visible)

def IsClickable(self):
caller = DynamicCaller(self.element.is_clickable)
return BooleanCondition(caller, True)
return Conditions.true_condition(self.element.is_clickable)

class GuiElementLenientInteraction:

Expand Down
2 changes: 1 addition & 1 deletion arjuna/tpi/httpauto/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is a part of Arjuna
# This file is a part of Arjuna
# Copyright 2015-2021 Rahul Verma

# Website: www.RahulVerma.net
Expand Down

0 comments on commit b9e6156

Please sign in to comment.