55from selenium .webdriver .common .by import By
66from widgetastic .exceptions import NoSuchElementException
77from widgetastic .utils import ParametrizedLocator
8- from widgetastic .widget import GenericLocatorWidget , Widget
8+ from widgetastic .widget import GenericLocatorWidget , Widget , View
99from widgetastic .widget import TextInput
1010from widgetastic .xpath import quote
11- from widgetastic_patternfly4 import Select
11+ from widgetastic_patternfly4 import Select , Modal
1212
1313from testsuite .ui .exception import ItemNotPresentException
1414
@@ -254,36 +254,44 @@ def is_checked(self):
254254 return self .__element__ ().get_attribute ("checked" ) == "true"
255255
256256
257- class PolicySection (Widget ):
257+ class PolicySection (View ):
258258 """Widget representing Policies table section"""
259259
260260 ROOT = "//div[@class='PoliciesWidget']/section"
261261 POLICY_LIST = ".//ul/li"
262262 REGISTRY_ITEMS_LOCATOR = "//ul/li/article/h3"
263263 REGISTRY_ITEM_LOCATOR = "//ul/li/article/h3[text()='{}']"
264264 CHAIN_ITEMS_LOCATOR = ".//ul/li//article/h3"
265- CHAIN_ITEM_LOCATOR = "//ul/li//article/h3[text()='{}']"
266- ADD_POLICY_LOC = ".//button[normalize-space(.)='Add policy']"
265+ CHAIN_ITEM_LOCATOR = ".//ul/li//article/h3[text()='{}']"
266+ ADD_POLICY_LOCATOR = ".//button[normalize-space(.)='Add policy']"
267+ add_policy_modal = Modal (locator = "//div[contains(@class, 'pf-c-modal-box')]" )
267268
269+ # pylint: disable=super-init-not-called, non-parent-init-called
268270 def __init__ (self , parent = None , logger = None ):
269271 Widget .__init__ (self , parent , logger = logger )
270272
271273 @property
272274 def items (self ):
273- """Returns a list of all policy registry items as strings."""
275+ """Returns a list of all policy registry items from policy chain as strings."""
274276 return [self .browser .text (el ) for el in self .browser .elements (self .CHAIN_ITEMS_LOCATOR , parent = self )]
275277
276278 @property
277279 def registry_items (self ):
278280 """Returns a list of all policy registry items as strings."""
279- return [self .browser .text (el ) for el in self .browser .elements (self .REGISTRY_ITEMS_LOCATOR )]
281+ displayed = self .add_policy_modal .is_displayed
282+ if not displayed :
283+ self .open_policy_registry ()
284+ result = [self .browser .text (el ) for el in self .browser .elements (self .REGISTRY_ITEMS_LOCATOR )]
285+ if not displayed :
286+ self .close_policy_registry ()
287+ return result
280288
281289 @property
282290 def is_policy_registry_displayed (self ):
283291 """Check if policy registry list is displayed.
284292 :return: True if displayed, False otherwise.
285293 """
286- return self .browser .is_displayed ( self . ADD_POLICY_LOC )
294+ return self .add_policy_modal .is_displayed
287295
288296 @property
289297 def first_policy (self ):
@@ -297,15 +305,15 @@ def add_policy(self, policy_name):
297305 """Opens the Policy registry list and adds a policy by its name.
298306 :param policy_name: Name of the policy to be added
299307 """
300- self .browser .click (self .ADD_POLICY_LOC , parent = self )
308+ self .browser .click (self .ADD_POLICY_LOCATOR , parent = self )
301309 self .add_item (policy_name )
302310
303311 def edit_policy (self , policy_name ):
304312 """
305313 :param policy_name:
306314 """
307315 if self .has_item (policy_name ):
308- self .item_select ( policy_name )
316+ self .browser . click ( self . item_element ( policy_name ) )
309317 else :
310318 raise ItemNotPresentException ("Item {!r} not found." .format (policy_name ))
311319
@@ -323,7 +331,7 @@ def drag_and_drop_policy(self, source, destination):
323331 ac .perform ()
324332
325333 def has_item (self , item ):
326- """Returns whether the items exists .
334+ """Returns whether the items exist in policy chain .
327335 :param
328336 item: item name
329337 :return:
@@ -333,31 +341,53 @@ def has_item(self, item):
333341
334342 # pylint: disable=raise-missing-from
335343 def item_element (self , item ):
336- """Returns a WebElement for given item name.
344+ """Returns a WebElement for given item name from policy chain .
337345 :return WebElement
338346 """
339347 try :
340348 return self .browser .element (self .CHAIN_ITEM_LOCATOR .format (item ), parent = self )
341349 except NoSuchElementException :
342350 raise ItemNotPresentException ("Item {!r} not found." .format (item ))
343351
352+ # pylint: disable=raise-missing-from
353+ def registry_item_element (self , item ):
354+ """Returns a WebElement for given item name from policy registry.
355+ :return WebElement
356+ """
357+ if not self .add_policy_modal .is_displayed :
358+ self .open_policy_registry ()
359+ try :
360+ return self .browser .element (self .REGISTRY_ITEM_LOCATOR .format (item ), parent = self )
361+ except NoSuchElementException :
362+ raise ItemNotPresentException ("Item {!r} not found." .format (item ))
363+
344364 # pylint: disable=raise-missing-from
345365 def add_item (self , item ):
346366 """Add policy from policy registry"""
347367 self .logger .info ("Selecting %r" , item )
348368 if item not in self .registry_items :
349369 raise ItemNotPresentException ('Item "{item}" of policy is not present' .format (item = item ))
350- self .browser .click (self .item_element (item ))
370+ self .browser .click (self .registry_item_element (item ))
351371
352372 def item_select (self , item ):
353- """Opens the Policy registry and selects the desired policy.
373+ """Opens the policy registry and selects the desired policy.
354374 :param
355375 item: Item to be selected
356376 """
357377 self .logger .info ("Selecting %r" , item )
358378 if not self .has_item (item ):
359379 raise ItemNotPresentException ('Item "{item}" of policy is not present' .format (item = item ))
360- self .browser .click (self .item_element (item ))
380+ self .browser .click (self .registry_item_element (item ))
381+
382+ def open_policy_registry (self ):
383+ """Opens the policy registry if not already opened"""
384+ if not self .is_policy_registry_displayed :
385+ self .browser .click (self .ADD_POLICY_LOCATOR , parent = self )
386+
387+ def close_policy_registry (self ):
388+ """Closes the policy registry if not already closed"""
389+ if self .is_policy_registry_displayed :
390+ self .add_policy_modal .close ()
361391
362392
363393class APIDocsSelect (Select ):
0 commit comments