Exemplo n.º 1
0
class View_Examples:
    def __init__(self, tmp_img=None, clip=None, headless=False):
        self.headless = headless
        self.path_views = Files.path_combine(Files.parent_folder(__file__),
                                             '../../src/web_root')
        self.render_page = Render_Page(headless=self.headless,
                                       auto_close=self.headless,
                                       web_root=self.path_views)
        self.tmp_img = tmp_img
        self.clip = clip

    def _open_file_and_get_html(self, filename):
        file = '{0}{1}{2}'.format(self.path_views, '/examples/', filename)
        return self.render_page.render_file(file)

    def _open_file_and_take_screenshot(self, filename):
        file = '{0}{1}{2}'.format(self.path_views, '/examples/', filename)
        return self.render_page.screenshot_file(file, self.tmp_img, self.clip)

    def set_clip(self, clip):
        self.clip = clip
        return self

    def render_file_from_zip(self, target):
        with Zip_Folder(self.path_views) as zip_file:
            with Unzip_File(zip_file, '/tmp/test_render_from_zip',
                            True) as web_root:
                return self.render_page.screenshot_file_in_folder(
                    web_root, target, self.tmp_img)

    def open_file_in_browser(self, path, js_code=None):
        with self.render_page.web_server as web_server:
            url = web_server.url(path)
            return self.render_page.get_page_html_via_browser(url, js_code)

    # def open_file_in_browser_and_invoke_js(self, path, js_to_invoke):
    #     with self.render_page.web_server as web_server:
    #         url = web_server.url(path)
    #         return self.render_page.get_page_html_via_browser(url)

    def hello_world__html(self):
        return self._open_file_and_get_html('hello-world.html')

    def hello_world(self):
        return self._open_file_and_take_screenshot('hello-world.html')

    def bootstrap_cdn(self):
        return self._open_file_and_take_screenshot('bootstrap-cdn.html')

    def bootstrap_local(self):
        return self.render_file_from_zip('/examples/bootstrap-local.html')

    def folder_root(self):
        return self.render_page.screenshot_folder(self.path_views,
                                                  self.tmp_img)
class Test_Render_Page(TestCase):
    def setUp(self):
        self.render_page  = Render_Page(headless = False, auto_close = False)
        self.random_value = Misc.random_string_and_numbers(6, "dynamic text ")
        self.html         = "<html><script>document.write('{0}')</script></html>".format(self.random_value)

    def test_render_file(self):
        with Temp_File(self.html, 'html') as temp_file:
            result = self.render_page.render_file(temp_file.file_path)
            assert result('body').html() == self.random_value

    def test_render_folder(self):
        result = self.render_page.render_folder(Files.current_folder())
        Dev.print(result.html())

    def test_render_html(self):
        result = self.render_page.render_html(self.html)
        assert result('body').html() == self.random_value

    def test_screenshot_html(self):
        tmp_img = '/tmp/test_screenshot_html.png'
        img_file = self.render_page.screenshot_html(self.html,tmp_img)
        assert Files.exists(img_file)

    def test_screenshot_file(self):
        with Temp_File(self.html, 'html') as temp_file:
            tmp_img = '/tmp/test_screenshot_html.png'
            clip = {'x': 1, 'y': 1, 'width': 180, 'height': 30}
            img_file = self.render_page.screenshot_file(temp_file.file_path,tmp_img,clip=clip)
            assert Files.exists(img_file)

    def test_screenshot_folder(self):
        web_root = Files.current_folder()
        tmp_img  = '/tmp/test_screenshot_html.png'
        clip = {'x': 1, 'y': 1, 'width':280, 'height': 200}
        self.render_page.screenshot_folder(web_root, tmp_img,clip)

    def test_screenshot_file_in_folder(self):
        web_root = Files.current_folder()
        tmp_img = '/tmp/test_screenshot_html.png'
        html_file = 'aaaa.html'
        self.render_page.screenshot_file_in_folder(web_root, html_file, tmp_img)

    def test_screenshot_url(self):
        #url = 'https://github.com/GoogleChrome/puppeteer'
        #url = 'https://www.google.co.uk'
        #url = 'http://visjs.org/examples/network/other/manipulation.html'
        #url = 'http://visjs.org/examples/graph3d/01_basics.html'
        url = 'https://getbootstrap.com/docs/4.3/examples/dashboard/'
        tmp_img = '/tmp/test_screenshot_html.png'
        self.render_page.screenshot_url(url, tmp_img)