testing.generators.generators

Module with generators for producing HTML content. Generators can be used to create customizable HTML content, that can be used to test selectors and workflows.

Classes

  • AttributeGenerator - class for generating HTML attribute strings.

  • TagGenerator - class for generating HTML tag strings.

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.