Skip to content

Instantly share code, notes, and snippets.

@schlomo
Last active July 21, 2017 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schlomo/8bd30a130bc28cfb1facf2d37ee25456 to your computer and use it in GitHub Desktop.
Save schlomo/8bd30a130bc28cfb1facf2d37ee25456 to your computer and use it in GitHub Desktop.
Demo for UI testing with selenium based on Zalenium, see http://blog.schlomo.schapiro.org/2017/07/web-ui-testing-made-easy-with-zalenium.html for details
/venv
*.png
*~
*.pyc
/.vscode
*.png
""" A simple Flask app """
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return """<!doctype html>
Hello, World!<br>
<a href="/demo">Demo</a>
"""
@app.route('/demo')
def demo():
return 'Demo works'
pylint
nose2
Flask
Flask-Testing
selenium==3.3.1
autopep8
#!/bin/bash
set -e
rm -Rf venv
virtualenv -p python3 --no-site-packages venv
source venv/bin/activate
pip install -r requirements.txt
zalenium=$(curl -sSL https://raw.githubusercontent.com/dosel/t/i/p)
export USE_NET_HOST=true
bash -s stop <<<"$zalenium"
bash -s start <<<"$zalenium"
nose2
bash -s stop <<<"$zalenium"
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from flask import Flask
from flask_testing import LiveServerTestCase
from urllib.error import URLError
class IntegrationTest(LiveServerTestCase):
@classmethod
def setUpClass(cls):
try:
cls.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
except URLError:
print("ERROR: Need selenium grid at %s, please start Zalanium like this:\n" % huburl +
"curl -sSL https://raw.githubusercontent.com/dosel/t/i/p | env USE_NET_HOST=true bash -s start")
exit(99)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def create_app(self):
from app import app
app.testing = True
app.config['LIVESERVER_PORT'] = 0
app.config['LIVESERVER_TIMEOUT'] = 10
return app
def test_main_view(self):
driver = self.driver
driver.get(self.get_server_url())
driver.get_screenshot_as_file("out1.png")
self.assertIn("Hello", driver.page_source)
def test_follow_link(self):
driver = self.driver
driver.get(self.get_server_url())
driver.get_screenshot_as_file("out2.png")
try:
driver.find_element_by_link_text("Demo").click()
except BaseException:
self.fail("Could not find link 'Demo'")
driver.get_screenshot_as_file("out3.png")
self.assertIn("works", driver.page_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment