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 ‘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:
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.
when looking for the email_xpath /html/body/div[4]/div/div/div[2]/div/ul/li[2]/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
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.monotonic() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
EDIT 3
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.
EDIT 4
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.
If you time update your github repo (it isn’t the same version that is part of the PyPI repository), I’d love to fork the library to work with current anvil.
If not, no worries. I’ll just make my own and credit you if I have your permission