Repository: ManageIQ/integration_tests

Issue:
[WIP] fix test_no_template_power
All 10 tests of cfme/test/infrastructure/test_vm_power_control.py:test_no_template_power_control are failing, the toolbar.select now does js clicks if it gets a exception.  This does work now wit 5.5 but I am don;t think this is a proper fix and needs a better approach.  Feel free to fix and close.



Discussion:
Lint report for commit 52803387ea1ee6e195ce572252b40e3f25e274ae:
:hurtrealbad: There were some flake issues that need to be resolved in order to merge the pull request:

```
./cfme/tests/infrastructure/test_vm_power_control.py:10:1: F401 'NoSuchElementException' imported but unused
./cfme/tests/infrastructure/test_vm_power_control.py:11:1: F401 'error' imported but unused
```

:trollface:
_CFME QE Bot_

[![Code Health](https://landscape.io/badge/251784/landscape.svg?style=flat)](https://landscape.io/diff/238509)
Repository health decreased by 0.01% when pulling **[5280338](https://github.com/dajohnso/cfme_tests/commit/52803387ea1ee6e195ce572252b40e3f25e274ae) on dajohnso:fix_no_template_power** into **[19e65d5](https://github.com/RedHatQE/cfme_tests/commit/19e65d5c38334dde58f1cded2c727b712f1314c4) on RedHatQE:master**.
- [1 new problem was found](https://landscape.io/diff/238509) (including 0 errors and 1 code smell).
- No problems were fixed.

@psav , could you take a peek here and see if you have any good ideas on how to address this

@psav or @mfalesni could you take a look at this and see if you can come up with a better .exists() .  Feel free to throw this PR away in favor of what you whip up but I believe we need a better .exists() here

Picking this up.


Resolving patch:
diff --git a/cfme/infrastructure/virtual_machines.py b/cfme/infrastructure/virtual_machines.py
--- a/cfme/infrastructure/virtual_machines.py
+++ b/cfme/infrastructure/virtual_machines.py
@@ -678,13 +678,13 @@ def get_all_vms(do_not_navigate=False):
         sel.force_navigate('infra_vms')
     vms = set([])
 
-    title_attr = version.pick({
-        version.LOWEST: "title",
-        "5.5": "data-original-title"})
-
+    # This is really stupid, but I cannot come up with better getting of the attributes :(
     if not paginator.page_controls_exist():
         for title in sel.elements(QUADICON_TITLE_LOCATOR):
-            vms.add(sel.get_attribute(title, title_attr))
+            title_value = sel.get_attribute(title, "title")
+            if not title_value:
+                title_value = sel.get_attribute(title, "data-original-title")
+            vms.add(title_value)
         return vms
 
     paginator.results_per_page(1000)
@@ -692,7 +692,10 @@ def get_all_vms(do_not_navigate=False):
         try:
             for page in paginator.pages():
                 for title in sel.elements(QUADICON_TITLE_LOCATOR):
-                    vms.add(sel.get_attribute(title, title_attr))
+                    title_value = sel.get_attribute(title, "title")
+                    if not title_value:
+                        title_value = sel.get_attribute(title, "data-original-title")
+                    vms.add(title_value)
         except sel.NoSuchElementException:
             pass
     return vms
diff --git a/cfme/web_ui/toolbar.py b/cfme/web_ui/toolbar.py
--- a/cfme/web_ui/toolbar.py
+++ b/cfme/web_ui/toolbar.py
@@ -86,7 +86,7 @@ def pf_select(root, sub=None, invokes_alert=False):
     sel.wait_for_ajax()
     if isinstance(root, dict):
         root = version.pick(root)
-    if sub is not None and isinstance(sub, dict):
+    if isinstance(sub, dict):
         sub = version.pick(sub)
 
     if sub:
@@ -106,8 +106,13 @@ def pf_select(root, sub=None, invokes_alert=False):
                 sel.execute_script(
                     "return $('button[title={}]').trigger('click')".format(q_root))
             except sel.NoSuchElementException:
-                sel.execute_script(
-                    "return $('button:contains({})').trigger('click')".format(q_root))
+                try:
+                    sel.element("//button[contains(@title, {})]".format(q_root))
+                    sel.execute_script(
+                        "return $('button:contains({})').trigger('click')".format(q_root))
+                except sel.NoSuchElementException:
+                    # The view selection buttons?
+                    sel.click("//li/a[@title={}]/i/../..".format(q_root))
 
     if not invokes_alert:
         sel.wait_for_ajax()
@@ -212,3 +217,31 @@ def refresh():
         sel.click("//div[@title='Reload current display']")
     else:
         sel.refresh()
+
+
+def exists(root, sub=None, and_is_not_greyed=False):
+    """ Checks presence and usability of toolbar buttons.
+
+    By default it checks whether the button is available, not caring whether it is greyed or not.
+    You can optionally enable check for greyedness.
+
+    Args:
+        root: Button name.
+        sub: Item name (optional)
+        and_is_not_greyed: Check if the button is available to click.
+
+    """
+    sel.wait_for_ajax()
+    if isinstance(root, dict):
+        root = version.pick(root)
+    if isinstance(sub, dict):
+        sub = version.pick(sub)
+
+    try:
+        greyed = is_greyed(root, sub)
+        if and_is_not_greyed:
+            return not greyed
+        else:
+            return True
+    except sel.NoSuchElementException:
+        return False


Test patch:
diff --git a/cfme/tests/infrastructure/test_vm_power_control.py b/cfme/tests/infrastructure/test_vm_power_control.py
--- a/cfme/tests/infrastructure/test_vm_power_control.py
+++ b/cfme/tests/infrastructure/test_vm_power_control.py
@@ -7,8 +7,7 @@
 from cfme.common.vm import VM
 from cfme.infrastructure.virtual_machines import get_all_vms
 from cfme.web_ui import toolbar
-from selenium.common.exceptions import NoSuchElementException
-from utils import testgen, error
+from utils import testgen
 from utils.log import logger
 from utils.wait import wait_for, TimedOutError
 from utils.version import appliance_is_downstream, current_version
@@ -362,17 +361,23 @@ def test_start_from_suspend(
                         "ui: {} should !=  orig: {}".format(new_last_boot_time, last_boot_time))
 
 
-def test_no_template_power_control(provider, setup_provider_funcscope):
-    """ Ensures that no power button is displayed for templates."""
+def test_no_template_power_control(provider, setup_provider_funcscope, soft_assert):
+    """ Ensures that no power button is displayed for templates.
+
+    Prerequisities:
+        * An infra provider that has some templates.
+
+    Steps:
+        * Open the view of all templates of the provider
+        * Verify the Power toolbar button is not visible
+        * Select some template using the checkbox
+        * Verify the Power toolbar button is not visible
+        * Click on some template to get into the details page
+        * Verify the Power toolbar button is not visible
+    """
     provider.load_all_provider_templates()
     toolbar.select('Grid View')
-    try:
-        with error.expected(NoSuchElementException):
-            toolbar.select("Power")
-    except Exception:
-        # try again
-        with error.expected(NoSuchElementException):
-            toolbar.select("Power")
+    soft_assert(not toolbar.exists("Power"), "Power displayed in template grid view!")
 
     # Ensure selecting a template doesn't cause power menu to appear
     templates = list(get_all_vms(True))
@@ -381,13 +386,11 @@ def test_no_template_power_control(provider, setup_provider_funcscope):
 
     # Check the power button with checking the quadicon
     quadicon = selected_template.find_quadicon(do_not_navigate=True, mark=True, refresh=False)
-    with error.expected(NoSuchElementException):
-        toolbar.select("Power")
+    soft_assert(not toolbar.exists("Power"), "Power displayed when template quadicon checked!")
 
     # Ensure there isn't a power button on the details page
     pytest.sel.click(quadicon)
-    with error.expected(NoSuchElementException):
-        toolbar.select("Power")
+    soft_assert(not toolbar.exists("Power"), "Power displayed in template details!")
 
 
 @pytest.mark.usefixtures("test_vm")