def init_firefox(self): desired_capabilities = DesiredCapabilities.FIREFOX desired_capabilities['loggingPrefs'] = {'browser': 'ALL'} browser = WebDriver( capabilities=desired_capabilities, firefox_profile=self.init_firefox_profile()) browser.implicitly_wait(3) browser.set_window_size(**self.IPHONE6_BROWSER_SIZE) return browser
def work(pid): print('Starting %s' % str(pid)) wd = WebDriver() wd.set_window_size(1280, 1000) wd.implicitly_wait(15) try: wd.get("http://trumpdonald.org/") while True: wd.find_element_by_id("can").click() finally: raise Exception("Test exited. pid=%s" % str(pid)) wd.quit() return None
class WebObject(StaticLiveServerTestCase): """Base class for page objects.""" @classmethod def setUpClass(self): super(WebObject, self).setUpClass() self.driver = WebDriver() self.driver.set_window_size(1024, 768) self.driver.maximize_window() self.driver.implicitly_wait(15) @classmethod def tearDownClass(self): self.driver.quit() super(WebObject, self).tearDownClass() def create_login_session(self, username): session_cookie = create_session_for_given_user(username) self.driver.get(self.live_server_url) self.driver.add_cookie(session_cookie)
def test_no_disclaimer_when_search_engine(self): profile = webdriver.FirefoxProfile() profile.set_preference( "general.useragent.override", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" ) browser = WebDriver(profile) browser.implicitly_wait(10) browser.set_window_size(width=1200, height=1200) old_browser = self.browser self.set_browser(browser) try: self.visit_home() self.find('#disclaimer').get_attribute('class').should.contain('fade') finally: browser.close() self.set_browser(old_browser) self.set_default_window_size() self.try_to_revive_browser()
def test_no_disclaimer_when_search_engine(self): profile = webdriver.FirefoxProfile() profile.set_preference( "general.useragent.override", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" ) browser = WebDriver(profile) browser.implicitly_wait(10) browser.set_window_size(width=1200, height=1200) old_browser = self.browser self.set_browser(browser) try: self.visit_home() self.find('#disclaimer').get_attribute('class').should.contain( 'fade') finally: browser.close() self.set_browser(old_browser) self.set_default_window_size() self.try_to_revive_browser()
def get_driver(self): if self.get_type() == 'firefox': driver = Firefox(firefox_profile=None, firefox_binary=self.bin_path, timeout=30, capabilities=None, proxy=None, executable_path=self.get_driver_path(), options=self.get_options()) elif self.get_type() == 'ie': driver = Ie(executable_path=self.driver_path, options=self.get_options()) if self.get_window_size(): driver.set_window_size(self.get_window_size()) else: print(self.get_driver_path()) driver = Chrome(executable_path=self.get_driver_path(), options=self.get_options()) # This work-around to enable download mode in headless chrome # https://bugs.chromium.org/p/chromium/issues/detail?id=696481 # TODO: Check if the latest chromedriver update or not enable_download_in_headless_chrome(driver, get_download_path()) return driver
class UITestCase(StaticLiveServerTestCase): def use_xvfb(self): from pyvirtualdisplay import Display self.display = Display('xvfb', visible=1, size=(DISPLAY_WIDTH, DISPLAY_HEIGHT)) self.display.start() self.driver = WebDriver() def setUp(self): try: self.driver = WebDriver() ui_is_not_available = False except WebDriverException: ui_is_not_available = True if ui_is_not_available: self.use_xvfb() self.driver.set_window_size(DISPLAY_WIDTH, DISPLAY_HEIGHT) self.driver.implicitly_wait(10) clear_caches() setup_for_ui_test() super(UITestCase, self).setUp() def tearDown(self): self.driver.quit() if hasattr(self, 'display'): self.display.stop() ContentType.objects.clear_cache() super(UITestCase, self).tearDown() def click(self, selector): self.find(selector).click() def click_when_visible(self, selector): element = self.find(selector) self.wait_until_visible(element) element.click() def find(self, selector): return self.driver.find_element_by_css_selector(selector) def find_name(self, name): return self.driver.find_element_by_name(name) def find_id(self, id): return self.driver.find_element_by_id(id) def is_visible(self, selector): element = self.find(selector) return element is not None and element.is_displayed() def process_login_form(self, username, password): username_elmt = self.wait_until_present('[name="username"]') password_elmt = self.find_name('password') username_elmt.send_keys(username) password_elmt.send_keys(password) self.click('form * button') def browse_to_url(self, url): self.driver.get(self.live_server_url + url) def browse_to_instance_url(self, url, instance=None): instance = instance if instance is not None else self.instance self.driver.get('%s/%s/%s' % (self.live_server_url, instance.url_name, url)) def find_anchor_by_url(self, url): return self.find("[href='%s']" % url) def wait_until_present(self, selector, timeout=10): """ Wait until an element with CSS 'selector' exists on the page. Useful for detecting that an operation loads the page you're expecting. """ element = [None] # use list so it can be set by inner scope def is_present(driver): element[0] = self.find(selector) return element[0] is not None and element[0].is_displayed() WebDriverWait(self.driver, timeout).until(is_present) return element[0] def wait_until_text_present(self, text, timeout=10): """ Wait until 'text' exists on the page. Useful for detecting that an operation loads the page you're expecting. """ WebDriverWait(self.driver, timeout).until( lambda driver: text in driver.page_source) def wait_until_enabled(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' is enabled. """ element = self._get_element(element_or_selector) WebDriverWait(self.driver, timeout).until( lambda driver: element.get_attribute("disabled") is None) return element def wait_until_visible(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' (known to already exist on the page) is displayed. """ element = self._get_element(element_or_selector) WebDriverWait(self.driver, timeout).until( lambda driver: element.is_displayed()) return element def wait_until_invisible(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' (known to already exist on the page) is not displayed. """ element = self._get_element(element_or_selector) def is_invisible(driver): try: return not element.is_displayed() except StaleElementReferenceException: return True WebDriverWait(self.driver, timeout).until(is_invisible) return element def wait_for_input_value(self, element_or_selector, value, timeout=10): """ Wait until 'element_or_selector' input has the specified value """ element = self._get_element(element_or_selector) WebDriverWait(self.driver, timeout).until( # It seems wrong, but selenium fetches the value of an input # element using get_attribute('value') lambda driver: element.get_attribute('value') == value) return element def _get_element(self, element_or_selector): if isinstance(element_or_selector, basestring): return self.find(element_or_selector) else: return element_or_selector
class EntrySeleleniumTests(StaticLiveServerTestCase): """Selenium tests for the entry form""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not settings.DEBUG: settings.DEBUG = True def setUp(self): """Handles login and things""" call_command('flush', interactive=False, verbosity=0) # Clears db call_command('loaddata', 'groups', commit=False, verbosity=0) call_command('loaddata', 'school', commit=False, verbosity=0) call_command('loaddata', 'permissions', commit=False, verbosity=0) call_command('loaddata', 'auth_users', commit=False, verbosity=0) call_command('loaddata', 'student', commit=False, verbosity=0) call_command('loaddata', 'advisor', commit=False, verbosity=0) call_command('loaddata', 'coordinator', commit=False, verbosity=0) call_command('loaddata', 'activityoptions', commit=False, verbosity=0) call_command('loaddata', 'learningobjectiveoptions', commit=False, verbosity=0) call_command('loaddata', 'sample_entries', commit=False, verbosity=0) self.selenium = WebDriver() self.selenium.set_window_size(1024, 800) self.selenium.get('{0}/{1}'.format(self.live_server_url, '')) self.selenium.find_element_by_xpath('//*[@id="djHideToolBarButton"]').click() self.selenium.implicitly_wait(10) self.selenium.find_element_by_link_text('Login').click() # Click on the student button in the gateway self.selenium.find_element_by_xpath('/html/body/center/md-content/div/div/div[1]/a').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") super() def tearDown(self): self.selenium.quit() super() def test_text_entry(self): """Test to ensure that a student can add a text entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(entry in box_text) def test_image_entry(self): """Test to ensure that a student can add an image entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[2]').click() entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(entry) # click on the inset image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists on the page. self.selenium.find_element_by_xpath("//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']") def test_video_entry(self): """Test to ensure that a student can add a video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert video self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[3]').click() entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(entry) # click on the insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') self.selenium.find_element_by_xpath('//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_image_text_entry(self): """Test to ensure that a student can add image+text entries""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time with a flower.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Insert the image self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the insert image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure the text is on the entries page box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(entry in box_text) # Ensure the image is on the entries page self.selenium.find_element_by_xpath("//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']") def test_video_text_entry(self): """Test to ensure that a student can add an text+video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Insert video self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button')\ .click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div')\ .text self.assertTrue(entry in box_text) self.selenium.find_element_by_xpath('//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_text_image_video_entry(self): """Test to ensure that a student can add an text+image+video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert text text_entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(text_entry) # Insert image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the inset image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(text_entry in box_text) self.selenium.find_element_by_xpath("//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']") self.selenium.find_element_by_xpath('//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_delete_entry(self): """Test to ensure that a student can delete an entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue('Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame(self.selenium.find_element_by_id('id_entry_iframe')) # Insert text text_entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(text_entry) # Insert image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the inset image button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(text_entry in box_text) self.selenium.find_element_by_xpath("//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']") self.selenium.find_element_by_xpath('//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') # Click on the entry that was created self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a').click() # Click on the delete button self.selenium.find_element_by_class_name('btn-danger').click() self.selenium.find_element_by_xpath('//*[@id="delete-modal"]/div/div/div[3]/a').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that the entry created is no longer on the entries page main_text = self.selenium.find_element_by_class_name('main').text # Check for text self.assertFalse(text_entry in main_text) # Check for image image_entry_xpath = "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" self.assertFalse(image_entry_xpath in main_text) # Check for video video_entry_xpath = '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]' self.assertFalse(video_entry_xpath in main_text)
class WStoreSeleniumTestCase(TestCase, LiveServerTestCase): fixtures = ['selenium_basic.json'] @classmethod def setUpClass(cls): super(WStoreSeleniumTestCase, cls).setUpClass() def setUp(self): # Open the page self.driver = WebDriver() self.driver.implicitly_wait(5) self.driver.set_window_size(1024, 768) self.driver.get(self.live_server_url) TestCase.setUp(self) def _check_container(self, container, offering_names): # Check offerings container container = self.driver.find_element_by_class_name(container) offering_elems = container.find_elements_by_class_name('menu-offering') self.assertEquals(len(offering_elems), len(offering_names)) for off_elem in offering_elems: title = off_elem.find_element_by_css_selector('h2') self.assertTrue(title.text in offering_names) def login(self, username='******'): # Set username username_elem = self.driver.find_element_by_name('username') username_elem.send_keys(username) # Set password password_elem = self.driver.find_element_by_name('password') password_elem.send_keys('admin') # Click login self.driver.find_element_by_css_selector('#login-form button').click() def oauth2_login(self, username='******'): from wstore.selenium_tests.tests import TESTING_PORT self.driver.get(self.live_server_url + '/oauth2/auth?response_type=code&client_id=test_app&redirect_uri=http://localhost:' + unicode(TESTING_PORT)) self.login(username) self.driver.find_element_by_class_name('btn-blue').click() time.sleep(1) # Get authorization code while self._server.call_received() < 1: pass code = self._server.get_path().split('=')[1] # Get access token opener = urllib2.build_opener() url = self.live_server_url + '/oauth2/token' data = 'client_id=test_app' data += '&client_secret=secret' data += '&grant_type=authorization_code' data += '&code=' + code data += '&redirect_uri=' + 'http://localhost:' + unicode(TESTING_PORT) headers = { 'content-type': 'application/form-url-encoded', } request = MethodRequest('POST', url, data, headers) response = opener.open(request) token = json.loads(response.read())['access_token'] return token def logout(self): self.driver.find_element_by_class_name('arrow-down-settings').click() options = self.driver.find_elements_by_css_selector('#settings-menu > li') options[-1].click() def tearDown(self): self.driver.quit() TestCase.tearDown(self) def back(self): self.driver.find_element_by_id('back').click() def view_all(self): self.driver.find_element_by_css_selector('#all').click() def search_keyword(self, keyword, id_='#text-search', btn='#search'): # Set search field search_elem = self.driver.find_element_by_css_selector(id_) search_elem.send_keys(keyword) # Click search button self.driver.find_element_by_css_selector(btn).click() def open_offering_details(self, offering_name): elements = self.driver.find_elements_by_class_name('menu-offering') for element in elements: if element.find_element_by_css_selector('h2').text == offering_name: element.click() break def _get_navs(self): submenu = self.driver.find_element_by_class_name('store-sub-menu') # Get first element return submenu.find_elements_by_css_selector('li') def click_first_cat(self): self.driver.find_element_by_id('menu-first-text').click() def click_second_cat(self): self.driver.find_element_by_id('menu-second-text').click() def click_third_cat(self): self.driver.find_element_by_id('menu-third-text').click() def click_first_nav(self): self._get_navs()[0].click() def click_second_nav(self): self._get_navs()[1].click() def _open_provider_option(self, option): self.driver.find_element_by_css_selector('#provider-options a.btn').click() self.driver.find_element_by_id(option).click() def create_offering_menu(self): self._open_provider_option('create-app') def fill_basic_offering_info(self, offering_info): # Name and version self.driver.find_element_by_css_selector('[name="app-name"]').send_keys(offering_info['name']) self.driver.find_element_by_css_selector('[name="app-version"]').send_keys(offering_info['version']) # Select the notification URL option if not offering_info['notification']: self.driver.find_element_by_css_selector('input[type="radio"][value="none"]').click() elif offering_info['notification'] == 'default': self.driver.find_element_by_css_selector('input[type="radio"][value="default"]').click() else: self.driver.find_element_by_css_selector('input[type="radio"][value="new"]').click() self.driver.find_element_by_id('notify').send_keys(offering_info['notification']) # Add the logo logo_path = os.path.join(settings.BASEDIR, 'wstore/ui/fiware/defaulttheme/static/assets/img/noimage.png') self.driver.find_element_by_id('img-logo').send_keys(logo_path) # Mark as open if needed if offering_info['open']: self.driver.find_element_by_id('open-offering').click() def _fill_usdl_form(self, usdl_info): # Fill description field self.driver.find_element_by_id('description').send_keys(usdl_info['description']) # Fill pricing info if needed if 'price' in usdl_info: self.driver.find_element_by_css_selector('#pricing-select option[value="single_payment"]').click() self.driver.find_element_by_id('price-input').send_keys(usdl_info['price']) if 'legal' in usdl_info: self.driver.find_element_by_id('legal-title').send_keys(usdl_info['legal']['title']) self.driver.find_element_by_id('legal-text').send_keys(usdl_info['legal']['text']) def _fill_usdl_upload(self, usdl_info): pass def _fill_usdl_url(self, usdl_info): pass def fill_usdl_info(self, usdl_info): # Select the correct method methods = { 'form': self._fill_usdl_form, 'upload': self._fill_usdl_upload, 'url': self._fill_usdl_url } methods[usdl_info['type']](usdl_info) def register_resource(self, resource_info): pass def click_tag(self, tag): tag_elems = self.driver.find_elements_by_class_name('tag') for te in tag_elems: if te.text == tag: te.click() break def fill_tax_address(self, tax): self.driver.find_element_by_id('street').send_keys(tax['street']) self.driver.find_element_by_id('postal').send_keys(tax['postal']) self.driver.find_element_by_id('city').send_keys(tax['city']) self.driver.find_element_by_id('country').send_keys(tax['country'])
# -*- coding: utf-8 -*- from selenium.webdriver.firefox.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains success = True driver = WebDriver() try: driver.set_window_size(1900, 1000) driver.get("https://shop.briggsandstratton.com/us/en") driver.find_element_by_link_text("Shop Repair Parts Now »").click() # This try statement is used to detect the Foresee overlay that pops up at this point in FF # Without this, Chrome will not continue to next step as it will error out while waiting try: wait = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.LINK_TEXT, "No, thanks"))) driver.find_element_by_link_text("No, thanks").click() except Exception, e: print("Foresee not seen here") WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.ID, "fsrOverlay"))) driver.find_element_by_id("aarisearch_brands_jl").click() wait = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//ul[@class='ari-caused-spacer-expand']/li[3]"))) driver.find_element_by_xpath("//ul[@class='ari-caused-spacer-expand']/li[3]").click() driver.find_element_by_id("arisearch_txtSearch").click() driver.find_element_by_id("arisearch_txtSearch").clear()
class ActivitySeleleniumTests(StaticLiveServerTestCase): """Selenium tests for the activity page""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not settings.DEBUG: settings.DEBUG = True if not settings.TESTING: settings.TESTING = True def setUp(self): """Handles login and things""" call_command('flush', interactive=False, verbosity=0) # Clears db call_command('loaddata', 'groups', commit=False, verbosity=0) call_command('loaddata', 'school', commit=False, verbosity=0) call_command('loaddata', 'permissions', commit=False, verbosity=0) call_command('loaddata', 'auth_users', commit=False, verbosity=0) call_command('loaddata', 'student', commit=False, verbosity=0) call_command('loaddata', 'advisor', commit=False, verbosity=0) call_command('loaddata', 'coordinator', commit=False, verbosity=0) call_command('loaddata', 'activityoptions', commit=False, verbosity=0) call_command('loaddata', 'learningobjectiveoptions', commit=False, verbosity=0) call_command('loaddata', 'sample_entries', commit=False, verbosity=0) self.selenium = WebDriver() self.selenium.set_window_size(1024, 800) self.selenium.get('{0}/{1}'.format(self.live_server_url, '')) self.selenium.find_element_by_xpath('//*[@id="djHideToolBarButton"]').click() self.selenium.implicitly_wait(10) self.selenium.find_element_by_link_text('Login').click() # Click on the student button in the gateway self.selenium.find_element_by_xpath('/html/body/center/md-content/div/div/div[1]/a').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") super() def tearDown(self): self.selenium.quit() super() def test_activity_form_back(self): """make sure the back button works""" self.selenium.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() self.selenium.find_element_by_name('activity_name').send_keys('Walking the cat') self.selenium.find_element_by_name('activity_description').send_keys('Walking the cat around the neighborhood') self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input').send_keys('02/07/1990') self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys('1234567890') self.selenium.find_element_by_name('advisor_email').send_keys('*****@*****.**') self.selenium.find_element_by_link_text('Back').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") def test_activity_form_error(self): """Tests to check errors on the activity form""" self.selenium.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() # self.selenium.find_element_by_name('activity_name').send_keys('') self.selenium.find_element_by_name('activity_description').send_keys('Walking with huahua around the neighborhood') self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input').send_keys('02/07/1990') self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys('1234567890') self.selenium.find_element_by_name('advisor_email').send_keys('*****@*****.**') self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/form/div[6]/div/input').click() self.selenium.find_element_by_name('activity_description').text def test_activity_form(self): """Tests to ensure that activities page has all necessary elements.""" self.selenium.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() self.selenium.find_element_by_name('activity_name').send_keys('Walking the cat') self.selenium.find_element_by_name('activity_description').send_keys('Walking the cat around the neighborhood') self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input').send_keys('02/07/1990') self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath('//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys('1234567890') self.selenium.find_element_by_name('advisor_email').send_keys('*****@*****.**') self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/form/div[6]/div/input').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") body_text = self.selenium.find_element_by_tag_name('body').text self.assertTrue('Walking the cat' in body_text) self.assertTrue('Walking the cat around the neighborhood' in body_text)
class UITestCase(LiveServerTestCase): def use_xvfb(self): from pyvirtualdisplay import Display self.display = Display('xvfb', visible=1, size=(DISPLAY_WIDTH, DISPLAY_HEIGHT)) self.display.start() self.driver = WebDriver() def setUp(self): try: self.driver = WebDriver() ui_is_not_available = False except WebDriverException: ui_is_not_available = True if ui_is_not_available: self.use_xvfb() self.driver.set_window_size(DISPLAY_WIDTH, DISPLAY_HEIGHT) self.driver.implicitly_wait(10) clear_caches() setup_for_ui_test() super(UITestCase, self).setUp() def tearDown(self): self.driver.quit() if hasattr(self, 'display'): self.display.stop() ContentType.objects.clear_cache() super(UITestCase, self).tearDown() def click(self, selector): self.find(selector).click() def click_when_visible(self, selector): element = self.find(selector) self.wait_until_visible(element) element.click() def find(self, selector): return self.driver.find_element_by_css_selector(selector) def find_name(self, name): return self.driver.find_element_by_name(name) def find_id(self, id): return self.driver.find_element_by_id(id) def process_login_form(self, username, password): username_elmt = self.wait_until_present('[name="username"]') password_elmt = self.find_name('password') username_elmt.send_keys(username) password_elmt.send_keys(password) self.click('form * button') def browse_to_url(self, url): self.driver.get(self.live_server_url + url) def browse_to_instance_url(self, url, instance=None): instance = instance if instance is not None else self.instance self.driver.get('%s/%s/%s' % (self.live_server_url, self.instance.url_name, url)) def find_anchor_by_url(self, url): return self.find("[href='%s']" % url) def wait_until_present(self, selector, timeout=10): """ Wait until an element with CSS 'selector' exists on the page. Useful for detecting that an operation loads the page you're expecting. """ element = [None] # use list so it can be set by inner scope def is_present(driver): element[0] = self.find(selector) return element[0] is not None WebDriverWait(self.driver, timeout).until(is_present) return element[0] def wait_until_text_present(self, text, timeout=10): """ Wait until 'text' exists on the page. Useful for detecting that an operation loads the page you're expecting. """ WebDriverWait(self.driver, timeout).until(lambda driver: text in driver.page_source) def wait_until_enabled(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' is enabled. """ element = self._get_element(element_or_selector) WebDriverWait(self.driver, timeout).until( lambda driver: element.get_attribute("disabled") is None) return element def wait_until_visible(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' (known to already exist on the page) is displayed. """ element = self._get_element(element_or_selector) WebDriverWait(self.driver, timeout).until(lambda driver: element.is_displayed()) return element def wait_until_invisible(self, element_or_selector, timeout=10): """ Wait until 'element_or_selector' (known to already exist on the page) is not displayed. """ element = self._get_element(element_or_selector) def is_invisible(driver): try: return not element.is_displayed() except StaleElementReferenceException: return True WebDriverWait(self.driver, timeout).until(is_invisible) return element def _get_element(self, element_or_selector): if isinstance(element_or_selector, basestring): return self.find(element_or_selector) else: return element_or_selector
class WStoreSeleniumTestCase(TestCase, LiveServerTestCase): fixtures = ['selenium_basic.json'] @classmethod def setUpClass(cls): super(WStoreSeleniumTestCase, cls).setUpClass() def setUp(self): # Open the page self.driver = WebDriver() self.driver.implicitly_wait(5) self.driver.set_window_size(1024, 768) self.driver.get(self.live_server_url) TestCase.setUp(self) def _check_container(self, container, offering_names): # Check offerings container container = self.driver.find_element_by_class_name(container) offering_elems = container.find_elements_by_class_name('menu-offering') self.assertEquals(len(offering_elems), len(offering_names)) for off_elem in offering_elems: title = off_elem.find_element_by_css_selector('h2') self.assertTrue(title.text in offering_names) def login(self, username='******'): # Set username username_elem = self.driver.find_element_by_name('username') username_elem.send_keys(username) # Set password password_elem = self.driver.find_element_by_name('password') password_elem.send_keys('admin') # Click login self.driver.find_element_by_css_selector('#login-form button').click() def oauth2_login(self, username='******'): from wstore.selenium_tests.tests import TESTING_PORT self.driver.get( self.live_server_url + '/oauth2/auth?response_type=code&client_id=test_app&redirect_uri=http://localhost:' + unicode(TESTING_PORT)) self.login(username) self.driver.find_element_by_class_name('btn-blue').click() time.sleep(1) # Get authorization code while self._server.call_received() < 1: pass code = self._server.get_path().split('=')[1] # Get access token opener = urllib2.build_opener() url = self.live_server_url + '/oauth2/token' data = 'client_id=test_app' data += '&client_secret=secret' data += '&grant_type=authorization_code' data += '&code=' + code data += '&redirect_uri=' + 'http://localhost:' + unicode(TESTING_PORT) headers = { 'content-type': 'application/form-url-encoded', } request = MethodRequest('POST', url, data, headers) response = opener.open(request) token = json.loads(response.read())['access_token'] return token def logout(self): self.driver.find_element_by_class_name( 'icon-double-angle-down').click() options = self.driver.find_elements_by_css_selector( '#settings-menu > li') options[-1].click() def tearDown(self): self.driver.quit() TestCase.tearDown(self) def back(self): self.driver.find_element_by_id('back').click() def view_all(self): self.driver.find_element_by_css_selector('#all').click() def search_keyword(self, keyword, id_='#text-search', btn='#search'): # Set search field search_elem = self.driver.find_element_by_css_selector(id_) search_elem.send_keys(keyword) # Click search button self.driver.find_element_by_css_selector(btn).click() def open_offering_details(self, offering_name): elements = self.driver.find_elements_by_class_name('menu-offering') for element in elements: if element.find_element_by_css_selector( 'h2').text == offering_name: element.click() break def _get_navs(self): submenu = self.driver.find_element_by_class_name('store-sub-menu') # Get first element return submenu.find_elements_by_css_selector('li') def click_first_cat(self): self.driver.find_element_by_id('menu-first-text').click() def click_second_cat(self): self.driver.find_element_by_id('menu-second-text').click() def click_third_cat(self): self.driver.find_element_by_id('menu-third-text').click() def click_first_nav(self): self._get_navs()[0].click() def click_second_nav(self): self._get_navs()[1].click() def _open_provider_option(self, option): self.driver.find_element_by_css_selector( '#provider-options a.btn').click() self.driver.find_element_by_id(option).click() def create_offering_menu(self): self._open_provider_option('create-app') def fill_basic_offering_info(self, offering_info): # Name and version self.driver.find_element_by_css_selector( '[name="app-name"]').send_keys(offering_info['name']) self.driver.find_element_by_css_selector( '[name="app-version"]').send_keys(offering_info['version']) # Select the notification URL option if not offering_info['notification']: self.driver.find_element_by_css_selector( 'input[type="radio"][value="none"]').click() elif offering_info['notification'] == 'default': self.driver.find_element_by_css_selector( 'input[type="radio"][value="default"]').click() else: self.driver.find_element_by_css_selector( 'input[type="radio"][value="new"]').click() self.driver.find_element_by_id('notify').send_keys( offering_info['notification']) # Add the logo logo_path = os.path.join( settings.BASEDIR, 'wstore/defaulttheme/static/assets/img/noimage.png') self.driver.find_element_by_id('img-logo').send_keys(logo_path) # Mark as open if needed if offering_info['open']: self.driver.find_element_by_id('open-offering').click() def fill_usdl_info(self, usdl_info): # Fill description field self.driver.find_element_by_id('description').send_keys( usdl_info['description']) self.driver.find_element_by_id('abstract').send_keys( usdl_info['abstract']) if 'legal' in usdl_info: self.driver.find_element_by_id('legal-title').send_keys( usdl_info['legal']['title']) self.driver.find_element_by_id('legal-text').send_keys( usdl_info['legal']['text']) def register_resource(self, resource_info): pass def click_tag(self, tag): tag_elems = self.driver.find_elements_by_class_name('tag') for te in tag_elems: if te.text == tag: te.click() break def select_plan(self, plan): element = WebDriverWait(self.driver, 5).until( EC.presence_of_element_located((By.ID, plan))) element.click() def accept_conditions(self): element = WebDriverWait(self.driver, 5).until( EC.presence_of_element_located((By.ID, "conditions-accepted"))) element.click() def fill_tax_address(self, tax): # Wait until the form is loaded element = WebDriverWait(self.driver, 5).until( EC.presence_of_element_located((By.ID, "street"))) element.send_keys(tax['street']) self.driver.find_element_by_id('postal').send_keys(tax['postal']) self.driver.find_element_by_id('city').send_keys(tax['city']) self.driver.find_element_by_id('country').send_keys(tax['country'])
class EntrySeleleniumTests(StaticLiveServerTestCase): """Selenium tests for the entry form""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not settings.DEBUG: settings.DEBUG = True def setUp(self): """Handles login and things""" call_command('flush', interactive=False, verbosity=0) # Clears db call_command('loaddata', 'groups', commit=False, verbosity=0) call_command('loaddata', 'school', commit=False, verbosity=0) call_command('loaddata', 'permissions', commit=False, verbosity=0) call_command('loaddata', 'auth_users', commit=False, verbosity=0) call_command('loaddata', 'student', commit=False, verbosity=0) call_command('loaddata', 'advisor', commit=False, verbosity=0) call_command('loaddata', 'coordinator', commit=False, verbosity=0) call_command('loaddata', 'activityoptions', commit=False, verbosity=0) call_command('loaddata', 'learningobjectiveoptions', commit=False, verbosity=0) call_command('loaddata', 'sample_entries', commit=False, verbosity=0) self.selenium = WebDriver() self.selenium.set_window_size(1024, 800) self.selenium.get('{0}/{1}'.format(self.live_server_url, '')) self.selenium.find_element_by_xpath( '//*[@id="djHideToolBarButton"]').click() self.selenium.implicitly_wait(10) self.selenium.find_element_by_link_text('Login').click() # Click on the student button in the gateway self.selenium.find_element_by_xpath( '/html/body/center/md-content/div/div/div[1]/a').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") super() def tearDown(self): self.selenium.quit() super() def test_text_entry(self): """Test to ensure that a student can add a text entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(entry in box_text) def test_image_entry(self): """Test to ensure that a student can add an image entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[2]').click() entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(entry) # click on the inset image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists on the page. self.selenium.find_element_by_xpath( "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" ) def test_video_entry(self): """Test to ensure that a student can add a video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert video self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[3]').click() entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(entry) # click on the insert video button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') self.selenium.find_element_by_xpath( '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_image_text_entry(self): """Test to ensure that a student can add image+text entries""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time with a flower.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Insert the image self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the insert image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.implicitly_wait(10) self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure the text is on the entries page box_text = self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(entry in box_text) # Ensure the image is on the entries page self.selenium.find_element_by_xpath( "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" ) def test_video_text_entry(self): """Test to ensure that a student can add an text+video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert text entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(entry) # Insert video self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button')\ .click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[1]/a/div')\ .text self.assertTrue(entry in box_text) self.selenium.find_element_by_xpath( '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_text_image_video_entry(self): """Test to ensure that a student can add an text+image+video entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert text text_entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(text_entry) # Insert image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the inset image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Insert video button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(text_entry in box_text) self.selenium.find_element_by_xpath( "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" ) self.selenium.find_element_by_xpath( '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') def test_delete_entry(self): """Test to ensure that a student can delete an entry""" # Click on the first activity box: Walking around the block self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div/div[1]/div/div[3]/a/div').click() self.selenium.find_element_by_link_text('Add an entry').click() # The following has 2 matching: Just walking and Adding entry...block header_text = self.selenium.find_elements_by_tag_name('h3')[1].text self.assertTrue( 'Adding entry for Walking around the block!' in header_text) # Switching to iframe focus self.selenium.switch_to_frame( self.selenium.find_element_by_id('id_entry_iframe')) # Insert text text_entry = 'I think I will bring my cat out next time.' self.selenium.find_element_by_class_name('note-editable')\ .send_keys(text_entry) # Insert image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[2]').click() image_entry = 'http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/input')\ .send_keys(image_entry) # Click on the inset image button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[1]/div/div/div[3]/button').click() # Insert video button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[5]/div[3]/button[3]').click() video_entry = 'https://www.youtube.com/watch?v=Rk_bV0RJRhs&index=20&list=PLJU_WCB1rA2SFwFy3lEvY_NH23ql1-Cgi' self.selenium.find_element_by_xpath('/html/body/div[2]/div[2]/div[4]/div/div/div[2]/div/input')\ .send_keys(video_entry) # Click on the insert video button self.selenium.find_element_by_xpath( '/html/body/div[2]/div[2]/div[4]/div/div/div[3]/button').click() # Switch back out of the iframe. self.selenium.switch_to_default_content() # Click on the submit button self.selenium.find_element_by_class_name('btn-success').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that entry exists as the first box on the page. box_text = self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div[1]/a/div').text self.assertTrue(text_entry in box_text) self.selenium.find_element_by_xpath( "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" ) self.selenium.find_element_by_xpath( '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]') # Click on the entry that was created self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[2]/div[1]/a').click() # Click on the delete button self.selenium.find_element_by_class_name('btn-danger').click() self.selenium.find_element_by_xpath( '//*[@id="delete-modal"]/div/div/div[3]/a').click() # Ensure that we are back on the entries page. self.selenium.find_element_by_link_text('Add an entry') # Ensure that the entry created is no longer on the entries page main_text = self.selenium.find_element_by_class_name('main').text # Check for text self.assertFalse(text_entry in main_text) # Check for image image_entry_xpath = "//img[@src='http://images.jfdaily.com/jiefang/wenyu/new/201409/W020140919421426345484.jpg']" self.assertFalse(image_entry_xpath in main_text) # Check for video video_entry_xpath = '//iframe[@src="//www.youtube.com/embed/Rk_bV0RJRhs"]' self.assertFalse(video_entry_xpath in main_text)
class ActivitySeleleniumTests(StaticLiveServerTestCase): """Selenium tests for the activity page""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not settings.DEBUG: settings.DEBUG = True if not settings.TESTING: settings.TESTING = True def setUp(self): """Handles login and things""" call_command('flush', interactive=False, verbosity=0) # Clears db call_command('loaddata', 'groups', commit=False, verbosity=0) call_command('loaddata', 'school', commit=False, verbosity=0) call_command('loaddata', 'permissions', commit=False, verbosity=0) call_command('loaddata', 'auth_users', commit=False, verbosity=0) call_command('loaddata', 'student', commit=False, verbosity=0) call_command('loaddata', 'advisor', commit=False, verbosity=0) call_command('loaddata', 'coordinator', commit=False, verbosity=0) call_command('loaddata', 'activityoptions', commit=False, verbosity=0) call_command('loaddata', 'learningobjectiveoptions', commit=False, verbosity=0) call_command('loaddata', 'sample_entries', commit=False, verbosity=0) self.selenium = WebDriver() self.selenium.set_window_size(1024, 800) self.selenium.get('{0}/{1}'.format(self.live_server_url, '')) self.selenium.find_element_by_xpath( '//*[@id="djHideToolBarButton"]').click() self.selenium.implicitly_wait(10) self.selenium.find_element_by_link_text('Login').click() # Click on the student button in the gateway self.selenium.find_element_by_xpath( '/html/body/center/md-content/div/div/div[1]/a').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") super() def tearDown(self): self.selenium.quit() super() def test_activity_form_back(self): """make sure the back button works""" self.selenium.find_element_by_xpath( "/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() self.selenium.find_element_by_name('activity_name').send_keys( 'Walking the cat') self.selenium.find_element_by_name('activity_description').send_keys( 'Walking the cat around the neighborhood') self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input' ).send_keys('02/07/1990') self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys( '1234567890') self.selenium.find_element_by_name('advisor_email').send_keys( '*****@*****.**') self.selenium.find_element_by_link_text('Back').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") def test_activity_form_error(self): """Tests to check errors on the activity form""" self.selenium.find_element_by_xpath( "/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() # self.selenium.find_element_by_name('activity_name').send_keys('') self.selenium.find_element_by_name('activity_description').send_keys( 'Walking with huahua around the neighborhood') self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input' ).send_keys('02/07/1990') self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys( '1234567890') self.selenium.find_element_by_name('advisor_email').send_keys( '*****@*****.**') self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[1]/form/div[6]/div/input').click() self.selenium.find_element_by_name('activity_description').text def test_activity_form(self): """Tests to ensure that activities page has all necessary elements.""" self.selenium.find_element_by_xpath( "/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div/a").click() self.selenium.find_element_by_name('activity_name').send_keys( 'Walking the cat') self.selenium.find_element_by_name('activity_description').send_keys( 'Walking the cat around the neighborhood') self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[1]/form/div[2]/div[2]/input' ).send_keys('02/07/1990') self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[2]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_activity_type"]/li[3]/label').click() self.selenium.find_element_by_xpath( '//*[@id="id_learned_objective"]/li[6]/label').click() self.selenium.find_element_by_name('activity_adviser').send_keys('Cat') self.selenium.find_element_by_name('advisor_phone').send_keys( '1234567890') self.selenium.find_element_by_name('advisor_email').send_keys( '*****@*****.**') self.selenium.find_element_by_xpath( '/html/body/div[1]/div[2]/div[1]/form/div[6]/div/input').click() self.selenium\ .find_element_by_xpath("//img[@src='/static/journal/activities/img" "/journal_sign.png']") body_text = self.selenium.find_element_by_tag_name('body').text self.assertTrue('Walking the cat' in body_text) self.assertTrue('Walking the cat around the neighborhood' in body_text)