def test_generate_file_structure_dict(self, random_project_fixture): project = random_project_fixture['name'] testdir = random_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_one') page_object.new_page_object(testdir, project, ['module'], 'page_two', add_parents=True) full_path = page_object.pages_base_dir(testdir, project) file_structure = file_manager.generate_file_structure_dict(full_path) expected_result = { 'type': 'directory', 'name': 'pages', 'dot_path': '.', 'sub_elements': [ { 'type': 'directory', 'name': 'module', 'dot_path': 'module', 'sub_elements': [ { 'type': 'file', 'name': 'page_two', 'dot_path': 'module.page_two', 'sub_elements': [] } ] }, { 'type': 'file', 'name': 'page_one', 'dot_path': 'page_one', 'sub_elements': [] } ] } assert file_structure == expected_result
def test_new_page_object_page_exists(self, project_session): _, project = project_session.activate() page_name = 'page_name_x3' parents = ['new', 'page', 'object'] page_object.new_page_object(project, parents, page_name) error = page_object.new_page_object(project, parents, page_name) assert error == ['A page file with that name already exists']
def test_save_page_object(self, project_session): _, project = project_session.activate() page_path = os.path.join(project_session.path, 'pages', 'testa', 'testb', 'page_test987.py') page_object.new_page_object(project, ['testa', 'testb'], 'page_test987') page_name = 'testa.testb.page_test987' elements = [{ 'name': 'a', 'selector': 'id', 'value': 'b', 'display_name': 'a' }, { 'name': 'c', 'selector': 'id', 'value': 'd', 'display_name': '' }] functions = ["def func1(a, b):\n print(a, b)\n"] import_lines = ['import time', 'from golem import browser'] page_object.save_page_object(project, page_name, elements, functions, import_lines) expected_contents = ('import time\n' 'from golem import browser\n' '\n' '\n' 'a = (\'id\', \'b\', \'a\')\n' '\n' 'c = (\'id\', \'d\', \'c\')\n' '\n' 'def func1(a, b):\n' ' print(a, b)\n') with open(page_path) as page_file: contents = page_file.read() assert contents == expected_contents
def test_save_page_object_code(self, permanent_project_fixture): project = permanent_project_fixture['name'] testdir = permanent_project_fixture['testdir'] page_name = 'page_name_x1' parents = ['save', 'page', 'code'] page_object.new_page_object(testdir, project, parents, page_name, add_parents=True) page_code = ("elem1 = ('id', 'x')\n" "elem2 = ('id', 'y')\n" "def func1():\n" " pass") full_page_name = '{}.{}'.format('.'.join(parents), page_name) page_object.save_page_object_code(testdir, project, full_page_name, page_code) full_path = os.path.join(testdir, 'projects', project, 'pages', os.sep.join(parents), page_name + '.py') with open(full_path) as page_file: content = page_file.read() assert content == page_code
def test_generate_file_structure_dict(self, project_function): project = project_function.name testdir = project_function.testdir page_object.new_page_object(testdir, project, [], 'page_one') page_object.new_page_object(testdir, project, ['module'], 'page_two') full_path = page_object.pages_base_dir(testdir, project) file_structure = file_manager.generate_file_structure_dict(full_path) expected_result = { 'type': 'directory', 'name': 'pages', 'dot_path': '.', 'sub_elements': [{ 'type': 'directory', 'name': 'module', 'dot_path': 'module', 'sub_elements': [{ 'type': 'file', 'name': 'page_two', 'dot_path': 'module.page_two', 'sub_elements': [] }] }, { 'type': 'file', 'name': 'page_one', 'dot_path': 'page_one', 'sub_elements': [] }] } assert file_structure == expected_result
def test_get_files_dot_path(self, project_function): _, project = project_function.activate() page_object.new_page_object(project, [], 'page1') page_object.new_page_object(project, ['dir', 'subdir'], 'page2') base_path = os.path.join(project_function.path, 'pages') dot_files = file_manager.get_files_dot_path(base_path) expected_result = ['page1', 'dir.subdir.page2'] assert dot_files == expected_result
def test_new_page_object(self, project_session): _, project = project_session.activate() page_name = 'page_name_x2' parents = ['new', 'page', 'object'] page_object.new_page_object(project, parents, page_name) full_path = os.path.join(project_session.path, 'pages', os.sep.join(parents), page_name + '.py') assert os.path.isfile(full_path)
def test_new_page_object_into_subdirectory(self, project_session): _, project = project_session.activate() page_name = 'page_name_x3' parents = ['subdir'] page_object.new_page_object(project, parents, page_name) init_path = os.path.join(project_session.path, 'pages', 'subdir', '__init__.py') assert os.path.isfile(init_path)
def test_new_page_object_into_subdirectory(self, project_session): testdir = project_session['testdir'] project = project_session['name'] page_name = 'page_name_x3' parents = ['subdir'] page_object.new_page_object(testdir, project, parents, page_name) init_path = os.path.join(testdir, 'projects', project, 'pages', 'subdir', '__init__.py') assert os.path.isfile(init_path)
def test_new_page_object_page_exists(self, project_session): testdir = project_session['testdir'] project = project_session['name'] page_name = 'page_name_x3' parents = ['new', 'page', 'object'] page_object.new_page_object(testdir, project, parents, page_name) error = page_object.new_page_object(testdir, project, parents, page_name) assert error == ['A page file with that name already exists']
def test_new_page_object(self, project_session): testdir = project_session['testdir'] project = project_session['name'] page_name = 'page_name_x2' parents = ['new', 'page', 'object'] page_object.new_page_object(testdir, project, parents, page_name) full_path = os.path.join(testdir, 'projects', project, 'pages', os.sep.join(parents), page_name + '.py') assert os.path.isfile(full_path)
def test_duplicate_page_to_same_folder(self, project_function): _, project = project_function.activate() page_object.new_page_object(project, [], 'page1') errors = utils.duplicate_element(project, 'page', 'page1', 'page2') assert errors == [] path = os.path.join(project_function.path, 'pages', 'page1.py') assert os.path.isfile(path) path = os.path.join(project_function.path, 'pages', 'page2.py') assert os.path.isfile(path)
def test_get_files_dot_path_with_extension(self, project_function): _, project = project_function.activate() page_object.new_page_object(project, [], 'page2') base_path = os.path.join(project_function.path, 'pages') another_extension = os.path.join(base_path, 'another.json') open(another_extension, 'w+').close() dot_files = file_manager.get_files_dot_path(base_path, extension='.py') expected_result = ['page2'] assert dot_files == expected_result
def test_new_page_object(self, permanent_project_fixture): project = permanent_project_fixture['name'] testdir = permanent_project_fixture['testdir'] page_name = 'page_name_x2' parents = ['new', 'page', 'object'] page_object.new_page_object(testdir, project, parents, page_name) full_path = os.path.join(testdir, 'projects', project, 'pages', os.sep.join(parents), page_name + '.py') assert os.path.isfile(full_path)
def test_page_exists(self, permanent_project_fixture): project = permanent_project_fixture['name'] testdir = permanent_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_x001_exist') exists = page_object.page_exists(testdir, project, 'page_x001_exist') not_exists = page_object.page_exists(testdir, project, 'page_x001_not_exist') assert exists assert not not_exists
def test_page_exists(self, project_session): testdir = project_session.testdir project = project_session.name page_object.new_page_object(testdir, project, [], 'page_x001_exist') exists = page_object.page_exists(testdir, project, 'page_x001_exist') not_exists = page_object.page_exists(testdir, project, 'page_x001_not_exist') assert exists assert not not_exists
def test_delete_pages(self, project_function): _, project = project_function.activate() page_object.new_page_object(project, [], 'page1') page_object.new_page_object(project, [], 'page2') errors = utils.delete_element(project, 'page', 'page1') assert errors == [] path = os.path.join(project_function.path, 'pages', 'page1.py') assert not os.path.exists(path) path = os.path.join(project_function.path, 'pages', 'page2.py') assert os.path.exists(path)
def test_get_files_dot_path_with_extension(self, project_function): testdir = project_function['testdir'] project = project_function['name'] page_object.new_page_object(testdir, project, [], 'page2') base_path = os.path.join(testdir, 'projects', project, 'pages') another_extension = os.path.join(base_path, 'another.json') open(another_extension, 'w+').close() dot_files = file_manager.get_files_dot_path(base_path, extension='.py') expected_result = ['page2'] assert dot_files == expected_result
def test_rename_file_destination_exist(self, random_project_fixture): project = random_project_fixture['name'] testdir = random_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_two') page_object.new_page_object(testdir, project, [], 'destination') base_dir = page_object.pages_base_dir(testdir, project) error = file_manager.rename_file(base_dir, 'page_one.py', base_dir, 'destination.py') expected_error = ('File {} already exists' .format(os.path.join(base_dir, 'destination.py'))) assert error == expected_error
def test_rename_file(self, random_project_fixture): project = random_project_fixture['name'] testdir = random_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_one') base_dir = page_object.pages_base_dir(testdir, project) new_path = os.path.join(base_dir, 'submodule') error = file_manager.rename_file(base_dir, 'page_one.py', new_path, 'page_one_edit.py') new_full_path = os.path.join(new_path, 'page_one_edit.py') assert os.path.isfile(new_full_path) assert error == ''
def test_duplicate_page_to_same_folder(self, project_function): testdir = project_function['testdir'] project = project_function['name'] os.chdir(testdir) page_object.new_page_object(testdir, project, [], 'page1') errors = utils.duplicate_element(testdir, project, 'page', 'page1', 'page2') assert errors == [] path = os.path.join(testdir, 'projects', project, 'pages', 'page1.py') assert os.path.isfile(path) path = os.path.join(testdir, 'projects', project, 'pages', 'page2.py') assert os.path.isfile(path)
def test_get_files_dot_path(self, project_function): testdir = project_function['testdir'] project = project_function['name'] page_object.new_page_object(testdir, project, [], 'page1') page_object.new_page_object(testdir, project, ['dir', 'subdir'], 'page2') base_path = os.path.join(testdir, 'projects', project, 'pages') dot_files = file_manager.get_files_dot_path(base_path) expected_result = [ 'page1', 'dir.subdir.page2' ] assert dot_files == expected_result
def test_delete_pages(self, project_function): testdir = project_function.testdir project = project_function.name os.chdir(testdir) page_object.new_page_object(testdir, project, [], 'page1') page_object.new_page_object(testdir, project, [], 'page2') errors = utils.delete_element(testdir, project, 'page', 'page1') assert errors == [] path = os.path.join(project_function.path, 'pages', 'page1.py') assert not os.path.exists(path) path = os.path.join(project_function.path, 'pages', 'page2.py') assert os.path.exists(path)
def test_duplicate_page(self, project_function): testdir = project_function.testdir project = project_function.name os.chdir(testdir) page_object.new_page_object(testdir, project, [], 'page1') errors = utils.duplicate_element(testdir, project, 'page', 'page1', 'sub.page2') assert errors == [] path = os.path.join(project_function.path, 'pages', 'page1.py') assert os.path.isfile(path) path = os.path.join(project_function.path, 'pages', 'sub', 'page2.py') assert os.path.isfile(path)
def test_get_page_object_code(self, project_session): _, project = project_session.activate() page_name = 'page_test_get_code_ab8456' page_object.new_page_object(project, [], page_name) page_path = os.path.join(project_session.path, 'pages', page_name + '.py') file_content = 'test=("id", "xyz")\ntest2=("id", "abc")\n' with open(page_path, 'w') as page_file: page_file.write(file_content) code = page_object.get_page_object_code(page_path) assert code == file_content
def test_get_page_object_code(self, permanent_project_fixture): project = permanent_project_fixture['name'] testdir = permanent_project_fixture['testdir'] page_name = 'page_test_get_code_ab8456' page_object.new_page_object(testdir, project, [], page_name) page_path = os.path.join(testdir, 'projects', project, 'pages', page_name + '.py') file_content = 'test=("id", "xyz")\ntest2=("id", "abc")\n' with open(page_path, 'w') as page_file: page_file.write(file_content) code = page_object.get_page_object_code(page_path) assert code == file_content
def test_get_files_dot_path(self, testdir_fixture): project = helper_functions.create_random_project( testdir_fixture['path']) # create a new page object in pages folder page_object.new_page_object(testdir_fixture['path'], project, [], 'page1') # create a new page object in pages/dir/subdir/ page_object.new_page_object(testdir_fixture['path'], project, ['dir', 'subdir'], 'page2') base_path = os.path.join(testdir_fixture['path'], 'projects', project, 'pages') dot_files = file_manager.get_files_dot_path(base_path) expected_result = ['page1', 'dir.subdir.page2'] assert dot_files == expected_result
def test_get_files_dot_path_extension(self, testdir_fixture): project = helper_functions.create_random_project(testdir_fixture['path']) testdir = testdir_fixture['path'] # create a new page object in pages folder (.py extension) page_object.new_page_object(testdir, project, [], 'page2') # create a file with another extension another_extension = os.path.join(testdir, 'projects', project, 'pages', 'another.json') with open(another_extension, 'w') as another_file: another_file.write('') base_path = os.path.join(testdir_fixture['path'], 'projects', project, 'pages') dot_files = file_manager.get_files_dot_path(base_path, extension='.py') expected_result = ['page2'] assert dot_files == expected_result
def test_get_page_object_content(self, permanent_project_fixture): project = permanent_project_fixture['name'] testdir = permanent_project_fixture['testdir'] page_name = 'page_test_get_content_ab1412' page_object.new_page_object(testdir, project, [], page_name, add_parents=False) page_path = os.path.join(testdir, 'projects', project, 'pages', page_name + '.py') with open(page_path, 'w') as page_file: page_file.write('elem1 = (\'id\', \'someId\', \'Elem1\')\n') page_file.write('def func1(c, b, a):\n') page_file.write(' pass') content = page_object.get_page_object_content(project, page_name) expected = { 'functions': [{ 'function_name': 'func1', 'full_function_name': 'page_test_get_content_ab1412.func1', 'description': None, 'arguments': ['c', 'b', 'a'], 'code': 'def func1(c, b, a):\n pass\n' }], 'elements': [{ 'element_name': 'elem1', 'element_selector': 'id', 'element_value': 'someId', 'element_display_name': 'Elem1', 'element_full_name': 'page_test_get_content_ab1412.elem1' }], 'import_lines': [], 'code_lines': [ "elem1 = ('id', 'someId', 'Elem1')", 'def func1(c, b, a):', ' pass', '' ], 'source_code': ("elem1 = ('id', 'someId', 'Elem1')\ndef func1(c, b, a):\n" " pass\n") } assert content == expected
def new_tree_element(): if request.method == 'POST': project = request.form['project'] elem_type = request.form['elementType'] is_dir = json.loads(request.form['isDir']) full_path = request.form['fullPath'] add_parents = request.form['addParents'] full_path = full_path.replace(' ', '_') dot_path = full_path errors = [] full_path = full_path.split('.') element_name = full_path.pop() parents = full_path # verify that the string only contains letters, numbers # dashes or underscores for c in element_name: if not c.isalnum() and not c in ['-', '_']: errors.append('Only letters, numbers, \'-\' and \'_\' are allowed') break if not errors: if elem_type == 'test': if is_dir: errors = file_manager.new_directory_of_type(root_path, project, parents, element_name, dir_type='tests') else: errors = test_case.new_test_case(root_path, project, parents, element_name) # changelog.log_change(root_path, project, 'CREATE', 'test', # full_path, g.user.username) elif elem_type == 'page': if is_dir: errors = file_manager.new_directory_of_type(root_path, project, parents, element_name, dir_type='pages') else: errors = page_object.new_page_object(root_path, project, parents, element_name, add_parents=add_parents) elif elem_type == 'suite': if is_dir: errors = file_manager.new_directory_of_type(root_path, project, parents, element_name, dir_type='suites') else: errors = suite_module.new_suite(root_path, project, parents, element_name) element = { 'name': element_name, 'full_path': dot_path, 'type': elem_type, 'is_directory': is_dir } return json.dumps({'errors': errors, 'project_name': project, 'element': element})
def new_tree_element(): if request.method == 'POST': project = request.form['project'] elem_type = request.form['elementType'] is_dir = json.loads(request.form['isDir']) full_path = request.form['fullPath'] add_parents = request.form['addParents'] full_path = full_path.replace(' ', '_') dot_path = full_path errors = [] full_path = full_path.split('.') element_name = full_path.pop() parents = full_path # verify that the string only contains letters, numbers # dashes or underscores for c in element_name: if not c.isalnum() and not c in ['-', '_']: errors.append('Only letters, numbers, \'-\' and \'_\' are allowed') break if not errors: if elem_type == 'test': if is_dir: errors = io_manager.new_directory(root_path, project, parents, element_name, dir_type='tests') else: errors = test_case.new_test_case(root_path, project, parents, element_name) # changelog.log_change(root_path, project, 'CREATE', 'test', # full_path, g.user.username) elif elem_type == 'page': if is_dir: errors = io_manager.new_directory(root_path, project, parents, element_name, dir_type='pages') else: errors = page_object.new_page_object(root_path, project, parents, element_name, add_parents=add_parents) elif elem_type == 'suite': if is_dir: errors = io_manager.new_directory(root_path, project, parents, element_name, dir_type='suites') else: errors = suite_module.new_suite(root_path, project, parents, element_name) element = { 'name': element_name, 'full_path': dot_path, 'type': elem_type, 'is_directory': is_dir } return json.dumps({'errors': errors, 'project_name': project, 'element': element})
def test_get_page_object_content(self, testdir_fixture, random_project_fixture): project = random_project_fixture['name'] page_name = 'page_test_get_content' page_object.new_page_object(testdir_fixture['path'], project, [], page_name, add_parents=False) page_path = os.path.join(testdir_fixture['path'], 'projects', project, 'pages', page_name + '.py') with open(page_path, 'w') as page_file: page_file.write('elem1 = (\'id\', \'someId\', \'Elem1\')\n') page_file.write('def func1(c, b, a):\n') page_file.write(' pass') #sys.path.append(testdir_fixture['path']) content = page_object.get_page_object_content(project, page_name) expected = { 'function_list': [ { 'function_name': 'func1', 'full_function_name': 'page_test_get_content.func1', 'description': None, 'arguments': ['c', 'b', 'a'], 'code': 'def func1(c, b, a):\n pass\n' }], 'element_list': [ { 'element_name': 'elem1', 'element_selector': 'id', 'element_value': 'someId', 'element_display_name': 'Elem1', 'element_full_name': 'page_test_get_content.elem1' }], 'import_lines': [], 'code_line_list': ["elem1 = ('id', 'someId', 'Elem1')", 'def func1(c, b, a):', ' pass', ''], 'source_code': "elem1 = ('id', 'someId', 'Elem1')\ndef func1(c, b, a):\n pass\n" } assert content == expected
def test_get_files_in_directory_dot_path(self, testdir_fixture): project = helper_functions.create_random_project(testdir_fixture['path']) # create a new page object in pages folder page_object.new_page_object(testdir_fixture['path'], project, [], 'page1') # create a new page object in pages/dir/subdir/ page_object.new_page_object(testdir_fixture['path'], project, ['dir', 'subdir'], 'page2', add_parents=True) base_path = os.path.join(testdir_fixture['path'], 'projects', project, 'pages') dotted_files = utils.get_files_in_directory_dot_path(base_path) assert 'page1' in dotted_files assert 'dir.subdir.page2' in dotted_files assert len(dotted_files) == 2
def test_get_pages(self, testdir_fixture, project_fixture): page_object.new_page_object(testdir_fixture['path'], project_fixture['name'], ['subdir1', 'subdir2'], 'test3', add_parents=True) page_object.new_page_object(testdir_fixture['path'], project_fixture['name'], ['subdir1'], 'test2', add_parents=True) page_object.new_page_object(testdir_fixture['path'], project_fixture['name'], [], 'test1', add_parents=True) pages = utils.get_pages(testdir_fixture['path'], project_fixture['name']) expected_result = { 'type': 'directory', 'name': 'pages', 'dot_path': '.', 'sub_elements': [ { 'type': 'directory', 'name': 'subdir1', 'dot_path': 'subdir1', 'sub_elements': [ { 'type': 'directory', 'name': 'subdir2', 'dot_path': 'subdir1.subdir2', 'sub_elements': [ { 'type': 'file', 'name': 'test3', 'dot_path': 'subdir1.subdir2.test3', 'sub_elements': [] } ] }, { 'type': 'file', 'name': 'test2', 'dot_path': 'subdir1.test2', 'sub_elements': [] } ] }, { 'type': 'file', 'name': 'test1', 'dot_path': 'test1', 'sub_elements': [] } ] } assert pages == expected_result