Skip to content

Instantly share code, notes, and snippets.

@cwurld
Last active January 16, 2017 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwurld/7db1a642c32ff548c184 to your computer and use it in GitHub Desktop.
Save cwurld/7db1a642c32ff548c184 to your computer and use it in GitHub Desktop.
django selenium cheatsheet
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.select import Select
# DOES NOT WORK WITH selenium 2.5 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
class TestCalendar(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
cache.clear()
def tearDown(self):
# Call tearDown to close the web browser
self.browser.quit()
cache.clear()
def get(self, url):
self.browser.get(self.live_server_url + url)
def test(self):
self.browser.get('{}{}'.format(self.live_server_url, reverse('my_url'))
x = self.browser.find_element_by_css_selector('a selector')
# Clear an input and enter new text
my_input = self.browser.find_element_by_id('id_number')
my_input.clear()
my_input.send_keys('the text')
# Inspect a select
my_select_input = self.browser.find_element_by_id('id_number')
my_select = Select(my_select)
self.assertEqual(my_select.first_selected_option.text, 'the text')
# Where are we?
save_button = self.browser.find_element_by_id('submit-id-save')
save_button.click()
time.sleep(1)
self.assertEqual(self.selenium.current_url, self.live_server_url + reverse('my_url'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment