testing.generators
Subpackages
Submodules
Content
- class AttributeGenerator(name: str, value: BaseTemplate | str | None = None)[source]
Bases:
BaseGeneratorClass 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.
- class TagGenerator(name: str, attrs: Iterable[AttributeGenerator | str | tuple[str, BaseTemplate | str | None]] = (), children: Iterable[TagGenerator | str] = (), text: BaseTemplate | str | None = None)[source]
Bases:
BaseGeneratorClass 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.