コード例 #1
0
    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        with open(path, 'r') as f:
            self.html = f.read()
コード例 #2
0
    def setUp(self):
        super(BrowserClientTest, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
コード例 #3
0
    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        with open(path, 'r') as f:
            self.html = f.read()
コード例 #4
0
    def setUp(self):
        super(TestBrowserRedirection, self).setUp()
        self.browser = Browser()

        from wsgiref.simple_server import make_server
        import threading
        import random

        self.port = random.randint(8000, 9999)

        class WSGIRunner(threading.Thread):

            def __init__(self, app, port):
                super(WSGIRunner, self).__init__()
                self.server = make_server('', port, app)

            def run(self):
                self.server.serve_forever()

            def stop(self):
                self.server.shutdown()
                self.join()

        def app(environ, start_response):
            """
            A sample WSGI app that forcibly redirects all requests to /
            """
            if environ['PATH_INFO'] == '/':
                response_headers = [('Content-type', 'text/plain')]
                start_response('200 OK', response_headers)
                return [
                    bytes('Hello world!', 'utf-8') if PY3 else 'Hello world!'
                ]

            response_headers = [
                ('Location', '/'),
                ('Content-type', 'text/plain')
            ]
            start_response('302 Found', response_headers)
            return [bytes('', 'utf-8') if PY3 else '']

        self.runner = WSGIRunner(app, self.port)
        self.runner.start()
コード例 #5
0
ファイル: test_browser.py プロジェクト: graffic/python-zombie
class BrowserClientTest(TestCase):
    """
    Sets up a zombie.Browser() object to test with.
    """

    def setUp(self):
        super(BrowserClientTest, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        with open(path, 'r') as f:
            self.browser.load(f.read())

    def tearDown(self):
        super(BrowserClientTest, self).tearDown()
        fudge.clear_expectations()
コード例 #6
0
class TestBrowserRedirection(BrowserClientTest):

    def setUp(self):
        super(TestBrowserRedirection, self).setUp()
        self.browser = Browser()

        from wsgiref.simple_server import make_server
        import threading
        import random

        self.port = random.randint(8000, 9999)

        class WSGIRunner(threading.Thread):

            def __init__(self, app, port):
                super(WSGIRunner, self).__init__()
                self.server = make_server('', port, app)

            def run(self):
                self.server.serve_forever()

            def stop(self):
                self.server.shutdown()
                self.join()

        def app(environ, start_response):
            """
            A sample WSGI app that forcibly redirects all requests to /
            """
            if environ['PATH_INFO'] == '/':
                response_headers = [('Content-type', 'text/plain')]
                start_response('200 OK', response_headers)
                return [
                    bytes('Hello world!', 'utf-8') if PY3 else 'Hello world!'
                ]

            response_headers = [
                ('Location', '/'),
                ('Content-type', 'text/plain')
            ]
            start_response('302 Found', response_headers)
            return [bytes('', 'utf-8') if PY3 else '']

        self.runner = WSGIRunner(app, self.port)
        self.runner.start()

    def tearDown(self):
        super(TestBrowserRedirection, self).tearDown()
        self.runner.stop()

    def test_redirected(self):
        self.browser.visit('http://localhost:%d/' % self.port)
        assert 'Hello world!' in self.browser.html()
        assert self.browser.redirected is False

        self.browser.visit('http://localhost:%d/redirect' % self.port)
        assert 'Hello world!' in self.browser.html()
        assert self.browser.redirected is True
コード例 #7
0
from zombie import Browser
b = Browser()
b.visit('https://github.com/ryanpetrello/python-zombie')
コード例 #8
0
class TestBrowser(BrowserClientTest):

    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        with open(path, 'r') as f:
            self.html = f.read()

    #
    # Document Content
    #
    def test_html(self):
        self.browser.visit(self.path)
        html = self.browser.html()
        assert '<title>Example</title>' in html
        assert '<p>This is an HTML document</p>' in html

    def test_html_with_selector(self):
        self.browser.visit(self.path)
        html = self.browser.html('#content')
        assert '<title>Example</title>' not in html
        assert '<p>This is an HTML document</p>' in html

    def test_html_with_context(self):
        self.browser.visit(self.path)
        html = self.browser.html('#content', self.browser.query('body'))
        assert '<title>Example</title>' not in html
        assert '<p>This is an HTML document</p>' in html

    def test_text(self):
        self.browser.visit(self.path)
        text = self.browser.text('title')
        assert text == 'Example'

    def test_text_no_match(self):
        self.browser.visit(self.path)
        text = self.browser.text('blink')
        assert not text

    def test_text_with_context(self):
        self.browser.visit(self.path)
        text = self.browser.text('title', self.browser.query('head'))
        assert text == 'Example'

    def test_text_with_context_missing(self):
        self.browser.visit(self.path)
        text = self.browser.text('title', self.browser.query('body'))
        assert not text

    def test_css(self):
        self.browser.visit(self.path)
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            matches = self.browser.css(tag)
            assert len(matches)

    def test_css_no_results(self):
        self.browser.visit(self.path)
        matches = self.browser.css('blink')
        assert len(matches) == 0

    def test_query(self):
        self.browser.visit(self.path)
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            match = self.browser.query(tag)
            assert isinstance(match, DOMNode)

    def test_query_no_results(self):
        self.browser.visit(self.path)
        match = self.browser.query('blink')
        assert match is None

    def test_by_id(self):
        matches = self.browser.visit(self.path).css('#submit')
        assert len(matches) == 1
        assert matches[0].tagName.lower() == 'button'

    def test_by_class_name(self):
        matches = self.browser.visit(self.path).css('.textfield')
        assert len(matches) == 1
        assert matches[0].tagName.lower() == 'input'

    #
    # Navigation
    #
    def test_location_get(self):
        for p in ('scheme', 'path'):
            getattr(urlparse(self.browser.visit(self.path).location), p) == \
                getattr(urlparse(self.path), p)

    def test_location_set(self):
        self.browser.location = self.path
        for p in ('scheme', 'path'):
            getattr(urlparse(self.browser.visit(self.path).location), p) == \
                getattr(urlparse(self.path), p)

    def test_click_link(self):
        self.browser.visit(self.path)
        self.browser.clickLink('#about-zombie')
        assert self.browser.location == 'http://zombie.labnotes.org/'

    def test_back(self):
        self.browser.visit('http://zombie.labnotes.org/')
        self.browser.visit('http://google.com/')
        self.browser.back()
        assert self.browser.location == 'http://zombie.labnotes.org/'

    #
    # Forms
    #
    def test_fill(self):
        self.browser.visit(self.path).fill('q', 'Zombie.js')
        assert self.browser.css('input')[0].value == 'Zombie.js'

    def test_press_button(self):
        self.browser.visit(self.path)
        self.browser.pressButton('Search')
        assert urlparse(self.browser.location).netloc == 'www.google.com'
コード例 #9
0
class TestBrowser(BrowserClientTest):

    def setUp(self):
        super(TestBrowser, self).setUp()
        self.browser = Browser()

        # Build the path to the example.html file
        path = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(path, 'helpers', 'example.html')
        self.path = 'file://%s' % path
        with open(path, 'r') as f:
            self.html = f.read()

    #
    # Document Content
    #
    def test_body(self):
        self.browser.visit(self.path)
        body = self.browser.body
        assert isinstance(body, DOMNode)

        html = body.innerHTML
        assert '<title>Example</title>' not in html
        assert '<p>This is an HTML document</p>' in html

    def test_html(self):
        self.browser.visit(self.path)
        html = self.browser.html()
        assert '<title>Example</title>' in html
        assert '<p>This is an HTML document</p>' in html

    def test_html_with_selector(self):
        self.browser.visit(self.path)
        html = self.browser.html('#content')
        assert '<title>Example</title>' not in html
        assert '<p>This is an HTML document</p>' in html

    def test_html_with_context(self):
        self.browser.visit(self.path)
        html = self.browser.html('#content', self.browser.query('body'))
        assert '<title>Example</title>' not in html
        assert '<p>This is an HTML document</p>' in html

    def test_text(self):
        self.browser.visit(self.path)
        text = self.browser.text('title')
        assert text == 'Example'

    def test_text_no_match(self):
        self.browser.visit(self.path)
        text = self.browser.text('blink')
        assert not text

    def test_text_with_context(self):
        self.browser.visit(self.path)
        text = self.browser.text('title', self.browser.query('head'))
        assert text == 'Example'

    def test_text_with_context_missing(self):
        self.browser.visit(self.path)
        text = self.browser.text('title', self.browser.query('body'))
        assert not text

    def test_css(self):
        self.browser.visit(self.path)
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            matches = self.browser.css(tag)
            assert len(matches)

    def test_css_no_results(self):
        self.browser.visit(self.path)
        matches = self.browser.css('blink')
        assert len(matches) == 0

    def test_query(self):
        self.browser.visit(self.path)
        for tag in ['h1', 'p', 'form', 'input', 'button']:
            match = self.browser.query(tag)
            assert isinstance(match, DOMNode)

    def test_query_no_results(self):
        self.browser.visit(self.path)
        match = self.browser.query('blink')
        assert match is None

    def test_by_id(self):
        matches = self.browser.visit(self.path).css('#submit')
        assert len(matches) == 1
        assert matches[0].tagName.lower() == 'button'

    def test_by_class_name(self):
        matches = self.browser.visit(self.path).css('.textfield')
        assert len(matches) == 1
        assert matches[0].tagName.lower() == 'input'

    #
    # Navigation
    #
    def test_location_get(self):
        for p in ('scheme', 'path'):
            getattr(urlparse(self.browser.visit(self.path).location), p) == \
                getattr(urlparse(self.path), p)

    def test_location_set(self):
        self.browser.location = self.path
        for p in ('scheme', 'path'):
            getattr(urlparse(self.browser.visit(self.path).location), p) == \
                getattr(urlparse(self.path), p)

    def test_click_link(self):
        self.browser.visit(self.path)
        self.browser.clickLink('#about-zombie')
        assert self.browser.location == 'http://zombie.labnotes.org/'

    def test_link_by_selector(self):
        self.browser.visit(self.path)
        match = self.browser.link('#about-zombie')
        assert isinstance(match, DOMNode)

        assert match.innerHTML == 'Learn About Zombie'

    def test_link_by_inner_text(self):
        self.browser.visit(self.path)
        match = self.browser.link('Learn About Zombie')
        assert isinstance(match, DOMNode)

        assert match.id == 'about-zombie'

    def test_back(self):
        self.browser.visit('http://zombie.labnotes.org/')
        self.browser.visit('http://google.com/')
        self.browser.back()
        assert self.browser.location == 'http://zombie.labnotes.org/'

    def test_reload(self):
        self.browser.visit(self.path)
        self.browser.fill('q', 'Zombie.js')
        assert self.browser.css('input')[0].value == 'Zombie.js'

        self.browser.reload()
        assert self.browser.css('input')[0].value == ''

    def test_status_code_200(self):
        self.browser.visit(self.path)
        assert self.browser.statusCode == 200

    def test_success(self):
        self.browser.visit(self.path)
        assert self.browser.success is True

    #
    # Forms
    #
    def test_fill(self):
        self.browser.visit(self.path).fill('q', 'Zombie.js')
        assert self.browser.css('input')[0].value == 'Zombie.js'

    def test_press_button(self):
        self.browser.visit(self.path)
        self.browser.pressButton('Search')
        assert urlparse(self.browser.location).path.endswith('/submit.html')

    #
    # Debugging
    #
    def test_dump(self):
        self.browser.visit(self.path)
        self.browser.dump()

    def test_resources(self):
        self.browser.visit('http://google.com')

        resources = self.browser.resources
        assert len(resources)

        for r in resources:
            assert r['url']
            assert r['time']
            assert r['size']
            assert r['request']
            assert r['response']
コード例 #10
0
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 19:40:08 2019

@author: zee
"""

import requests as rq
from bs4 import BeautifulSoup

from zombie import Browser
b = Browser()
b.visit('http://pypi.python.org/').fill('term', 'Zombie').pressButton('submit')
assert "A Python driver for Zombie.js" in b.body.text

#rq.get("")