Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import html
- import os
- from string import Template
- import pytest
- import requests
- from allure.constants import AttachmentType
- from helpers.utils import change_host_for_mobile
- from settings import LANDING_URLS
- from tests.conftest import Severity
- @pytest.mark.timeout(2400)
- @pytest.allure.feature('JS url checker')
- @pytest.allure.severity(Severity.CRITICAL)
- class TestCheckUrlsJs:
- @pytest.mark.categories('landings', component='js_check', suite='smoke', site=['desktop'])
- @pytest.allure.story('Check landing urls')
- def test_check_landing_urls_js(self, driver, platform):
- with pytest.allure.step('Load landing urls'):
- self.set_cookie(driver, platform)
- urls = self._get_landing_urls(platform)
- assert urls, "Landing urls not found"
- with pytest.allure.step('Check landing pages fo js errors'):
- pages_with_error = ''
- for url in urls:
- log = self.get_browser_log(driver, url)
- level = 'default'
- if 0 != len(log):
- page_id = 'm_' + url.split('/')[-2] if url.endswith('mob') else url.split('/')[-2]
- log_msg = ''
- for msg in log:
- if 'LMDA' in msg['message']:
- level = 'danger'
- p_tag = '\t\t\t\t\t<p class="bg-%s">' % level + html.escape(msg['message']) + '</p>\n'
- log_msg += p_tag
- pages_with_error += self.__err_msg(page_id, log_msg, url, level)
- if '' != pages_with_error:
- pages_with_error = self.__report(pages_with_error)
- pytest.allure.attach('Browser logs', pages_with_error, AttachmentType.HTML)
- assert not pages_with_error, 'Some errors occurred while opening pages, see browser logs for details.'
- @staticmethod
- def get_browser_log(driver, url):
- driver.get(url)
- return driver.get_log('browser')
- @staticmethod
- def set_cookie(driver, platform):
- driver.get(platform.host)
- domain = [cookie['domain'] for cookie in driver.get_cookies() if cookie['name'] == 'LMDA'][0]
- driver.add_cookie({'name': 'landing_host', 'value': LANDING_URLS, 'domain': domain})
- @staticmethod
- def _get_landing_urls(platform):
- """Parse pages.json, make url with platform and returns list of urls
- :param platform: domain and country from settings
- branch: landings host from settings
- :return: list of urls
- """
- landing_urls = f'{LANDING_URLS}/pages.json'
- response = requests.get(landing_urls)
- assert response.status_code == 200, "Can not load landing urls"
- pages = response.json()
- urls = []
- for path, sites in pages.items():
- for site in sites.keys():
- if site[:2] == platform.country:
- urls.append("{host}{path}?sv={factor}".format(
- host=change_host_for_mobile(platform.host) if site[3:] == 'mobile' else platform.host,
- path=path,
- factor='mob' if site[3:] == 'mobile' else 'dsk'))
- return urls
- @staticmethod
- def __err_msg(page_id, log_msg, url, level='default'):
- with open(os.path.join(os.path.dirname(__file__), "js_error_div_template.html"), "r") as div_template:
- err_div = Template(div_template.read())
- return err_div.safe_substitute(page_id=page_id, message=log_msg, page_url=url, level=level)
- @staticmethod
- def __report(err_blocks):
- with open(os.path.join(os.path.dirname(__file__), "js_errors_report_template.html"), "r") as report_template:
- report = Template(report_template.read())
- return report.safe_substitute(errors=err_blocks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement