Skip to content

Instantly share code, notes, and snippets.

@lukecampbell
Created January 16, 2015 19:15
Show Gist options
  • Save lukecampbell/aec1218c7f444cc1f2b0 to your computer and use it in GitHub Desktop.
Save lukecampbell/aec1218c7f444cc1f2b0 to your computer and use it in GitHub Desktop.
Integration testing with Selenium
#!/usr/bin/env python
import requests
from zipfile import ZipFile
import os
import stat
def main(path):
chrome_fo_mac = 'http://chromedriver.storage.googleapis.com/2.13/chromedriver_mac32.zip'
print "Downloading Chrome Driver to", path
zip_file = download_file(chrome_fo_mac, path)
with ZipFile(zip_file) as zf:
zf.extractall(path)
driver_path = os.path.join(path, "chromedriver")
if os.path.exists(driver_path):
os.chmod(driver_path, 0755)
def download_file(url, path):
local_filename = url.split('/')[-1]
local_filename = os.path.join(path, local_filename)
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
if r.status_code != 200:
raise IOError("Failed to GET URL %s because %s" % (url, r.text))
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
if __name__ == '__main__':
main()
#!/usr/bin/env python
'''
test.test_selenium
'''
from unittest import TestCase
from selenium import webdriver
import os
class TestSelenium(TestCase):
def setUp(self):
if not os.path.exists('drivers/chromedriver'):
from drivers.install_drivers import main
main('drivers')
self.webdriver = webdriver.Chrome('drivers/chromedriver')
def tearDown(self):
self.webdriver.quit()
class TestOOI(TestSelenium):
def test_ooi(self):
self.webdriver.get('http://data.ooi.rutgers.edu/')
assert 'OOI' in self.webdriver.title
class MyTest(TestSelenium):
def test_yahoo(self):
self.webdriver.get('http://www.yahoo.com/')
assert 'Yahoo' in self.webdriver.title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment