Exemple #1
0
 def test_state_deque_max_length(self):
     browser = RoboBrowser(history=5)
     for _ in range(5):
         browser.open('http://pyrobot.com/page1/')
     assert_equal(len(browser._states), 5)
     browser.open('http://pyrobot.com/page2/')
     assert_equal(len(browser._states), 5)
Exemple #2
0
 def test_user_agent(self):
     browser = RoboBrowser(user_agent='freddie')
     browser.open('http://pyrobot.com/links/')
     assert_true('User-Agent' in browser.session.headers)
     assert_equal(
         browser.session.headers['User-Agent'], 'freddie'
     )
Exemple #3
0
 def test_headers(self):
     headers = {
         'X-Song': 'Innuendo',
         'X-Writer': 'Freddie',
     }
     browser = RoboBrowser(headers=headers)
     browser.open('http://pyrobot.com/links/')
     assert_equal(browser.session.headers, headers)
Exemple #4
0
 def test_headers(self):
     headers = {
         'X-Song': 'Innuendo',
         'X-Writer': 'Freddie',
     }
     browser = RoboBrowser(headers=headers)
     browser.open('http://pyrobot.com/links/')
     assert_equal(browser.session.headers, headers)
Exemple #5
0
 def test_state_deque_max_length(self):
     browser = RoboBrowser(history=5)
     for _ in range(5):
         browser.open('http://pyrobot.com/page1/')
     assert_equal(len(browser._states), 5)
     browser.open('http://pyrobot.com/page2/')
     assert_equal(len(browser._states), 5)
Exemple #6
0
class TestForms(unittest.TestCase):
    @mock_forms
    def setUp(self):
        self.browser = RoboBrowser()
        self.browser.open('http://pyrobot.com/get/')

    @mock_forms
    def test_get_forms(self):
        forms = self.browser.get_forms()
        assert_equal(len(forms), 2)

    @mock_forms
    def test_get_form_by_id(self):
        form = self.browser.get_form('bass')
        assert_equal(form._parsed.get('id'), 'bass')

    @mock_forms
    def test_submit_form(self):
        form = self.browser.get_form()
        self.browser.submit_form(form)
        assert_equal(self.browser.url, 'http://pyrobot.com/post/')
Exemple #7
0
class TestForms(unittest.TestCase):

    @mock_forms
    def setUp(self):
        self.browser = RoboBrowser()
        self.browser.open('http://pyrobot.com/get/')

    @mock_forms
    def test_get_forms(self):
        forms = self.browser.get_forms()
        assert_equal(len(forms), 2)

    @mock_forms
    def test_get_form_by_id(self):
        form = self.browser.get_form('bass')
        assert_equal(form._parsed.get('id'), 'bass')

    @mock_forms
    def test_submit_form(self):
        form = self.browser.get_form()
        self.browser.submit_form(form)
        assert_equal(self.browser.url, 'http://pyrobot.com/post/')
Exemple #8
0
 def setUp(self):
     self.browser = RoboBrowser(history=True)
Exemple #9
0
class TestLinks(unittest.TestCase):

    @mock_links
    def setUp(self):
        self.browser = RoboBrowser()
        self.browser.open('http://pyrobot.com/links/')

    @mock_links
    def test_get_link(self):
        link = self.browser.get_link()
        assert_equal(link.get('href'), '/link1/')

    @mock_links
    def test_get_links(self):
        links = self.browser.get_links()
        assert_equal(len(links), 2)

    @mock_links
    def test_get_link_by_text(self):
        link = self.browser.get_link('opera')
        assert_equal(link.get('href'), '/link2/')

    @mock_links
    def test_follow_link_tag(self):
        link = self.browser.get_link(text=re.compile('sheer'))
        self.browser.follow_link(link)
        assert_equal(self.browser.url, 'http://pyrobot.com/link1/')

    @mock_links
    def test_follow_link_text(self):
        self.browser.follow_link('heart attack')
        assert_equal(self.browser.url, 'http://pyrobot.com/link1/')

    @mock_links
    def test_follow_link_regex(self):
        self.browser.follow_link(re.compile(r'opera'))
        assert_equal(self.browser.url, 'http://pyrobot.com/link2/')

    @mock_links
    def test_follow_link_bs_args(self):
        self.browser.follow_link(class_=re.compile(r'song'))
        assert_equal(self.browser.url, 'http://pyrobot.com/link2/')
Exemple #10
0
 def setUp(self):
     self.browser = RoboBrowser(history=True)
     self.browser.open('http://pyrobot.com/page1/')
     self.browser.open('http://pyrobot.com/page2/')
     self.browser.open('http://pyrobot.com/page3/')
Exemple #11
0
 def test_state_deque_no_history(self):
     browser = RoboBrowser(history=False)
     for _ in range(5):
         browser.open('http://pyrobot.com/page1/')
         assert_equal(len(browser._states), 1)
         assert_equal(browser._cursor, 0)
Exemple #12
0
 def test_state_deque_no_history(self):
     browser = RoboBrowser(history=False)
     for _ in range(5):
         browser.open('http://pyrobot.com/page1/')
         assert_equal(len(browser._states), 1)
         assert_equal(browser._cursor, 0)
Exemple #13
0
class TestHistory(unittest.TestCase):

    @mock_urls
    def setUp(self):
        self.browser = RoboBrowser(history=True)
        self.browser.open('http://pyrobot.com/page1/')
        self.browser.open('http://pyrobot.com/page2/')
        self.browser.open('http://pyrobot.com/page3/')

    def test_back(self):
        self.browser.back()
        assert_equal(
            self.browser.url,
            'http://pyrobot.com/page2/'
        )

    def test_back_n(self):
        self.browser.back(n=2)
        assert_equal(
            self.browser.url,
            'http://pyrobot.com/page1/'
        )

    def test_forward(self):
        self.browser.back()
        self.browser.forward()
        assert_equal(
            self.browser.url,
            'http://pyrobot.com/page3/'
        )

    def test_forward_n(self):
        self.browser.back(n=2)
        self.browser.forward(n=2)
        assert_equal(
            self.browser.url,
            'http://pyrobot.com/page3/'
        )

    @mock_urls
    def test_open_clears_forward(self):
        self.browser.back(n=2)
        self.browser.open('http://pyrobot.com/page4/')
        assert_equal(
            self.browser._cursor,
            len(self.browser._states) - 1
        )
        assert_raises(
            RoboError,
            self.browser.forward
        )

    def test_back_error(self):
        assert_raises(
            RoboError,
            self.browser.back,
            5
        )
Exemple #14
0
 def setUp(self):
     self.browser = RoboBrowser(history=True)
Exemple #15
0
class TestHistory(unittest.TestCase):
    @mock_urls
    def setUp(self):
        self.browser = RoboBrowser(history=True)
        self.browser.open('http://pyrobot.com/page1/')
        self.browser.open('http://pyrobot.com/page2/')
        self.browser.open('http://pyrobot.com/page3/')

    def test_back(self):
        self.browser.back()
        assert_equal(self.browser.url, 'http://pyrobot.com/page2/')

    def test_back_n(self):
        self.browser.back(n=2)
        assert_equal(self.browser.url, 'http://pyrobot.com/page1/')

    def test_forward(self):
        self.browser.back()
        self.browser.forward()
        assert_equal(self.browser.url, 'http://pyrobot.com/page3/')

    def test_forward_n(self):
        self.browser.back(n=2)
        self.browser.forward(n=2)
        assert_equal(self.browser.url, 'http://pyrobot.com/page3/')

    @mock_urls
    def test_open_clears_forward(self):
        self.browser.back(n=2)
        self.browser.open('http://pyrobot.com/page4/')
        assert_equal(self.browser._cursor, len(self.browser._states) - 1)
        assert_raises(RoboError, self.browser.forward)

    def test_back_error(self):
        assert_raises(RoboError, self.browser.back, 5)
Exemple #16
0
 def setUp(self):
     self.browser = RoboBrowser()
     self.browser.open('http://pyrobot.com/get/')
Exemple #17
0
class TestHistoryInternals(unittest.TestCase):

    def setUp(self):
        self.browser = RoboBrowser(history=True)

    @mock_urls
    def test_open_appends_to_history(self):
        assert_equal(len(self.browser._states), 0)
        assert_equal(self.browser._cursor, -1)
        self.browser.open('http://pyrobot.com/page1/')
        assert_equal(len(self.browser._states), 1)
        assert_equal(self.browser._cursor, 0)

    @mock_forms
    def test_submit_appends_to_history(self):
        self.browser.open('http://pyrobot.com/get/')
        form = self.browser.get_form()
        self.browser.submit_form(form)

        assert_equal(len(self.browser._states), 2)
        assert_equal(self.browser._cursor, 1)

    @mock_urls
    def test_open_clears_history_after_back(self):
        self.browser.open('http://pyrobot.com/page1/')
        self.browser.open('http://pyrobot.com/page2/')
        self.browser.back()
        self.browser.open('http://pyrobot.com/page3/')
        assert_equal(len(self.browser._states), 2)
        assert_equal(self.browser._cursor, 1)

    @mock_urls
    def test_state_deque_max_length(self):
        browser = RoboBrowser(history=5)
        for _ in range(5):
            browser.open('http://pyrobot.com/page1/')
        assert_equal(len(browser._states), 5)
        browser.open('http://pyrobot.com/page2/')
        assert_equal(len(browser._states), 5)

    @mock_urls
    def test_state_deque_no_history(self):
        browser = RoboBrowser(history=False)
        for _ in range(5):
            browser.open('http://pyrobot.com/page1/')
            assert_equal(len(browser._states), 1)
            assert_equal(browser._cursor, 0)
Exemple #18
0
class TestHistoryInternals(unittest.TestCase):
    def setUp(self):
        self.browser = RoboBrowser(history=True)

    @mock_urls
    def test_open_appends_to_history(self):
        assert_equal(len(self.browser._states), 0)
        assert_equal(self.browser._cursor, -1)
        self.browser.open('http://pyrobot.com/page1/')
        assert_equal(len(self.browser._states), 1)
        assert_equal(self.browser._cursor, 0)

    @mock_forms
    def test_submit_appends_to_history(self):
        self.browser.open('http://pyrobot.com/get/')
        form = self.browser.get_form()
        self.browser.submit_form(form)

        assert_equal(len(self.browser._states), 2)
        assert_equal(self.browser._cursor, 1)

    @mock_urls
    def test_open_clears_history_after_back(self):
        self.browser.open('http://pyrobot.com/page1/')
        self.browser.open('http://pyrobot.com/page2/')
        self.browser.back()
        self.browser.open('http://pyrobot.com/page3/')
        assert_equal(len(self.browser._states), 2)
        assert_equal(self.browser._cursor, 1)

    @mock_urls
    def test_state_deque_max_length(self):
        browser = RoboBrowser(history=5)
        for _ in range(5):
            browser.open('http://pyrobot.com/page1/')
        assert_equal(len(browser._states), 5)
        browser.open('http://pyrobot.com/page2/')
        assert_equal(len(browser._states), 5)

    @mock_urls
    def test_state_deque_no_history(self):
        browser = RoboBrowser(history=False)
        for _ in range(5):
            browser.open('http://pyrobot.com/page1/')
            assert_equal(len(browser._states), 1)
            assert_equal(browser._cursor, 0)
Exemple #19
0
 def setUp(self):
     self.browser = RoboBrowser()
     self.browser.open('http://pyrobot.com/links/')
Exemple #20
0
class TestLinks(unittest.TestCase):
    @mock_links
    def setUp(self):
        self.browser = RoboBrowser()
        self.browser.open('http://pyrobot.com/links/')

    @mock_links
    def test_get_link(self):
        link = self.browser.get_link()
        assert_equal(link.get('href'), '/link1/')

    @mock_links
    def test_get_links(self):
        links = self.browser.get_links()
        assert_equal(len(links), 2)

    @mock_links
    def test_get_link_by_text(self):
        link = self.browser.get_link('opera')
        assert_equal(link.get('href'), '/link2/')

    @mock_links
    def test_follow_link_tag(self):
        link = self.browser.get_link(text=re.compile('sheer'))
        self.browser.follow_link(link)
        assert_equal(self.browser.url, 'http://pyrobot.com/link1/')

    @mock_links
    def test_follow_link_text(self):
        self.browser.follow_link('heart attack')
        assert_equal(self.browser.url, 'http://pyrobot.com/link1/')

    @mock_links
    def test_follow_link_regex(self):
        self.browser.follow_link(re.compile(r'opera'))
        assert_equal(self.browser.url, 'http://pyrobot.com/link2/')

    @mock_links
    def test_follow_link_bs_args(self):
        self.browser.follow_link(class_=re.compile(r'song'))
        assert_equal(self.browser.url, 'http://pyrobot.com/link2/')
Exemple #21
0
 def test_user_agent(self):
     browser = RoboBrowser(user_agent='freddie')
     browser.open('http://pyrobot.com/links/')
     assert_true('User-Agent' in browser.session.headers)
     assert_equal(browser.session.headers['User-Agent'], 'freddie')
Exemple #22
0
 def setUp(self):
     self.browser = RoboBrowser(history=True)
     self.browser.open('http://pyrobot.com/page1/')
     self.browser.open('http://pyrobot.com/page2/')
     self.browser.open('http://pyrobot.com/page3/')