From 7db4406293e0050239510e7af79ceea4c10615db Mon Sep 17 00:00:00 2001
From: George Gelashvili and Tira Odhner <pair+ggelashvili+aodhner@pivotal.io>
Date: Mon, 3 Apr 2017 14:17:24 -0400
Subject: [PATCH 11/11] Add row and column-selection feature test

---
 .../copy_selected_query_results_feature_test.py    | 67 ++++++++++++++++++++++
 web/regression/feature_utils/base_feature_test.py  |  1 +
 web/regression/feature_utils/pgadmin_page.py       | 11 ++++
 web/regression/python_test_utils/test_utils.py     |  1 +
 4 files changed, 80 insertions(+)
 create mode 100644 web/pgadmin/feature_tests/copy_selected_query_results_feature_test.py

diff --git a/web/pgadmin/feature_tests/copy_selected_query_results_feature_test.py b/web/pgadmin/feature_tests/copy_selected_query_results_feature_test.py
new file mode 100644
index 00000000..2f0bd336
--- /dev/null
+++ b/web/pgadmin/feature_tests/copy_selected_query_results_feature_test.py
@@ -0,0 +1,67 @@
+import pyperclip
+import time
+
+from selenium.webdriver import ActionChains
+
+from regression.python_test_utils import test_utils
+from regression.feature_utils.base_feature_test import BaseFeatureTest
+
+
+class CopySelectedQueryResultsFeatureTest(BaseFeatureTest):
+    def before(self):
+        connection = test_utils.get_db_connection(self.server['db'],
+                                                  self.server['username'],
+                                                  self.server['db_password'],
+                                                  self.server['host'],
+                                                  self.server['port'])
+        test_utils.drop_database(connection, "acceptance_test_db")
+        test_utils.create_database(self.server, "acceptance_test_db")
+        test_utils.create_table(self.server, "acceptance_test_db", "test_table")
+        self.page.add_server(self.server)
+
+    def runTest(self):
+        self.page.toggle_open_tree_item(self.server['name'])
+        self.page.toggle_open_tree_item('Databases')
+        self.page.toggle_open_tree_item('acceptance_test_db')
+        time.sleep(5)
+        self.page.find_by_partial_link_text("Tools").click()
+        self.page.find_by_partial_link_text("Query Tool").click()
+        self.page.click_tab('Query-1')
+        time.sleep(5)
+        ActionChains(self.page.driver).send_keys("SELECT * FROM test_table").perform()
+        self.page.driver.switch_to_frame(self.page.driver.find_element_by_tag_name("iframe"))
+        self.page.find_by_id("btn-flash").click()
+
+        self._copies_rows()
+        self._copies_columns()
+
+    def _copies_rows(self):
+        pyperclip.copy("old clipboard contents")
+        time.sleep(5)
+        self.page.find_by_xpath("//*[contains(@class, 'sr')]/*[1]/input[@type='checkbox']").click()
+        self.page.find_by_xpath("//*[@id='btn-copy-row']").click()
+
+        self.assertEqual("'Some-Name','6'",
+                         pyperclip.paste())
+
+    def _copies_columns(self):
+        pyperclip.copy("old clipboard contents")
+
+        self.page.find_by_xpath("//*[@data-test='output-column-header' and contains(., 'some_column')]/input").click()
+        self.page.find_by_xpath("//*[@id='btn-copy-row']").click()
+
+        self.assertEqual(
+            """'Some-Name'
+'Some-Other-Name'""",
+            pyperclip.paste())
+
+    def after(self):
+        self.page.close_query_tool()
+        self.page.remove_server(self.server)
+
+        connection = test_utils.get_db_connection(self.server['db'],
+                                                  self.server['username'],
+                                                  self.server['db_password'],
+                                                  self.server['host'],
+                                                  self.server['port'])
+        test_utils.drop_database(connection, "acceptance_test_db")
diff --git a/web/regression/feature_utils/base_feature_test.py b/web/regression/feature_utils/base_feature_test.py
index 8bb6bc00..dc704e8e 100644
--- a/web/regression/feature_utils/base_feature_test.py
+++ b/web/regression/feature_utils/base_feature_test.py
@@ -28,6 +28,7 @@ class BaseFeatureTest(BaseTestGenerator):
 
         self.page = PgadminPage(self.driver, app_config)
         try:
+            self.page.driver.switch_to.default_content()
             self.page.wait_for_app()
             self.page.wait_for_spinner_to_disappear()
             self.page.reset_layout()
diff --git a/web/regression/feature_utils/pgadmin_page.py b/web/regression/feature_utils/pgadmin_page.py
index aa31bc04..c1995966 100644
--- a/web/regression/feature_utils/pgadmin_page.py
+++ b/web/regression/feature_utils/pgadmin_page.py
@@ -55,7 +55,18 @@ class PgadminPage:
 
         self.find_by_xpath("//*[@id='tree']//*[.='" + server_config['name'] + "']")
 
+    def close_query_tool(self):
+        self.driver.switch_to.default_content()
+        tab = self.find_by_xpath("//*[contains(@class,'wcPanelTab') and contains(.,'" + "Query" + "')]")
+        ActionChains(self.driver).context_click(tab).perform()
+        self.find_by_xpath("//li[contains(@class, 'context-menu-item')]/span[contains(text(), 'Remove Panel')]").click()
+        self.driver.switch_to.frame(self.driver.find_elements_by_tag_name("iframe")[0])
+        time.sleep(.5)
+        self.click_element(self.find_by_xpath('//button[contains(@class, "ajs-button") and contains(.,"Yes")]'))
+        self.driver.switch_to.default_content()
+
     def remove_server(self, server_config):
+        self.driver.switch_to.default_content()
         self.find_by_xpath("//*[@id='tree']//*[.='" + server_config['name'] + "' and @class='aciTreeItem']").click()
         self.find_by_partial_link_text("Object").click()
         self.find_by_partial_link_text("Delete/Drop").click()
diff --git a/web/regression/python_test_utils/test_utils.py b/web/regression/python_test_utils/test_utils.py
index ada3f829..c7808e92 100644
--- a/web/regression/python_test_utils/test_utils.py
+++ b/web/regression/python_test_utils/test_utils.py
@@ -166,6 +166,7 @@ def create_table(server, db_name, table_name):
         pg_cursor = connection.cursor()
         pg_cursor.execute('''CREATE TABLE "%s" (some_column VARCHAR, value NUMERIC)''' % table_name)
         pg_cursor.execute('''INSERT INTO "%s" VALUES ('Some-Name', 6)''' % table_name)
+        pg_cursor.execute('''INSERT INTO "%s" VALUES ('Some-Other-Name', 22)''' % table_name)
         connection.set_isolation_level(old_isolation_level)
         connection.commit()
 
-- 
2.12.0

