testing

Subpackages

Content

Package with functionalities for generating customizable html, that can be used to test selectors and workflows.

Classes

  • AttributeGenerator - Generator for creating html attributes.

  • TagGenerator - Generator for creating html tags.

  • ChoiceTemplate - Template for randomly selecting from a list of choices.

  • RandomTemplate - Template for generating a random string of ASCII letters and digits.

  • BaseTemplate - Base for user-defined templates.

class AttributeGenerator(name: str, value: BaseTemplate | str | None = None)[source]

Bases: BaseGenerator

Class for generating HTML attribute strings.

AttributeGenerator allows to generate empty HTML attribute with specific name:

Example

>>> gen = AttributeGenerator("class")
... gen.generate()
'class=""'

Attribute can also be generated with specified constant value:

Example

>>> gen = AttributeGenerator("class", value="container")
... gen.generate()
'class="container"'

Value can be passed as BaseTemplate instance as well:

Example

>>> from soupsavvy.testing.generators import RandomTemplate
... template = RandomTemplate(length=4, seed=42)
... gen = AttributeGenerator("id", value=template)
... gen.generate()
'id="Nbrn"'

For more information on available Templates, how to use them and customize for your needs, see the documentation.

See also

  • soupsavvy.testing.generators.templates package

  • soupsavvy.testing.TagGenerator class

__init__(name: str, value: BaseTemplate | str | None = None) None[source]

Initializes the AttributeGenerator.

Parameters

namestr

The name of the attribute.

valueTemplateType, optional

The value of the attribute. Defaults to None.

generate() str[source]

Generates the HTML attribute string.

Returns

str

The generated HTML attribute string.

class TagGenerator(name: str, attrs: Iterable[AttributeGenerator | str | tuple[str, BaseTemplate | str | None]] = (), children: Iterable[TagGenerator | str] = (), text: BaseTemplate | str | None = None)[source]

Bases: BaseGenerator

Class for generating HTML tag strings.

TagGenerator allows to generate empty HTML tag with specific name:

Example

>>> gen = TagGenerator("div")
... gen.generate()
'<div></div>'

Tag can also be generated with specified attributes:

Example

>>> gen = TagGenerator("div", attrs=[("class", "container")])
... gen.generate()
'<div class="container"></div>'

Attributes can be passed as an iterable of of mixed types: - AttributeGenerator - instance of AttributeGenerator class - str - attribute name, value would be a default Template (empty string) - tuple[str, TemplateType] - attribute name and literal value or Template

Example

>>> gen = TagGenerator(
...     name="a",
...     attrs=[
...         ("id", "link"),
...         AttributeGenerator("href", "/endpoint"),
...         "class",
...     ],
... )
... gen.generate()
'<div id="link" href="/endpoint", class=""></div>'

Similarly, children can be passed as an iterable of mixed types: - TagGenerator - instance of TagGenerator class - str - tag name, children tags would be empty

Example

>>> gen = TagGenerator(
...     name="div",
...     children=[
...         "a",
...         TagGenerator("span", attrs=[("class", "container")],
...     ],
... )
... gen.generate()
'<div><a></a><span class="container"></span></div>'

Text of the tag can be passed as a string or a Template:

Example

>>> gen = TagGenerator("p", text="Hello, World!")
... gen.generate()
'<p>Hello, World!</p>'

Example

>>> from soupsavvy.testing.generators import RandomTemplate
... template = RandomTemplate(length=4, seed=42)
... gen = TagGenerator("p", text=template)
... gen.generate()
'<p>Nbrn</p>'

For more information on available Templates, how to use them and customize for your needs, see the documentation.

Void tags like <img>, <br>, <hr> etc. can be generated as well and are automatically closed:

Example

>>> gen = TagGenerator("img", attrs=[("src", "/path/to/image.jpg")])
... gen.generate()
'<img src="/path/to/image.jpg"/>'

No children are allowed for void tags, and an error will be raised.

See also

  • soupsavvy.testing.generators.templates package

  • soupsavvy.testing.AttributeGenerator class

__init__(name: str, attrs: Iterable[AttributeGenerator | str | tuple[str, BaseTemplate | str | None]] = (), children: Iterable[TagGenerator | str] = (), text: BaseTemplate | str | None = None) None[source]

Initialize the TagGenerator.

Parameters

namestr

The name of the HTML tag.

attrsAttributeList, optional

The attributes of the tag. Defaults to empty tuple.

childrenChildList, optional

The children of the tag. Defaults to empty tuple.

textTemplateType, optional

The text content of the tag. Defaults to None, which generates empty string.

generate() str[source]

Generates the HTML tag string.

Returns

str

The generated HTML tag string.

class ChoiceTemplate(choices: Iterable, seed: int | None = None)[source]

Bases: BaseTemplate

Template for randomly selecting from a list of choices.

Example

>>> template = ChoiceTemplate(["apple", "banana", "cherry"], seed=42)
... template.generate()
'cherry'
__init__(choices: Iterable, seed: int | None = None) None[source]

Initializes ChoiceTemplate instance.

Parameters

choicesIterable

Iterable of choices to select from.

seedint, optional

Random seed for reproducibility. Defaults to None, no seed is used.

generate() str[source]

Generates a random choice from the list of choices.

Returns

str

The randomly selected choice.

class RandomTemplate(length: int = 4, seed: int | None = None)[source]

Bases: BaseTemplate

Template for generating a random string of ASCII letters and digits.

Example

>>> template = RandomTemplate(length=4, seed=42)
... template.generate()
'Nbrn'
__init__(length: int = 4, seed: int | None = None) None[source]

Initializes RandomTemplate instance.

Parameters

lengthint, optional

Length of the random string. Defaults to 4.

seedint, optional

Random seed for reproducibility. Defaults to None.

generate() str[source]

Generates a random string from ASCII letters and digits.

Returns

str

The generated random string.

class BaseTemplate[source]

Bases: BaseGenerator

Interface for templates used in generators.

BaseTemplate is also the subclass of BaseGenerator, all Templates implement BaseGenerator interface and are easy to interchange with other generators.

abstract generate() str[source]

Generates a string based on the template.

Returns

str

The generated string.