I’ve started a small library to assist with automated testing of my anvil apps, so I thought I’d publish it to github and pypi:
It currently has general helper functions to click an element, send keystrokes to an element and to read the text value of an element plus login and signup functions for the normal forms from the anvil user service.
The app presents a login form. If you click ‘cancel’, you should see a ‘Hello World’ message and if you login with the credentials ‘email@example.com’ and ‘AnvilWorks’, you should instead see the message ‘Hello Owen’
Here is the code to test that the app is working properly:
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:
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.
when looking for the email_xpath /html/body/div/div/div/div/div/ul/li/input
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value does not evaluate to ``False``.
:param method: callable(WebDriver)
:param message: optional message for :exc:`TimeoutException`
:returns: the result of the last call to `method`
:raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
screen = None
stacktrace = None
end_time = time.monotonic() + self._timeout
value = method(self._driver)
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
if time.monotonic() > end_time:
raise TimeoutException(message, screen, stacktrace)
I am hoping this makes sense as I am a novice web dev.
It looks like my login HTML is a bit different and the email_xpath needs to be updated. I’m currently shooting from the hip, hoping to hit.
I am now realizing I should’ve maybe made a different post.
EDIT 3 was the issue, a many more
tags changed the indexing in /html/body/div[XXX], not sure if this is application-specific or because the anvil source, itself, has changed.