Skip to content

Commit b9e6156

Browse files
committed
Waiting logic in testing stage.
1 parent 362df30 commit b9e6156

File tree

6 files changed

+93
-14
lines changed

6 files changed

+93
-14
lines changed

arjuna-samples/arjex/test/pkg/waiter/__init__.py

Whitespace-only changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# This file is a part of Arjuna
2+
# Copyright 2015-2021 Rahul Verma
3+
4+
# Website: www.RahulVerma.net
5+
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
import random
19+
20+
from arjuna import *
21+
22+
from arjuna.core.poller.conditions import *
23+
from arjuna.core.error import WaitableError
24+
25+
def is_expected_number_generated_1(num):
26+
retval = random.randint(1, 20)
27+
log_info("Generated:", retval)
28+
return retval == num
29+
30+
def expected_number_generated_1(num):
31+
return Conditions.true_condition(is_expected_number_generated_1, num)
32+
33+
@test
34+
def check_random_waiter_1(request):
35+
expected_number_generated_1(5).wait()
36+
37+
@test
38+
def check_random_waiter_2(request):
39+
expected_number_generated_1(51).wait(max_wait=10)
40+
41+
class ExpectedNumNotGenerated(WaitableError):
42+
43+
def __init__(self, num):
44+
super().__init__(f"Expected number {num} not generated despite waiting.")
45+
46+
def is_expected_number_generated_2(num):
47+
retval = random.randint(1, 20)
48+
log_info(retval, num, retval==num)
49+
if retval != num:
50+
raise ExpectedNumNotGenerated(num)
51+
else:
52+
return True
53+
54+
def expected_number_generated_2(num):
55+
return Conditions.true_condition(is_expected_number_generated_2, num)
56+
57+
@test
58+
def check_random_waiter_3(request):
59+
expected_number_generated_2(5).wait()
60+
61+
@test
62+
def check_random_waiter_4(request):
63+
expected_number_generated_2(51).wait(max_wait=10)

arjuna/core/poller/conditions.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ def wait(self, *, max_wait=60, poll_interval=0.5):
6868
ctime = time.time()
6969
if(ctime > end_time):
7070
break
71-
raise ArjunaTimeoutError(self.__class__.__name__, str(e) + etrace)
71+
if e is not None:
72+
raise ArjunaTimeoutError(self.__class__.__name__, str(e) + etrace)
73+
else:
74+
raise ArjunaTimeoutError(self.__class__.__name__, "Wait time over.")
7275

7376
def execute(self):
7477
try:
@@ -89,13 +92,30 @@ def is_met(self):
8992

9093
class BooleanCondition(Condition):
9194

92-
def __init__(self, dynamic_caller, expectation_type=True):
95+
def __init__(self, dynamic_caller, expected=True):
9396
super().__init__(dynamic_caller)
94-
self.__expectation_type = expectation_type
97+
self.__expectation_type = expected
9598

9699
def is_met(self):
97100
try:
98101
self.execute()
99102
return self.__expectation_type == self.get_call_result()
103+
except WaitableError as we:
104+
raise we
100105
except Exception as e:
101-
raise ConditionException("Unexpected exception in boolean condition checking: " + str(e) + traceback.format_exc())
106+
raise ConditionException("Unexpected exception in boolean condition checking: " + str(e) + traceback.format_exc())
107+
108+
class Conditions:
109+
110+
@classmethod
111+
def __create_caller(cls, target, *vargs, **kwargs):
112+
from arjuna.core.poller.caller import DynamicCaller
113+
return DynamicCaller(target, *vargs, **kwargs)
114+
115+
@classmethod
116+
def true_condition(cls, target, *vargs, **kwargs):
117+
return BooleanCondition(cls.__create_caller(target, *vargs, **kwargs), True)
118+
119+
@classmethod
120+
def false_condition(cls, target, *vargs, **kwargs):
121+
return BooleanCondition(cls.__create_caller(target, *vargs, **kwargs), False)

arjuna/interact/gui/auto/automator/automator_conditions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ def __init__(self, automator):
2424
self.__automator = automator
2525

2626
def AlertIsPresent(self):
27-
caller = DynamicCaller(self.__automator.get_alert_handler().is_alert_present)
28-
return BooleanCondition(caller, True)
27+
return Conditions.true_condition(self.__automator.get_alert_handler().is_alert_present)

arjuna/interact/gui/auto/condition/element_conditions.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import types
2020

2121
from arjuna.core.poller.caller import DynamicCaller
22-
from arjuna.core.poller.conditions import BooleanCondition, CommandCondition
22+
from arjuna.core.poller.conditions import BooleanCondition, CommandCondition, Conditions
2323

2424
class Handler(metaclass=abc.ABCMeta):
2525

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

4444
def IsSelected(self):
45-
caller = DynamicCaller(self.element.is_selected)
46-
return BooleanCondition(caller, True)
45+
return Conditions.true_condition(self.element.is_selected)
4746

4847
def IsVisible(self):
49-
caller = DynamicCaller(self.element.is_visible)
50-
return BooleanCondition(caller, True)
48+
return Conditions.true_condition(self.element.is_visible)
5149

5250
def IsClickable(self):
53-
caller = DynamicCaller(self.element.is_clickable)
54-
return BooleanCondition(caller, True)
51+
return Conditions.true_condition(self.element.is_clickable)
5552

5653
class GuiElementLenientInteraction:
5754

arjuna/tpi/httpauto/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file is a part of Arjuna
1+
# This file is a part of Arjuna
22
# Copyright 2015-2021 Rahul Verma
33

44
# Website: www.RahulVerma.net

0 commit comments

Comments
 (0)