Skip to main content

zelenium

New Selenium framework for Python with base pages and elements

Installation

pip install zelenium

Usage

Zelenium offers several features that could be combined with classical selenium usage:

  • Driver singleton configuration;
  • BasePage with BaseElements;
  • Suffix and formatting mechanisms for BaseElements;
  • It also should be useful for Appium testing.

zelenium configuration

To setup configuration for zelenium you could just use Config:

from selenium import webdriver
from zelenium import Config

config = Config.get_instance()
config.driver = webdriver.Chrome()

Because Config is singleton - you could not use it with two different webdrivers at one moment. But if you need it, you could use private class:

from zelenium import Config
from zelenium.base.config import _Config

config1 = Config.get_instance()
config2 = _Config()

assert not (config1 is config2)  # No assertion

BasePage and BaseElement

What offers you BasePage:

  • No need to pass webdriver instance - it would be passed from configuration automatically
  • Some predefined methods, which are useful in testing
  • Suffix mechanism

Define new Page

Let's imagine that we have already setup webdriver for Config, and starting to create new page:

from selenium.webdriver.common.by import By
from zelenium import BasePage

class LoginPage(BasePage):
    title = (By.CSS_SELECTOR, "[data-test='title']")
    username = (By.CSS_SELECTOR, "[data-test='username']")
    password = (By.CSS_SELECTOR, "[data-test='password']")
    submit = (By.CSS_SELECTOR, "[data-test='submit']")

def main():
    login_page = LoginPage()
    print(login_page.title().text)

main()

If we execute it after opening something in browser - it will find element and print text inside of it.

How it works?

Well, BasePage also has a metaclass that will go all over page class fields and if field is tuple with two strings - it would replace it with BaseElement.

BaseElement itself has magic __call__ method, which executes when you 'call' class instance:

from zelenium import BE
elem = BE("by", "selector")
web_element = elem()  # Here you calls class instance and it will return
                      # WebElement for you. Just classic WebElement

Inherit pages

For example, you have several pages, which have same structure, but some different logic, for example:

from selenium.webdriver.common.by import By
from zelenium import BasePage

class LoginPage(BasePage):
    title = (By.CSS_SELECTOR, "[data-test='title']")
    username = (By.CSS_SELECTOR, "[data-test='username']")
    password = (By.CSS_SELECTOR, "[data-test='password']")
    submit = (By.CSS_SELECTOR, "[data-test='submit']")

    def login(self, username, password):
        self.username().send_keys(username)
        self.password().send_keys(password)
        self.submit().click()


class RegisterPage(LoginPage):
    full_name = (By.CSS_SELECTOR, "[data-test='full_name']")

    def register(self, full_name, username, password):
        self.full_name().send_key(full_name)
        self.username().send_keys(username)
        self.password().send_keys(password)
        self.submit().click()

Using this - you have no need to redefine elements on different pages - you could just inherit them, if they have same locators (or quite the same).

Format elements

Sometimes you need to define a lot of elements with similar locators. Zelenium offers two way to solve this. First is BaseElement formatting:

from selenium.webdriver.common.by import By
from zelenium import BasePage, BE

class DevicesPage(BasePage):
    _cell = BE(By.CSS_SELECTOR, "[data-test='devicesPageCell_{}']")
    user = _cell.format("user")
    imei = _cell.format("imei")
    iccid = _cell.format("iccid")
    model = _cell.format("model")

.format() method formats locator as a string and returns new instance of BaseElement.

Second mechanism is suffix:

from selenium.webdriver.common.by import By
from zelenium import BasePage

class DevicesPage(BasePage):
    __suffix = "devicesPageCell_"
    user = (By.CSS_SELECTOR, "[data-test='{s}_user']")
    imei = (By.CSS_SELECTOR, "[data-test='{s}_imei']")
    iccid = (By.CSS_SELECTOR, "[data-test='{s}_iccid']")
    model = (By.CSS_SELECTOR, "[data-test='{s}_model']")

Main differences of this two mechanisms are:

  • Suffix adds to locator automatically;
  • Suffix could be inherited;
  • Format could be used anywhere outside classes - you could format element in some functions according to changes on page.
  • Format requires usage of BaseElement class itself

Example of suffix inheritance:

from selenium.webdriver.common.by import By
from zelenium import BasePage


class LoginPage(BasePage):
    __suffix = "loginPageForm_"

    title = (By.CSS_SELECTOR, "[data-test='{s}title']")
    username = (By.CSS_SELECTOR, "[data-test='{s}username']")
    password = (By.CSS_SELECTOR, "[data-test='{s}password']")
    submit = (By.CSS_SELECTOR, "[data-test='{s}submit']")


class RegisterPage(LoginPage):
    __suffix = "registerPageForm_"

    email = (By.CSS_SELECTOR, "[data-test='{s}email']")
    confirm = (By.CSS_SELECTOR, "[data-test='{s}confirm']")


class RenamedRegisterPage(RegisterPage):
    __suffix = "renamedRegisterPageForm_"


def main():
    log = LoginPage()
    reg = RegisterPage()
    ren = RenamedRegisterPage()

    print(log.title)
    print(log.username)
    print(log.password)
    print(log.submit)
    print(reg.title)
    print(reg.username)
    print(reg.password)
    print(reg.submit)
    print(reg.email)
    print(reg.confirm)
    print(ren.title)
    print(ren.username)
    print(ren.password)
    print(ren.submit)
    print(ren.email)
    print(ren.confirm)


if __name__ == '__main__':
    main()

This code will output:

Element [data-test='loginPageForm_title'] (css selector)
Element [data-test='loginPageForm_username'] (css selector)
Element [data-test='loginPageForm_password'] (css selector)
Element [data-test='loginPageForm_submit'] (css selector)
Element [data-test='registerPageForm_title'] (css selector)
Element [data-test='registerPageForm_username'] (css selector)
Element [data-test='registerPageForm_password'] (css selector)
Element [data-test='registerPageForm_submit'] (css selector)
Element [data-test='registerPageForm_email'] (css selector)
Element [data-test='registerPageForm_confirm'] (css selector)
Element [data-test='renamedRegisterPageForm_title'] (css selector)
Element [data-test='renamedRegisterPageForm_username'] (css selector)
Element [data-test='renamedRegisterPageForm_password'] (css selector)
Element [data-test='renamedRegisterPageForm_submit'] (css selector)
Element [data-test='renamedRegisterPageForm_email'] (css selector)
Element [data-test='renamedRegisterPageForm_confirm'] (css selector)

Metadata

Release files for zelenium 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zelenium 1.0.3
File Size Uploaded Provenance Actions
zelenium-1.0.3.tar.gz 8.4 kB Without provenance Details for zelenium-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for zelenium 1.0.3
File Interpreter ABI Platform Actions
zelenium-1.0.3-py3-none-any.whl Python 3 none any Details for zelenium-1.0.3-py3-none-any.whl

Total release size: 27.2 kB

Release files / zelenium-1.0.3.tar.gz

Download URL zelenium-1.0.3.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b68784fb9cc25c0df1097cb6a939b3f9b7d3f94f92e40d34de21e0a7238b454b
BLAKE2b-256 checksum
How to use checksums
8a78b7ca56fc44d5be60be382e938481380d20aa3430e9059e2fd4b30b96171d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

Release files / zelenium-1.0.3-py3-none-any.whl

Download URL zelenium-1.0.3-py3-none-any.whl
Size 18.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42a8e239ec86b392f76b8fba0d52cf59bfea24b9ee1ad5661a6d2c0f70f1f1ba
BLAKE2b-256 checksum
How to use checksums
a4e9fabdd4da5e6a7964007584056fc301dc2fb58eb1e19e83cf9a94bc2159a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

Security for zelenium 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page