pys

A single instance of Pylenium for an entire Test Session

Usage

The pys fixture is used if you want a single instance of Pylenium that is shared across all tests within a Test Session.

Using py is the recommended way of working with Pylenium. Sharing data and states across tests is a bad practice. For example, you cannot run tests in parallel in this mode.

from pylenium.driver import Pylenium

class TestSauceDemo:
    def test_land_on_products_page_after_login(self, pys: Pylenium):
        pys.visit("https://www.saucedemo.com/")
        pys.get("#user-name").type("standard_user")
        pys.get("#password").type("secret_sauce")
        pys.get("#login-button").click()
        assert pys.contains("Products").should().be_visible()
        
    def test_add_item_to_cart_increments_counter_by_1(self, pys: Pylenium):
        pys.get("[id*='add-to-cart']").click()
        assert pys.get("a.shopping_cart_link").should().have_text("1")

Import Pylenium and add the type hint to your tests (as shown above) so you get intellisense and autocomplete when writing tests 💪

Arguments

  • none

Yields

  • Pylenium - an instance of Pylenium driver that interacts with the web

pys_config

When using pys, an instance of PyleniumConfig is also created and can be managed for the test session. You can access pys_config as a fixture or directly from pys.config

Access by Fixture

from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

class TestSauceDemo:
    def test_land_on_products_page_after_login(self, pys: Pylenium, pys_config: PyleniumConfig):
        pys_config.custom["user"] = "standard_user" # Set a value in one test...
        
        pys.visit("https://www.saucedemo.com/")
        pys.get("#user-name").type("standard_user")
        pys.get("#password").type("secret_sauce")
        pys.get("#login-button").click()
        assert pys.contains("Products").should().be_visible()
        
    def test_add_item_to_cart_increments_counter_by_1(self, pys: Pylenium, pys_config: PyleniumConfig):
        print(pys_config.custom.get("user")) # And use it in another test
        pys.get("[id*='add-to-cart']").click()
        assert pys.get("a.shopping_cart_link").should().have_text("1")

Recommended because it's fewer lines of code and you already have access via Pylenium

from pylenium.driver import Pylenium

class TestSauceDemo:
    def test_land_on_products_page_after_login(self, pys: Pylenium):
        pys.config.custom["user"] = "standard_user" # Set a value in one test...
        
        pys.visit("https://www.saucedemo.com/")
        pys.get("#user-name").type("standard_user")
        pys.get("#password").type("secret_sauce")
        pys.get("#login-button").click()
        assert pys.contains("Products").should().be_visible()
        
    def test_add_item_to_cart_increments_counter_by_1(self, pys: Pylenium):
        print(pys.config.custom.get("user")) # And use it in another test
        pys.get("[id*='add-to-cart']").click()
        assert pys.get("a.shopping_cart_link").should().have_text("1")

Last updated