class HomePageSeleleniumTests(StaticLiveServerTestCase): """Selenium tests for the home page""" def setUp(self): """Opens the home page""" self.selenium = WebDriver() self.selenium.get('{0}{1}'.format(self.live_server_url, '/')) def tearDown(self): self.selenium.quit() super() def test_elements(self): """Tests to ensure the proper elements are present""" self.selenium.find_elements_by_link_text('iU') self.selenium.find_elements_by_link_text('Welcome') about_us = self.selenium\ .find_elements_by_xpath('//*[@href="#about-us"]') assert_true(len(about_us), 2) self.selenium.find_element_by_link_text('Features').click()
wd.implicitly_wait(wait) wd.get( "https://acorn.utoronto.ca/sws/welcome.do?welcome.dispatch#/") if wd.find_elements_by_id("inputID"): wd.find_element_by_id("inputID").click() wd.find_element_by_id("inputID").clear() wd.find_element_by_id("inputID").send_keys(utorid) wd.find_element_by_id("inputPassword").click() wd.find_element_by_id("inputPassword").clear() wd.find_element_by_id("inputPassword").send_keys(password) wd.find_element_by_name("login").click() wd.find_element_by_link_text("Manage Courses").click() wd.find_element_by_link_text(program_year).click() wd.find_element_by_id("searchBox").click() wd.find_element_by_id("searchBox").clear() wd.find_element_by_id("searchBox").send_keys(course) wd.find_element_by_id("searchBox").click() WebDriverWait(wd, 10).until( EC.presence_of_element_located( (By.ID, "course_search_results_list"))) wd.find_elements_by_xpath( "//ul[@id='course_search_results_list']//li//a")[term].click() if wd.find_elements_by_link_text("Enrol"): wd.find_element_by_link_text("Enrol").click() courses.remove(course) print("%s was successfully enrolled. Congrats!\n" % course) break except Exception: continue wd.quit()
class IntegrationTests(StaticLiveServerTestCase): """Integration tests by using the Selenium web drivers""" fixtures = ['test_data.json'] def setUp(self): self.driver = WebDriver() def login_player(self): """Helper function for logging in as a player""" self.driver.get('%s%s' % (self.live_server_url, '/login')) username = self.driver.find_element_by_id('id_username') password = self.driver.find_element_by_id('id_password') submit = self.driver.find_element_by_tag_name('button') username.send_keys('player') password.send_keys('player') submit.click() def login_developer(self): """Helper function for logging in as a developer""" self.driver.get('%s%s' % (self.live_server_url, '/login')) username = self.driver.find_element_by_id('id_username') password = self.driver.find_element_by_id('id_password') submit = self.driver.find_element_by_tag_name('button') username.send_keys('developer') password.send_keys('developer') submit.click() def test_login(self): """Test that logging in works properly""" self.login_player() assert 'player Account' in self.driver.page_source def test_registration(self): """ Test that registration completes, a verification email notification is sent and that verification works """ self.driver.get('%s%s' % (self.live_server_url, '/register')) first_name = self.driver.find_element_by_id('id_first_name') last_name = self.driver.find_element_by_id('id_last_name') username = self.driver.find_element_by_id('id_username') email = self.driver.find_element_by_id('id_email') password1 = self.driver.find_element_by_id('id_password') password2 = self.driver.find_element_by_id('id_password1') submit = self.driver.find_element_by_tag_name('button') first_name.send_keys('Selenium') last_name.send_keys('Tester') username.send_keys('selenium') email.send_keys('*****@*****.**') password1.send_keys('qwerty') password2.send_keys('qwerty') submit.click() #Check that the user is not active but email is supposedly sent test_user = User.objects.get(username="******") assert 'User successfully created, verification email sent!' in self.driver.page_source self.assertEqual(test_user.is_active, False) #Enter verification bytes and check that the user has been activated verification_bytes = test_user.type.verification_bytes self.driver.get('%s%s%s' % (self.live_server_url, '/user_verification/', verification_bytes)) assert 'User selenium has been verified. Please login.' in self.driver.page_source self.assertEqual(User.objects.get(username="******").is_active, True) def test_purchasing_game(self): """Tests that game can be purchased""" self.login_player() self.driver.get('%s%s' % (self.live_server_url, '/gamestorepage/3')) buy_button = self.driver.find_element_by_xpath("//input[@type='submit']") buy_button.click() confirm_button = self.driver.find_element_by_xpath("//button[text()='Pay']") confirm_button.click() assert 'has now been added to your game list!' in self.driver.page_source def test_editing_game(self): """Tests that game details can be edited by navigating through my games""" self.login_developer() self.driver.get('%s%s' % (self.live_server_url, '/mygames')) game_entry = self.driver.find_element_by_class_name('game-list-container') game_entry.click() edit_button = self.driver.find_element_by_link_text('Edit') edit_button.click() modify_button = self.driver.find_element_by_xpath("//button[text()='Modify']") game_name = self.driver.find_element_by_id('id_game_name') game_name.clear() game_name.send_keys('New game name') modify_button.click() assert 'New game name details have been updated.' in self.driver.page_source def test_playing_game(self): """Test playing, acquiring score, submitting the score and that score is saved""" self.login_player() self.driver.get('%s%s' % (self.live_server_url, '/boughtgames')) game_entry = self.driver.find_elements_by_class_name('game-list-container')[1] game_entry.click() play_button = self.driver.find_elements_by_link_text('Play')[1] play_button.click() assert 'Test Game' in self.driver.page_source self.driver.switch_to.frame(self.driver.find_element_by_id('gameFrame')) add_score_button = self.driver.find_element_by_id('add_points') #Click the button 5 times totaling 50 score add_score_button.click() add_score_button.click() add_score_button.click() add_score_button.click() add_score_button.click() submit_score_button = self.driver.find_element_by_id('submit_score') submit_score_button.click() self.driver.get('%s%s' % (self.live_server_url, '/boughtgames')) game_entry = self.driver.find_elements_by_class_name('game-list-container')[1] game_entry.click() game_table = self.driver.find_element_by_xpath( '(//table[@class="game-list-stats"])[2]//tr[2]') self.assertEqual(game_table.text, 'Personal highscore: 50') def tearDown(self): self.driver.quit()
class PollsTest(LiveServerTestCase): def setUp(self): user = User() user.username = "******" user.email = "*****@*****.**" user.set_password("adm1n") user.is_active = True user.is_superuser = True user.is_staff = True user.save() self.browser = WebDriver() self.browser.implicitly_wait(3) def tearDown(self): self.browser.quit() def test_can_create_new_poll_via_admin_site(self): # Gertrude opens her web browser, and goes to the admin page self.browser.get(self.live_server_url + '/admin/') # She sees the familiar 'Django administration' heading body = self.browser.find_element_by_tag_name('body') self.assertIn('Django administration', \ body.find_element_by_id("header").text) # She types in her username and passwords and hits return username_field = self.browser.find_element_by_name('username') username_field.send_keys('admin') password_field = self.browser.find_element_by_name('password') password_field.send_keys('adm1n') password_field.send_keys(Keys.RETURN) # her username and password are accepted, and she is taken to # the Site Administration page body = self.browser.find_element_by_tag_name('body') self.assertIn('Site administration', body.text) # She now sees a couple of hyperlink that says "Polls" polls_links = self.browser.find_elements_by_link_text('Polls') self.assertEquals(len(polls_links), 2) # The second one looks more exciting, so she clicks it polls_links[1].click() # She is taken to the polls listing page, which shows she has # no polls yet body = self.browser.find_element_by_tag_name('body') self.assertIn('0 polls', body.text) # She sees a link to 'add' a new poll, so she clicks it new_poll_link = self.browser.find_element_by_link_text('Add poll') new_poll_link.click() # She sees some input fields for "Question" and "Date published" body = self.browser.find_element_by_tag_name('body') self.assertIn('Question:', body.text) self.assertIn('Date published:', body.text) # She types in an interesting question for the Poll question_field = self.browser.find_element_by_name('question') question_field.send_keys("How awesome is Test-Driven Development?") # She sets the date and time of publication - it'll be a new year's # poll! date_field = self.browser.find_element_by_name('pub_date_0') date_field.send_keys('01/01/12') time_field = self.browser.find_element_by_name('pub_date_1') time_field.send_keys('00:00') # Gertrude clicks the save button save_button = self.browser.find_element_by_css_selector("input[value='Save']") save_button.click() # She is returned to the "Polls" listing, where she can see her # new poll, listed as a clickable link new_poll_links = self.browser.find_elements_by_link_text( "How awesome is Test-Driven Development?" ) self.assertEquals(len(new_poll_links), 1)