|
| 1 | +import pytest |
| 2 | +from selenium.webdriver.common.by import By |
| 3 | +from selenium.webdriver.common.keys import Keys |
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
| 5 | +from selenium.webdriver.support import expected_conditions |
| 6 | +from selenium.webdriver.common.action_chains import ActionChains |
| 7 | +from selenium.webdriver.common.alert import Alert |
| 8 | +from codebender_testing.utils import SeleniumTestCase |
| 9 | +from codebender_testing.config import TIMEOUT |
| 10 | +import time |
| 11 | + |
| 12 | +TEST_SKETCH = 'test_collaboration' |
| 13 | +TEST_FILE = 'test.h' |
| 14 | +TEST_EMPTY_FILE = 'empty.h' |
| 15 | +TEST_RENAME_FILE = 'rename.h' |
| 16 | +TEST_RENAMED_FILE = 'renamed.h' |
| 17 | +TEST_INPUT = '// test input' |
| 18 | + |
| 19 | +class TestCollaboration(SeleniumTestCase): |
| 20 | + ''' Opens a new tab in the browser ''' |
| 21 | + def open_tab(self, url): |
| 22 | + self.execute_script('window.open("{}")'.format(url)) |
| 23 | + |
| 24 | + ''' Closes current tab in the browser ''' |
| 25 | + def close_tab(self): |
| 26 | + self.execute_script('window.close()') |
| 27 | + |
| 28 | + ''' Switches between tabs in the browser ''' |
| 29 | + def switch_window(self, index): |
| 30 | + window = self.driver.window_handles[index] |
| 31 | + self.driver.switch_to.window(window) |
| 32 | + |
| 33 | + @pytest.fixture(scope="class", autouse=True) |
| 34 | + def create_test_project(self, tester_login): |
| 35 | + """Makes sure we are logged in and have a project open before |
| 36 | + performing any of these tests.""" |
| 37 | + self.create_sketch('public' , TEST_SKETCH, '') |
| 38 | + |
| 39 | + def test_open_tab(self): |
| 40 | + self.sketch_url = self.execute_script('return location.href') |
| 41 | + self.open_tab(self.sketch_url) |
| 42 | + WebDriverWait(self.driver, TIMEOUT['LOCATE_ELEMENT']).until( |
| 43 | + expected_conditions.invisibility_of_element_located( |
| 44 | + (By.CSS_SELECTOR, "#editor-loading-screen") |
| 45 | + ) |
| 46 | + ) |
| 47 | + WebDriverWait(self.driver, TIMEOUT['LOCATE_ELEMENT']).until( |
| 48 | + expected_conditions.element_to_be_clickable( |
| 49 | + (By.CSS_SELECTOR, "#editor_heading_project_name") |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + def test_create_file(self): |
| 54 | + self.switch_window(0) |
| 55 | + self.create_file(TEST_FILE) |
| 56 | + |
| 57 | + def test_check_created_file(self): |
| 58 | + self.switch_window(1) |
| 59 | + test_file = self.get_element(By.CSS_SELECTOR, '#files_list .filelist[data-name="{}"]'.format(TEST_FILE)) |
| 60 | + filename = test_file.text.strip() |
| 61 | + assert filename == TEST_FILE |
| 62 | + test_file.click() |
| 63 | + self.execute_script('editor.aceEditor.setValue("{}");'.format(TEST_INPUT), 'editor') |
| 64 | + |
| 65 | + def test_check_contents_change(self): |
| 66 | + self.switch_window(0) |
| 67 | + test_input = self.execute_script('return editor.aceEditor.getValue();') |
| 68 | + assert TEST_INPUT == test_input |
| 69 | + |
| 70 | + def test_create_and_delete_file(self): |
| 71 | + self.switch_window(0) |
| 72 | + self.create_file(TEST_EMPTY_FILE) |
| 73 | + self.switch_window(1) |
| 74 | + test_file = self.get_element(By.CSS_SELECTOR, '#files_list .filelist[data-name="{}"]'.format(TEST_EMPTY_FILE)) |
| 75 | + filename = test_file.text.strip() |
| 76 | + assert filename == TEST_EMPTY_FILE |
| 77 | + test_file.click() |
| 78 | + self.execute_script('editor.aceEditor.setValue("{}");'.format(TEST_INPUT), 'editor') |
| 79 | + self.remove_file(filename) |
| 80 | + self.switch_window(0) |
| 81 | + WebDriverWait(self.driver, TIMEOUT['LOCATE_ELEMENT']).until( |
| 82 | + expected_conditions.alert_is_present() |
| 83 | + ) |
| 84 | + Alert(self.driver).dismiss() |
| 85 | + |
| 86 | + def test_check_deleted_file(self): |
| 87 | + self.switch_window(0) |
| 88 | + deleted_file = self.driver.find_elements_by_css_selector('#files_list .filelist[data-name="{}"]'.format(TEST_EMPTY_FILE)) |
| 89 | + assert len(deleted_file) == 0 |
| 90 | + self.create_file(TEST_EMPTY_FILE) |
| 91 | + empty_contents = self.execute_script('return editor.aceEditor.getValue();', 'editor') |
| 92 | + assert empty_contents == '' |
| 93 | + |
| 94 | + def test_rename_file(self): |
| 95 | + self.switch_window(1) |
| 96 | + self.create_file(TEST_RENAME_FILE) |
| 97 | + self.execute_script('editor.aceEditor.setValue("int a = 1;");', 'editor') |
| 98 | + self.execute_script('editor.aceEditor.session.selection.moveCursorToPosition({row: 0, column: 3}); editor.aceEditor.session.selection.clearSelection();', 'editor') |
| 99 | + self.switch_window(0) |
| 100 | + self.get_element(By.CSS_SELECTOR, '#files_list .filelist[data-name="{}"]'.format(TEST_RENAME_FILE)).click() |
| 101 | + self.rename_file(TEST_RENAME_FILE, TEST_RENAMED_FILE) |
| 102 | + self.switch_window(1) |
| 103 | + self.get_element(By.CSS_SELECTOR, '#files_list .filelist[data-name="{}"]'.format(TEST_RENAMED_FILE)) |
| 104 | + |
| 105 | + def test_remove_sketch(self): |
| 106 | + self.switch_window(1) |
| 107 | + self.remove_sketch_editor() |
| 108 | + self.switch_window(0) |
| 109 | + WebDriverWait(self.driver, TIMEOUT['LOCATE_ELEMENT']).until( |
| 110 | + expected_conditions.alert_is_present() |
| 111 | + ) |
| 112 | + Alert(self.driver).dismiss() |
| 113 | + self.logout() |
0 commit comments