Repository: ManageIQ/integration_tests

Issue:
Be able to point selenium at multiple appliances within a single test
Currently, we can aim selenium at one appliance and run all of our tests against it. This is useful for most of our test cases, but when we're setting up and testing distributed appliance cases we'll need to be able to interact with multiple appliances in a single test run (e.g. configuring an appliances to replicate its database for a multi-region setup, configuring specific server roles to ensure failover is working, etc).



Discussion:

Resolving patch:
diff --git a/fixtures/selenium.py b/fixtures/selenium.py
new file mode 100644
--- /dev/null
+++ b/fixtures/selenium.py
@@ -0,0 +1,52 @@
+import pytest
+from pytest_mozwebqa import split_class_and_test_names
+from pytest_mozwebqa.selenium_client import Client
+
+def pytest_runtest_setup(item):
+    # If we're using the selenium fixture, mark the test as
+    # skip_selenium to prevent mozwebqa from autostarting the browser,
+    # but we still want mozwebqa for passing testsetup to page objects
+    if 'selenium' in item.funcargnames and 'skip_selenium' not in item.keywords:
+        item.keywords['skip_selenium'] = True
+
+@pytest.fixture
+def selenium(mozwebqa, request):
+    """A fixture giving the user more control over the mozwebqa selenium client"""
+    # Start to bootstrap the selenium client like mozwebqa does,
+    # Doesn't currently support sauce labs, but could be made to do so if needed
+    test_id = '.'.join(split_class_and_test_names(request.node.nodeid))
+    mozwebqa.selenium_client = Client(test_id, request.session.config.option)
+
+    def cm_wrapper(url, page_obj):
+        return SeleniumContextManager(mozwebqa, url, page_obj)
+
+    return cm_wrapper
+
+class SeleniumContextManager(object):
+    def __init__(self, testsetup, url, page_obj):
+        self.testsetup = testsetup
+        self.url = url
+        self.page_obj = page_obj
+
+    def __enter__(self):
+        # More mozwebqa bootstrapping, start the browser, expose some
+        # client attrs on testsetup, navigate to the requested url,
+        # return a Page instance with the current testsetup
+        # This should mimic the behavior of mozwebqa as closely as possible
+        self.testsetup.selenium_client.start()
+        copy_attrs = (
+            'selenium',
+            'timeout',
+            'default_implicit_wait'
+        )
+        for attr in copy_attrs:
+            setattr(self.testsetup, attr, getattr(self.testsetup.selenium_client, attr))
+
+        self.testsetup.base_url = self.url
+        self.testsetup.selenium.get(self.url)
+        return self.page_obj(self.testsetup)
+
+    def __exit__(self, *args, **kwargs):
+        self.testsetup.selenium_client.stop()
+
+


Test patch:
diff --git a/utils/tests/test_selenium_fixture.py b/utils/tests/test_selenium_fixture.py
new file mode 100644
--- /dev/null
+++ b/utils/tests/test_selenium_fixture.py
@@ -0,0 +1,14 @@
+import time
+
+import pytest
+
+from pages.page import Page
+
+@pytest.mark.nondestructive
+def test_selenium(selenium):
+    with selenium('http://www.redhat.com', Page) as page:
+        assert 'redhat.com' in page.selenium.current_url.lower()
+
+    with selenium('http://www.google.com', Page) as page:
+        assert 'google.com' in page.selenium.current_url.lower()
+