Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. import html
  2. import os
  3. from string import Template
  4.  
  5. import pytest
  6. import requests
  7. from allure.constants import AttachmentType
  8.  
  9. from helpers.utils import change_host_for_mobile
  10. from settings import LANDING_URLS
  11. from tests.conftest import Severity
  12.  
  13.  
  14. @pytest.mark.timeout(2400)
  15. @pytest.allure.feature('JS url checker')
  16. @pytest.allure.severity(Severity.CRITICAL)
  17. class TestCheckUrlsJs:
  18.     @pytest.mark.categories('landings', component='js_check', suite='smoke', site=['desktop'])
  19.     @pytest.allure.story('Check landing urls')
  20.     def test_check_landing_urls_js(self, driver, platform):
  21.         with pytest.allure.step('Load landing urls'):
  22.             self.set_cookie(driver, platform)
  23.             urls = self._get_landing_urls(platform)
  24.             assert urls, "Landing urls not found"
  25.  
  26.         with pytest.allure.step('Check landing pages fo js errors'):
  27.             pages_with_error = ''
  28.             for url in urls:
  29.                 log = self.get_browser_log(driver, url)
  30.                 level = 'default'
  31.                 if 0 != len(log):
  32.                     page_id = 'm_' + url.split('/')[-2] if url.endswith('mob') else url.split('/')[-2]
  33.                     log_msg = ''
  34.                     for msg in log:
  35.                         if 'LMDA' in msg['message']:
  36.                             level = 'danger'
  37.                         p_tag = '\t\t\t\t\t<p class="bg-%s">' % level + html.escape(msg['message']) + '</p>\n'
  38.                         log_msg += p_tag
  39.                     pages_with_error += self.__err_msg(page_id, log_msg, url, level)
  40.             if '' != pages_with_error:
  41.                 pages_with_error = self.__report(pages_with_error)
  42.             pytest.allure.attach('Browser logs', pages_with_error, AttachmentType.HTML)
  43.             assert not pages_with_error, 'Some errors occurred while opening pages, see browser logs for details.'
  44.  
  45.     @staticmethod
  46.     def get_browser_log(driver, url):
  47.         driver.get(url)
  48.         return driver.get_log('browser')
  49.  
  50.     @staticmethod
  51.     def set_cookie(driver, platform):
  52.         driver.get(platform.host)
  53.         domain = [cookie['domain'] for cookie in driver.get_cookies() if cookie['name'] == 'LMDA'][0]
  54.         driver.add_cookie({'name': 'landing_host', 'value': LANDING_URLS, 'domain': domain})
  55.  
  56.     @staticmethod
  57.     def _get_landing_urls(platform):
  58.         """Parse pages.json, make url with platform and returns list of urls
  59.        :param platform: domain and country from settings
  60.               branch: landings host from settings
  61.        :return: list of urls
  62.        """
  63.         landing_urls = f'{LANDING_URLS}/pages.json'
  64.         response = requests.get(landing_urls)
  65.         assert response.status_code == 200, "Can not load landing urls"
  66.         pages = response.json()
  67.         urls = []
  68.         for path, sites in pages.items():
  69.             for site in sites.keys():
  70.                 if site[:2] == platform.country:
  71.                     urls.append("{host}{path}?sv={factor}".format(
  72.                         host=change_host_for_mobile(platform.host) if site[3:] == 'mobile' else platform.host,
  73.                         path=path,
  74.                         factor='mob' if site[3:] == 'mobile' else 'dsk'))
  75.         return urls
  76.  
  77.     @staticmethod
  78.     def __err_msg(page_id, log_msg, url, level='default'):
  79.         with open(os.path.join(os.path.dirname(__file__), "js_error_div_template.html"), "r") as div_template:
  80.             err_div = Template(div_template.read())
  81.         return err_div.safe_substitute(page_id=page_id, message=log_msg, page_url=url, level=level)
  82.  
  83.     @staticmethod
  84.     def __report(err_blocks):
  85.         with open(os.path.join(os.path.dirname(__file__), "js_errors_report_template.html"), "r") as report_template:
  86.             report = Template(report_template.read())
  87.         return report.safe_substitute(errors=err_blocks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement