I’ve created a trivial app to demonstrate the use of the testing libary at https://test-library-demo.anvilapp.net/
The app presents a login form. If you click ‘cancel’, you should see a ‘Hello World’ message and if you login with the credentials ‘owen@example.com’ and ‘AnvilWorks’, you should instead see the message ‘Hello Owen’
Here is the code to test that the app is working properly:
import pytest
from anvil_test import session
browser = 'firefox'
url = 'https://test-library-demo.anvilapp.net/'
email = 'owen@example.com'
password = 'AnvilWorks'
xpaths = {
'cancel_button': '/html/body/div[4]/div/div/div[3]/button[2]',
'label': '/html/body/div[2]/div/div/div[1]/div[3]/div[1]/div/div/div/div/div/div/div/div/div/div/span'
}
@pytest.fixture
def test_session():
session.init(browser, url)
yield
session.browser.quit()
@pytest.fixture
def logged_in_session(test_session):
session.login(email, password)
@pytest.fixture
def anonymous_session(test_session):
session.click(xpaths['cancel_button'])
def test_anonymous_session(anonymous_session):
label_text = session.get_text(xpaths['label'])
assert label_text == 'Hello World'
def test_logged_in_session(logged_in_session):
label_text = session.get_text(xpaths['label'])
assert label_text == 'Hello Owen'
To run it, you will need to pip install selenium, pytest and anvil_test.
You will also need to have firefox available on your machine and to install geckodriver and ensure it is available on your path.
(If you prefer to use Chrome, install chromedriver, ensure it is on your path and replace “browser = ‘firefox’” in the demo code with “browser=‘chrome’”
If you save the code as ‘demo.py’, you can then run it using:
pytest demo.py
and you should see it automatically start a browser session, navigate to the app, click the cancel button, close that session, start a new session, login and close down. For both sessions, it will have extracted the message text, compared it with the expected result and will report whether the test passed or failed.