ascy_finder|base|cookie 代码测试

# -*- coding: utf-8 -*-

# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.


class AsyncFinderTests(object):

    def test_find_by_css_should_found_an_async_element(self):
        "should find element by css found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_css('.async-element'))

    def test_find_by_xpath_should_found_an_async_element(self):
        "should find by xpath found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_xpath('//h4'))

    def test_find_by_tag_should_found_an_async_element(self):
        "should find by tag found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_tag('h4'))

    def test_find_by_id_should_found_an_async_element(self):
        "should find by id found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_id('async-header'))

    def test_find_by_name_should_found_an_async_element(self):
        "should find by name found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_name('async-input'))

    def test_find_by_value_should_found_an_async_element(self):
        "should find by value found an async element"
        self.browser.find_by_css('.add-async-element').first.click()
        assert 1 == len(self.browser.find_by_value('async-header-value'))


# -*- coding: utf-8 -*-

# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from __future__ import with_statement

from async_finder import AsyncFinderTests
from click_elements import ClickElementsTest
from cookies import CookiesTest
from element_does_not_exist import ElementDoestNotExistTest
from fake_webapp import EXAMPLE_APP
from find_elements import FindElementsTest
from form_elements import FormElementsTest
from iframes import IFrameElementsTest
from element import ElementTest
from is_element_present import IsElementPresentTest
from is_text_present import IsTextPresentTest
from mouse_interaction import MouseInteractionTest
from status_code import StatusCodeTest
from type import SlowlyTypeTest


class BaseBrowserTests(ElementTest, FindElementsTest, FormElementsTest, ClickElementsTest, CookiesTest, SlowlyTypeTest):

    def setUp(self):
        self.fail("You should set up your browser in the setUp() method")

    def test_can_open_page(self):
        "should be able to visit, get title and quit"
        title = self.browser.title
        self.assertEqual('Example Title', title)

    def test_can_back_on_history(self):
        "should be able to back on history"
        self.browser.visit("%s/iframe" % EXAMPLE_APP.rstrip('/'))
        self.browser.back()
        self.assertEqual(EXAMPLE_APP, self.browser.url)

    def test_can_forward_on_history(self):
        "should be able to forward history"
        url = "%s/iframe" % EXAMPLE_APP.rstrip('/')
        self.browser.visit(url)
        self.browser.back()
        self.browser.forward()
        self.assertEqual(url, self.browser.url)

    def test_should_have_html(self):
        "should have access to the html"
        html = self.browser.html
        assert '<title>Example Title</title>' in html
        assert '<h1 id="firstheader">Example Header</h1>' in html

    def test_should_reload_a_page(self):
        "should reload a page"
        title = self.browser.title
        self.browser.reload()
        self.assertEqual('Example Title', title)

    def test_should_have_url(self):
        "should have access to the url"
        self.assertEqual(EXAMPLE_APP, self.browser.url)

    def test_accessing_attributes_of_links(self):
        "should allow link's attributes retrieval"
        foo = self.browser.find_link_by_text('FOO').first
        self.assertEqual('http://localhost:5000/foo', foo['href'])

    def test_accessing_attributes_of_inputs(self):
        "should allow input's attributes retrieval"
        button = self.browser.find_by_css('input[name="send"]').first
        self.assertEqual('send', button['name'])

    def test_accessing_attributes_of_simple_elements(self):
        "should allow simple element's attributes retrieval"
        header = self.browser.find_by_css('h1').first
        self.assertEqual('firstheader', header['id'])

    def test_links_should_have_value_attribute(self):
        foo = self.browser.find_link_by_href('http://localhost:5000/foo').first
        self.assertEqual('FOO', foo.value)

    def test_should_receive_browser_on_parent(self):
        "element should contains the browser on \"parent\" attribute"
        element = self.browser.find_by_id("firstheader").first
        self.assertEqual(self.browser, element.parent)


class WebDriverTests(BaseBrowserTests, IFrameElementsTest, ElementDoestNotExistTest, IsElementPresentTest, AsyncFinderTests, IsTextPresentTest, StatusCodeTest, MouseInteractionTest):

    def test_can_execute_javascript(self):
        "should be able to execute javascript"
        self.browser.execute_script("$('body').empty()")
        self.assertEqual("", self.browser.find_by_tag("body").first.value)

    def test_can_evaluate_script(self):
        "should evaluate script"
        self.assertEqual(8, self.browser.evaluate_script("4+4"))

    def test_can_see_the_text_for_an_element(self):
        "should provide text for an element"
        self.assertEqual(self.browser.find_by_id("simple_text").first.text, "my test text")

    def test_the_text_for_an_element_strips_html_tags(self):
        "should show that the text attribute strips html"
        self.assertEqual(self.browser.find_by_id("text_with_html").first.text, "another bit of text")

    def test_can_verify_if_a_element_is_visible(self):
        "should provide verify if element is visible"
        self.assertTrue(self.browser.find_by_id("visible").first.visible)

    def test_can_verify_if_a_element_is_invisible(self):
        "should provide verify if element is invisible"
        self.assertFalse(self.browser.find_by_id("invisible").first.visible)

    def test_default_wait_time_should_be_2(self):
        "should driver default wait time 2"
        self.assertEqual(2, self.browser.wait_time)

    def test_access_alerts_and_accept_them(self):
        self.browser.visit(EXAMPLE_APP + 'alert')
        self.browser.find_by_tag('h1').first.click()
        alert = self.browser.get_alert()
        self.assertEqual('This is an alert example.', alert.text)
        alert.accept()

    def test_access_prompts_and_be_able_to_fill_then(self):
        self.browser.visit(EXAMPLE_APP + 'alert')
        self.browser.find_by_tag('h2').first.click()

        alert = self.browser.get_alert()
        self.assertEqual('What is your name?', alert.text)
        alert.fill_with('Splinter')
        alert.accept()

        response = self.browser.get_alert()
        self.assertEqual('Splinter', response.text)
        response.accept()

    def test_access_confirm_and_accept_and_dismiss_them(self):
        self.browser.visit(EXAMPLE_APP + 'alert')

        self.browser.find_by_tag('h3').first.click()
        alert = self.browser.get_alert()

        self.assertEqual('Should I continue?', alert.text)
        alert.accept()
        alert = self.browser.get_alert()
        self.assertEqual('You say I should', alert.text)
        alert.accept()

        self.browser.find_by_tag('h3').first.click()
        alert = self.browser.get_alert()
        self.assertEqual('Should I continue?', alert.text)
        alert.dismiss()
        alert = self.browser.get_alert()
        self.assertEqual('You say I should not', alert.text)
        alert.accept()

    def test_access_confirm_and_accept_and_dismiss_them_using_with(self):
        self.browser.visit(EXAMPLE_APP + 'alert')

        self.browser.find_by_tag('h3').first.click()
        with self.browser.get_alert() as alert:
            self.assertEqual('Should I continue?', alert.text)
            alert.accept()

        with self.browser.get_alert() as alert:
            self.assertEqual('You say I should', alert.text)
            alert.accept()

        self.browser.find_by_tag('h3').first.click()
        with self.browser.get_alert() as alert:
            self.assertEqual('Should I continue?', alert.text)
            alert.dismiss()
        with self.browser.get_alert() as alert:
            self.assertEqual('You say I should not', alert.text)
            alert.accept()

    def test_access_alerts_using_with(self):
        "should access alerts using 'with' statement"
        self.browser.visit(EXAMPLE_APP + 'alert')
        self.browser.find_by_tag('h1').first.click()
        with self.browser.get_alert() as alert:
            self.assertEqual('This is an alert example.', alert.text)
            alert.accept()


# -*- coding: utf-8 -*-

# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.


class ClickElementsTest(object):

    def test_click_links(self):
        "should allow to click links"
        self.browser.find_link_by_text('FOO').first.click()
        assert 'BAR!' in self.browser.html

    def test_click_element_by_css_selector(self):
        "should allow to click at elements by css selector"
        self.browser.find_by_css('a[href="http://localhost:5000/foo"]').first.click()
        assert 'BAR!' in self.browser.html

    def test_click_input_by_css_selector(self):
        "should allow to click at inputs by css selector"
        self.browser.find_by_css('input[name="send"]').first.click()
        assert 'My name is: Master Splinter' in self.browser.html

    def test_click_link_by_href(self):
        "should allow to click link by href"
        self.browser.click_link_by_href('http://localhost:5000/foo')
        assert "BAR!" in self.browser.html

    def test_click_link_by_partial_href(self):
        "should allow to click link by partial href"
        self.browser.click_link_by_partial_href('5000/foo')
        assert "BAR!" in self.browser.html

    def test_click_link_by_text(self):
        "should allow to click link by text"
        self.browser.click_link_by_text('FOO')
        assert "BAR!" in self.browser.html

    def test_click_link_by_partial_text(self):
        "should allow to click link by partial text"
        self.browser.click_link_by_partial_text("wordier")
        assert "BAR!" in self.browser.html

# -*- coding: utf-8 -*-

# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.


class CookiesTest(object):

    def test_create_and_access_a_cookie(self):
        "should be able to create and access a cookie"
        self.browser.cookies.add({'sha': 'zam'})
        self.assertEqual(self.browser.cookies['sha'], 'zam')

    def test_create_some_cookies_and_delete_them_all(self):
        "should be able to delete all cookies"
        self.browser.cookies.add({'whatever': 'and ever'})
        self.browser.cookies.add({'anothercookie': 'im bored'})
        self.browser.cookies.delete()
        self.assertEqual(self.browser.cookies, {})

    def test_create_and_delete_a_cookie(self):
        "should be able to create and destroy a cookie"
        self.browser.cookies.delete()
        self.browser.cookies.add({'cookie': 'with milk'})
        self.browser.cookies.delete('cookie')
        self.assertEqual(self.browser.cookies, {})

    def test_create_and_delete_many_cookies(self):
        "should be able to create and destroy many cookies"
        self.browser.cookies.delete()
        self.browser.cookies.add({'cookie': 'cooked'})
        self.browser.cookies.add({'anothercookie': 'uncooked'})
        self.browser.cookies.add({'notacookie': 'halfcooked'})
        self.browser.cookies.delete('cookie', 'notacookie')
        self.assertEqual('uncooked', self.browser.cookies['anothercookie'])

    def test_try_to_destroy_an_absent_cookie_and_nothing_happens(self):
        self.browser.cookies.delete()
        self.browser.cookies.add({'foo': 'bar'})
        self.browser.cookies.delete('mwahahahaha')
        self.assertEqual(self.browser.cookies, {'foo': 'bar'})
原文地址:https://www.cnblogs.com/hzhida/p/2638733.html