A-A+

Python headless 浏览器测试工具 Stanislaw

2016年07月28日 Linux 软件 暂无评论 阅读 54 次

Stanislaw一款Python headless 浏览器测试工具。

测试代码:

from unittest import TestCase
from stanislaw.browser import Browser
from stanislaw.forms import UnknownFormInput
from stanislaw.forms import SubmitException
from test.util import response_from_file

BASIC_FORM = "basic_form.html"
URL = "http://example.com/form.html"

def get_browser(url=URL, response_file=BASIC_FORM):
    b = Browser()
    b._set_response(response_from_file(url, response_file))
    return b


class FormFillTest(TestCase):
    def test_fill_all_form_values(self):
        browser = get_browser()
        browser.fill({"#first_name": "Niels",
                      "#last_name": "Bohr",
                      "#email_address": "[email protected]"})

        self.assertEquals("Niels", browser.query("#first_name").val())
        self.assertEquals("Bohr", browser.query("#last_name").val())
        self.assertEquals("[email protected]", browser.query("#email_address").val())

    def test_fill_some_form_values(self):
        browser = get_browser()
        browser.fill({"#first_name": "Niels",
                      "#last_name": "Bohr"})

        self.assertEquals("Niels", browser.query("#first_name").val())
        self.assertEquals("Bohr", browser.query("#last_name").val())
        self.assertEquals("[email protected]", browser.query("#email_address").val())

    def test_fill_nonexistant_element(self):
        browser = get_browser()
        self.assertRaises(UnknownFormInput, browser.fill,
                          {"#does_not_exist": "bad_value"})

        self.assertEquals("Edward", browser.query("#first_name").val())
        self.assertEquals("Teller", browser.query("#last_name").val())
        self.assertEquals("[email protected]", browser.query("#email_address").val())

class FormSerializationTest(TestCase):

    def test_form_value_list_simple(self):
        browser = get_browser()
        value_list = browser.form_manager.form_value_list()
        expected = [("first_name", "Edward"),
                    ("last_name", "Teller"),
                    ("email_address", "[email protected]")]
        self.assertEquals(expected, value_list)

    def test_value_list_after_fill(self):
        browser = get_browser()
        browser.fill({"#first_name": "Niels",
                      "#last_name": "Bohr",
                      "#email_address": "[email protected]"})
        expected = [("first_name", "Niels"),
                    ("last_name", "Bohr"),
                    ("email_address", "[email protected]")]
        value_list = browser.form_manager.form_value_list()
        self.assertEquals(expected, value_list)

    def test_doesnt_serialize_disabled_element(self):
        browser = get_browser(response_file="difficult_form.html")
        value_list = browser.form_manager.form_value_list()
        self.assertNotIn(("phone", "who uses phones?"), value_list)

    def test_serialize_no_defined_value(self):
        browser = get_browser(response_file="difficult_form.html")
        value_list = browser.form_manager.form_value_list()
        self.assertIn(("first_name", ""), value_list)
        self.assertIn(("last_name", ""), value_list)
        self.assertIn(("email_address", ""), value_list)
        self.assertIn(("biography", ""), value_list)

    def test_serialize_checkboxes(self):
        browser = get_browser(response_file="difficult_form.html")
        value_list = browser.form_manager.form_value_list()
        # An unchecked checkbox
        self.assertNotIn("legal_agree", [v[0] for v in value_list])

        # A checked checkbox, with value
        self.assertIn(("contact_me", "yes"), value_list)

        # A checked checkbox, no value
        self.assertIn(("checked_no_value", ""), value_list)

    def test_serialize_textareas(self):
        browser = get_browser(response_file="difficult_form.html")
        value_list = browser.form_manager.form_value_list()

        self.assertIn(("biography", ""), value_list)
        self.assertIn(("filled_textarea", "Textarea content"), value_list)


class MultipleFormTest(TestCase):
    def test_ambiguous_submit(self):
        browser = get_browser(response_file="multiple_forms.html")
        self.assertRaises(SubmitException,
                          browser.form_manager.get_submit_request)

    def test_precise_submit(self):
        browser = get_browser(response_file="multiple_forms.html")
        request = browser.form_manager.get_submit_request("#form2")
self.assertTrue(request)
标签:

给我留言

Copyright © SEARU.ORG 保留所有权利.   Theme  Ality 网站地图 360网站安全检测平台

用户登录

分享到: