예제 #1
0
def adding_result_in_test_rail():
    yield
    step = Steps()
    test_status = StatusTest.PASSED.value
    url_image = None
    try:
        with allure.step(
                "Uploading a picture for Testail to a remote resource"):
            step.upload_screenshot_to_the_cloudinary_service()
            url_image = step.get_url_uploaded_image_from_cloudinary_service()
    except FileNotFoundError:
        with allure.step(
                "Change test status to 'Failed'. Takes screenshot "
                "and uploading a picture for Testail to a remote resource"):
            step.take_screenshot()
            test_status = StatusTest.FAILED.value
            step.upload_screenshot_to_the_cloudinary_service()
            url_image = step.get_url_uploaded_image_from_cloudinary_service()
    finally:
        with allure.step("Sending test result to Testrail and clear work dir"):
            result_fields = {
                KeysTestRail.STATUS_ID.value: test_status,
                KeysTestRail.COMMENT.value: url_image
            }
            step.put_results_of_the_test_case(TestRail.RUN_ID.value,
                                              TestRail.CASE_ID.value,
                                              result_fields)
            step.clear_dir(ConfigProject.get_instance().name_screen())
예제 #2
0
def database_connect():
    conf_project = ConfigProject.get_instance()
    database = DatabaseConnect(conf_project.host(), conf_project.port(),
                               conf_project.db_login(),
                               conf_project.db_password(),
                               conf_project.db_name())
    yield database.get_connect()
    database.close_connect()
class ReportingPortalApiUtils:
    _URL = ConfigProject.get_instance().url_api()
    _TOKEN_GENERATION = "/token/get"
    _GET_JSON = "/test/get/json"

    def get_token(self, variant):
        response = requests.post(url=self._URL + self._TOKEN_GENERATION, params={"variant": variant})
        return ResponseApi(response.status_code, response.text)

    def get_list_project_tests_in_json_format(self, project_id):
        response = requests.post(url=self._URL + self._GET_JSON, params={"projectId": project_id})
        return ResponseApi(response.status_code, response)
예제 #4
0
class TestRailAPI:
    config_project = ConfigProject.get_instance()
    _login = config_project.test_rail_login()
    _password = config_project.test_rail_password()
    _HEADERS = {'Content-Type': 'application/json'}
    _SLASH = "/"
    _URL = "https://tr.a1qa.com/"
    _PATH_API = "index.php?/api/v2/"
    _ADD_RESULT_FOR_CASE = "add_result_for_case/"

    def add_result_for_case(self, run_id, case_id, result_fields):
        url = self._URL + self._PATH_API + self._ADD_RESULT_FOR_CASE + str(run_id) + self._SLASH + str(case_id)
        response = requests.post(url=url, headers=self._HEADERS, auth=HTTPBasicAuth(self._login, self._password),
                                 json=result_fields)
        return response.status_code
예제 #5
0
 def log_in(self, login, password):
     self._browser.get(ConfigProject.get_instance().url_auth().format(
         login, password))
예제 #6
0
class Test:
    step = Steps()
    conf_project = ConfigProject.get_instance()
    text_gen = TextGenerator()

    @allure.title("Variant - 1")
    @pytest.mark.parametrize("variant, cookie, existing_project_name, new_project_name, "
                             "test_name, test_method, env, content_log, is_exception",
                             [
                                 (Variant.ONE.value, {ConstTestCase.TOKEN.value: ""}, "Nexage", text_gen.generate(),
                                  text_gen.generate(), text_gen.generate(), text_gen.generate(), text_gen.generate(),
                                  IsException.NO.value)
                             ])
    def test_variant_1(self, cursor, variant, cookie, existing_project_name, new_project_name,
                       test_name, test_method, env, content_log, is_exception):
        with allure.step("Step 1. By request to api, get a token according to the option number"):
            token = self.step.get_token(variant)
            self.step.token_is_generated()

        with allure.step("Step 2. Go to the site. Pass the necessary authorization. Using a cookie, "
                         "pass the token generated in step 1 (token parameter). Refresh the page."):
            self.step.open_website()
            self.step.log_in(self.conf_project.web_login(), self.conf_project.web_password())
            self.step.projects_page_is_opened()
            cookie.update({ConstTestCase.TOKEN.value: token})
            self.step.add_cookie(cookie)
            self.step.refresh_page()
            self.step.is_in_footer_indicated_correct_number(variant)

        with allure.step("Step 3. Go to the Nexage project page. "
                         "A request to api to get a list of tests in JSON format."):
            self.step.go_to_project_page(existing_project_name)
            self.step.is_page_project_open(existing_project_name)
            list_tests_from_first_page_ui = self.step.get_list_tests_from_first_page_ui()
            id_existing_project = self.step.get_project_id_by_name_project_from_db(cursor, existing_project_name)
            list_of_tests_on_request_api = self.step.get_list_of_tests_on_request_api(id_existing_project)
            self.step.is_list_of_tests_on_request_api_in_format_json(list_of_tests_on_request_api)
            self.step.is_tests_on_the_first_page_are_sorted_by_date(list_tests_from_first_page_ui)
            self.step.is_tests_on_first_page_correspond_to_those_that_returned_request_for_api(
                list_tests_from_first_page_ui, list_of_tests_on_request_api)

        with allure.step("Step 4. Return to the previous page in the browser (project page). "
                         "Click on + Add. Enter the name of the project, and save. "
                         "To close the add project window, call the closePopUp () js method. Refresh the page"):
            self.step.return_to_previous_page()
            self.step.click_button_add()
            self.step.is_add_project_form_open()
            self.step.switch_to_iframe()
            self.step.enter_project_name(new_project_name)
            self.step.click_button_submit()
            self.step.is_message_about_successful_save_appeared()
            self.step.switch_to_window()
            self.step.close_add_project_window()
            self.step.is_add_project_form_closed()
            self.step.refresh_page()
            self.step.is_project_appeared_on_the_list_projects(new_project_name)

        with allure.step("Step 5. Go to the page of the created project. Add a test through the database "
                         "(along with the log and screenshot of the current page)."):
            self.step.go_to_project_page(new_project_name)
            content_image = self.step.get_screenshot_as_png()
            id_new_project = self.step.get_project_id_by_name_project_from_db(cursor,
                                                                              new_project_name)
            list_status_id = self.step.get_list_all_status_id_from_db(cursor)
            status_id = self.step.get_random_status_id(list_status_id)
            status_name = self.step.get_name_status_by_id(cursor, status_id)
            list_session_id = self.step.get_list_all_session_id_from_db(cursor)
            session_id = self.step.get_random_session_id(list_session_id)
            self.step.add_test_via_db(cursor, id_new_project, test_name, test_method, status_id, session_id, env)
            test_id = self.step.get_test_id_by_test_name_from_db(cursor, test_name)
            self.step.add_log_in_db(cursor, content_log, is_exception, test_id)
            self.step.add_attachment_in_db(cursor, self.conf_project.name_screen(), test_id, content_image)
            self.step.is_test_appeared_on_the_list_tests(test_name)
            test_data = TestModel(new_project_name, test_name, test_method, status_name, env,
                                  self.conf_project.name_screen(), content_log)

        with allure.step("Step 6. Go to the page of the created test. Check the information is correct."):
            self.step.go_to_test_page(test_name)
            test_data_from_page = TestModel()
            self.step.get_all_field_test_page_write_to_test_model(test_data_from_page)
            self.step.check_information_is_correct_from_test_page(test_data, test_data_from_page)
            download_image = self.step.get_content_from_attachment_db(cursor, test_id)
            self.step.is_screenshot_corresponds_to_the_sent(content_image, download_image)
            self.step.take_screenshot()
예제 #7
0
 def open(self):
     self._browser.get(ConfigProject.get_instance().url_web())
예제 #8
0
class Steps:
    config_project = ConfigProject.get_instance()
    projects_page = ProjectsPage()
    modal_window = ModalWindow()
    authorization = Authorization()
    rep_portal = ReportingPortalApiUtils()
    response_api = None
    cookie = Cookie()
    browser_navigate = BrowserNavigate()
    window_handles = WindowHandles()
    j_s_executor = JSExecutor()
    project = Project()
    test_db = TestDB()
    session = Session()
    status_db = StatusDB()
    log = Log()
    attachment = Attachment()
    page_project = PageProject()
    screenshot = ScreenShot()
    test_page = TestPage()
    cloudinary = Cloudinary(config_project.cloud_name_cloudinary(),
                            config_project.api_key_cloudinary(),
                            config_project.api_secret_cloudinary())
    testrail_api = TestRailAPI()

    def open_website(self):
        self.projects_page.open()

    def log_in(self, login, password):
        self.authorization.log_in(login, password)

    def get_token(self, variant):
        self.response_api = self.rep_portal.get_token(variant)
        return self.response_api.get_response()

    def token_is_generated(self):
        self.is_status_code_ok()
        assert self.response_api.get_response(), "Token is empty"

    def is_status_code_ok(self):
        assert self.response_api.get_status_code() == HTTPStatus.OK.value, \
            "Status code: not equal: {}".format(HTTPStatus.OK.value)

    def add_cookie(self, cookies):
        self.cookie.add_cookies(cookies)

    def refresh_page(self):
        self.browser_navigate.refresh()

    def projects_page_is_opened(self):
        assert self.projects_page.page_is_opened(), "Project page did not open"

    def is_in_footer_indicated_correct_number(self, variant):
        version = int(''.join(
            filter(str.isdigit, self.get_number_version_from_page())))
        assert variant == version, "Variant number is not correct"

    def get_number_version_from_page(self):
        return self.projects_page.get_number_version()

    def go_to_project_page(self, project_name):
        self.projects_page.list_projects.select_item(project_name)

    def go_to_test_page(self, test_name):
        self.page_project.test_list.select_test(test_name)

    def is_project_appeared_on_the_list_projects(self, project_name):
        assert self.projects_page.list_projects.is_project_appeared_on_the_list(project_name), \
            "Project did not appear on the list"

    def is_test_appeared_on_the_list_tests(self, test_name):
        assert self.page_project.test_list.is_test_appeared_on_the_list(
            test_name), "Test did not appear on the list"

    def return_to_previous_page(self):
        self.browser_navigate.back()

    def get_list_tests_from_first_page_ui(self):
        return self.page_project.test_list.get_list_test()

    def list_test_start_time(self, list_tests_from_page_ui):
        list_start_time = []
        for index in range(3, len(list_tests_from_page_ui), 7):
            list_start_time.append(list_tests_from_page_ui[index].text)
        return list_start_time

    def list_test_name(self, list_tests_from_page_ui):
        list_name = []
        for index in range(3, len(list_tests_from_page_ui), 7):
            list_name.append(list_tests_from_page_ui[index].text)
        return list_name

    def is_tests_on_the_first_page_are_sorted_by_date(self,
                                                      list_tests_from_page_ui):
        is_sorted = True
        list_start_time = self.list_test_start_time(list_tests_from_page_ui)
        time_test = list_start_time[0]
        for time in list_start_time:
            if time_test < time:
                is_sorted = False
            time_test = time
        assert is_sorted, "Tests on the first page not sorted by date"

    def is_tests_on_first_page_correspond_to_those_that_returned_request_for_api(
            self, list_tests_from_page_ui, list_of_tests_on_request_api):
        is_math = True
        str_list_of_tests_on_request_api = str(
            list_of_tests_on_request_api.json())
        list_name = self.list_test_name(list_tests_from_page_ui)
        for test_name in list_name:
            if test_name not in str_list_of_tests_on_request_api:
                is_math = False
        assert is_math, "Tests on the first page do not match those returned by the api request"

    def get_list_of_tests_on_request_api(self, project_id):
        self.response_api = self.rep_portal.get_list_project_tests_in_json_format(
            project_id)
        self.is_status_code_ok()
        return self.response_api.get_response()

    def is_list_of_tests_on_request_api_in_format_json(
            self, list_of_tests_on_request_api):
        try:
            list_of_tests_on_request_api.json()
            assert True
        except json.decoder.JSONDecodeError:
            assert False, "List of tests on request api not in json format"

    def click_button_add(self):
        self.projects_page.click_button_add()

    def enter_project_name(self, text):
        self.projects_page.iframe.enter_text(text)

    def is_message_about_successful_save_appeared(self):
        assert self.projects_page.iframe.is_message_about_successful_save_appeared(), \
            "Message_about not_successful_save_appeared"

    def switch_to_iframe(self):
        self.window_handles.switch_to_iframe()

    def switch_to_window(self):
        self.window_handles.switch_to_main_page()

    def click_button_submit(self):
        self.projects_page.iframe.click_button_submit()

    def close_add_project_window(self):
        self.j_s_executor.execute_script(MethodsJS.CLOSE_WIN_ADD_PROJECT.value)

    def get_project_id_by_name_project_from_db(self, cursor, project_name):
        cursor_method = cursor.execute_query_select(
            self.project.select_id_by_name(project_name))
        project_id = cursor_method.fetchone()
        cursor_method.close()
        return project_id[KeysBD.ID.value]

    def add_test_via_db(self, cursor, project_id, name, method_name, status_id,
                        session_id, env):
        cursor.execute_query_insert(
            self.test_db.insert(project_id, name, method_name, status_id,
                                session_id, env))

    def get_test_id_by_test_name_from_db(self, cursor, test_name):
        cursor_method = cursor.execute_query_select(
            self.test_db.select_id_by_name(test_name))
        test_id = cursor_method.fetchone()
        cursor_method.close()
        return test_id[KeysBD.ID.value]

    def get_list_all_session_id_from_db(self, cursor):
        list_session_id = cursor.execute_query_select(
            self.session.select_id()).fetchall()
        return list_session_id

    def get_random_session_id(self, list_session_id):
        session_id = random.choice(list_session_id)
        return session_id[KeysBD.ID.value]

    def get_status_id_by_status_name_from_db(self, cursor, status_name):
        status_id = cursor.execute_query_select(
            self.status_db.select_id_by_name(status_name)).fetchone()
        return status_id[KeysBD.ID.value]

    def get_list_all_status_id_from_db(self, cursor):
        list_status_id = cursor.execute_query_select(
            self.status_db.select_id()).fetchall()
        return list_status_id

    def get_random_status_id(self, list_status_id):
        status_id = random.choice(list_status_id)
        return status_id[KeysBD.ID.value]

    def get_name_status_by_id(self, cursor, status_id):
        name = cursor.execute_query_select(
            self.status_db.select_name_by_id(status_id)).fetchone()
        return name[KeysBD.NAME.value]

    def add_log_in_db(self, cursor, content, is_exception, test_id):
        cursor.execute_query_insert(
            self.log.insert(content, is_exception, test_id))

    def add_attachment_in_db(self, cursor, content_type, test_id, image):
        cursor.execute_query_insert_image(
            self.attachment.insert(content_type, test_id), image)

    def get_content_from_attachment_db(self, cursor, test_id):
        content = cursor.execute_query_select(
            self.attachment.select_content(test_id)).fetchone()
        return content[KeysBD.CONTENT.value]

    def take_screenshot(self):
        self.screenshot.take_screenshot(self.config_project.name_screen())

    def get_screenshot_as_png(self):
        return self.screenshot.get_screenshot_as_png()

    def upload_screenshot_to_the_cloudinary_service(self):
        self.cloudinary.upload_image(self.config_project.name_screen())

    def get_url_uploaded_image_from_cloudinary_service(self):
        return self.cloudinary.get_url_uploaded_image()

    def download_image(self, image_name):
        download = open(image_name, "rb").read()
        return download

    def get_project_name_from_test_page(self):
        return self.test_page.get_project_name()

    def get_test_name_from_test_page(self):
        return self.test_page.get_test_name()

    def get_test_method_name_from_test_page(self):
        return self.test_page.get_test_method_name()

    def get_status_from_test_page(self):
        return self.test_page.get_status()

    def get_env_from_test_page(self):
        return self.test_page.get_env()

    def get_log_from_test_page(self):
        return self.test_page.get_log()

    def get_attachment_type_from_test_page(self):
        return self.test_page.get_attachment_type()

    def get_all_field_test_page_write_to_test_model(self, test_model):
        test_model.set_project_name(self.get_project_name_from_test_page())
        test_model.set_test_name(self.get_test_name_from_test_page())
        test_model.set_test_method_name(
            self.get_test_method_name_from_test_page())
        test_model.set_status(self.get_status_from_test_page())
        test_model.set_environment(self.get_env_from_test_page())
        test_model.set_log(self.get_log_from_test_page())
        test_model.set_attachment_type(
            self.get_attachment_type_from_test_page())

    def image_is_download_to_the_page(self):
        assert self.test_page.get_attachment_type(), "No image per page"

    def check_information_is_correct_from_test_page(self, test_model_1,
                                                    test_model_2):
        assert test_model_1 == test_model_2, "Information is not correct from test page"

    def is_screenshot_corresponds_to_the_sent(self, image1, image2):
        self.image_is_download_to_the_page()
        assert image1 == image2, "Screenshot does not match sent"

    def clear_dir(self, image):
        os.remove(image)

    def put_results_of_the_test_case(self, run_id, case_id, result_fields):
        return self.testrail_api.add_result_for_case(run_id, case_id,
                                                     result_fields)

    def is_page_project_open(self, project_name):
        assert self.page_project.page_project_is_open(
            project_name), "Project page is not open"

    def is_add_project_form_open(self):
        assert self.projects_page.iframe.is_iframe_open(
        ), "Project form not open"

    def is_add_project_form_closed(self):
        assert not self.projects_page.iframe.is_iframe_closed(
        ), "Project form not closed"