Fixed #33211 -- Updated tests for Selenium 4.0.0.

Replaced deprecated `find_element[s]_by_*()` usages, in favour of
`find_element[s]()` with an explicit `By`.
This commit is contained in:
Carlton Gibson 2021-10-20 15:54:30 +02:00
parent 8fa974fcdd
commit 2ccc0b22db
10 changed files with 411 additions and 325 deletions

View File

@ -116,8 +116,9 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
"""
Block until a new page has loaded and is ready.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
old_page = self.selenium.find_element_by_tag_name('html')
old_page = self.selenium.find_element(By.TAG_NAME, 'html')
yield
# Wait for the next page to be loaded
self.wait_until(ec.staleness_of(old_page), timeout=timeout)
@ -127,22 +128,24 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
"""
Log in to the admin.
"""
from selenium.webdriver.common.by import By
self.selenium.get('%s%s' % (self.live_server_url, login_url))
username_input = self.selenium.find_element_by_name('username')
username_input = self.selenium.find_element(By.NAME, 'username')
username_input.send_keys(username)
password_input = self.selenium.find_element_by_name('password')
password_input = self.selenium.find_element(By.NAME, 'password')
password_input.send_keys(password)
login_text = _('Log in')
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="%s"]' % login_text).click()
self.selenium.find_element(By.XPATH, '//input[@value="%s"]' % login_text).click()
def select_option(self, selector, value):
"""
Select the <OPTION> with the value `value` inside the <SELECT> widget
identified by the CSS selector `selector`.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
select = Select(self.selenium.find_element_by_css_selector(selector))
select = Select(self.selenium.find_element(By.CSS_SELECTOR, selector))
select.select_by_value(value)
def deselect_option(self, selector, value):
@ -150,8 +153,9 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
Deselect the <OPTION> with the value `value` inside the <SELECT> widget
identified by the CSS selector `selector`.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
select = Select(self.selenium.find_element_by_css_selector(selector))
select = Select(self.selenium.find_element(By.CSS_SELECTOR, selector))
select.deselect_by_value(value)
def assertCountSeleniumElements(self, selector, count, root_element=None):
@ -160,23 +164,25 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
`root_element` allow restriction to a pre-selected node.
"""
from selenium.webdriver.common.by import By
root_element = root_element or self.selenium
self.assertEqual(len(root_element.find_elements_by_css_selector(selector)), count)
self.assertEqual(len(root_element.find_elements(By.CSS_SELECTOR, selector)), count)
def _assertOptionsValues(self, options_selector, values):
from selenium.webdriver.common.by import By
if values:
options = self.selenium.find_elements_by_css_selector(options_selector)
options = self.selenium.find_elements(By.CSS_SELECTOR, options_selector)
actual_values = []
for option in options:
actual_values.append(option.get_attribute('value'))
self.assertEqual(values, actual_values)
else:
# Prevent the `find_elements_by_css_selector` call from blocking
# Prevent the `find_elements(By.CSS_SELECTOR, …)` call from blocking
# if the selector doesn't match any options as we expect it
# to be the case.
with self.disable_implicit_wait():
self.wait_until(
lambda driver: not driver.find_elements_by_css_selector(options_selector)
lambda driver: not driver.find_elements(By.CSS_SELECTOR, options_selector)
)
def assertSelectOptions(self, selector, values):
@ -198,5 +204,8 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
Return True if the element identified by `selector` has the CSS class
`klass`.
"""
return (self.selenium.find_element_by_css_selector(selector)
.get_attribute('class').find(klass) != -1)
from selenium.webdriver.common.by import By
return self.selenium.find_element(
By.CSS_SELECTOR,
selector,
).get_attribute('class').find(klass) != -1

View File

@ -951,6 +951,7 @@ subclass which provides that functionality. Replace it with
The code for this test may look as follows::
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import WebDriver
class MySeleniumTests(StaticLiveServerTestCase):
@ -969,11 +970,11 @@ The code for this test may look as follows::
def test_login(self):
self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
username_input = self.selenium.find_element_by_name("username")
username_input = self.selenium.find_element(By.NAME, "username")
username_input.send_keys('myuser')
password_input = self.selenium.find_element_by_name("password")
password_input = self.selenium.find_element(By.NAME, "password")
password_input.send_keys('secret')
self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Log in"]').click()
Finally, you may run the test as follows:
@ -1011,10 +1012,10 @@ out the `full reference`_ for more details.
from selenium.webdriver.support.wait import WebDriverWait
timeout = 2
...
self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Log in"]').click()
# Wait until the response is received
WebDriverWait(self.selenium, timeout).until(
lambda driver: driver.find_element_by_tag_name('body'))
lambda driver: driver.find_element(By.TAG_NAME, 'body'))
The tricky thing here is that there's really no such thing as a "page load,"
especially in modern web apps that generate HTML dynamically after the

View File

@ -1412,23 +1412,27 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
The status line for selected rows gets updated correctly (#22038).
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:auth_user_changelist'))
form_id = '#changelist-form'
# Test amount of rows in the Changelist
rows = self.selenium.find_elements_by_css_selector(
rows = self.selenium.find_elements(
By.CSS_SELECTOR,
'%s #result_list tbody tr' % form_id
)
self.assertEqual(len(rows), 1)
row = rows[0]
selection_indicator = self.selenium.find_element_by_css_selector(
selection_indicator = self.selenium.find_element(
By.CSS_SELECTOR,
'%s .action-counter' % form_id
)
all_selector = self.selenium.find_element_by_id('action-toggle')
row_selector = self.selenium.find_element_by_css_selector(
all_selector = self.selenium.find_element(By.ID, 'action-toggle')
row_selector = self.selenium.find_element(
By.CSS_SELECTOR,
'%s #result_list tbody tr:first-child .action-select' % form_id
)
@ -1455,12 +1459,13 @@ class SeleniumTests(AdminSeleniumTestCase):
should select all rows in-between.
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
Parent.objects.bulk_create([Parent(name='parent%d' % i) for i in range(5)])
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_changelist_parent_changelist'))
checkboxes = self.selenium.find_elements_by_css_selector('tr input.action-select')
checkboxes = self.selenium.find_elements(By.CSS_SELECTOR, 'tr input.action-select')
self.assertEqual(len(checkboxes), 5)
for c in checkboxes:
self.assertIs(c.get_property('checked'), False)
@ -1472,16 +1477,17 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertIs(checkboxes[-1].get_property('checked'), False)
def test_select_all_across_pages(self):
from selenium.webdriver.common.by import By
Parent.objects.bulk_create([Parent(name='parent%d' % i) for i in range(101)])
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_changelist_parent_changelist'))
selection_indicator = self.selenium.find_element_by_css_selector('.action-counter')
select_all_indicator = self.selenium.find_element_by_css_selector('.actions .all')
question = self.selenium.find_element_by_css_selector('.actions > .question')
clear = self.selenium.find_element_by_css_selector('.actions > .clear')
select_all = self.selenium.find_element_by_id('action-toggle')
select_across = self.selenium.find_elements_by_name('select_across')
selection_indicator = self.selenium.find_element(By.CSS_SELECTOR, '.action-counter')
select_all_indicator = self.selenium.find_element(By.CSS_SELECTOR, '.actions .all')
question = self.selenium.find_element(By.CSS_SELECTOR, '.actions > .question')
clear = self.selenium.find_element(By.CSS_SELECTOR, '.actions > .clear')
select_all = self.selenium.find_element(By.ID, 'action-toggle')
select_across = self.selenium.find_elements(By.NAME, 'select_across')
self.assertIs(question.is_displayed(), False)
self.assertIs(clear.is_displayed(), False)
@ -1522,16 +1528,17 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertIs(select_all_indicator.is_displayed(), False)
def test_actions_warn_on_pending_edits(self):
from selenium.webdriver.common.by import By
Parent.objects.create(name='foo')
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_changelist_parent_changelist'))
name_input = self.selenium.find_element_by_id('id_form-0-name')
name_input = self.selenium.find_element(By.ID, 'id_form-0-name')
name_input.clear()
name_input.send_keys('bar')
self.selenium.find_element_by_id('action-toggle').click()
self.selenium.find_element_by_name('index').click() # Go
self.selenium.find_element(By.ID, 'action-toggle').click()
self.selenium.find_element(By.NAME, 'index').click() # Go
alert = self.selenium.switch_to.alert
try:
self.assertEqual(
@ -1543,6 +1550,7 @@ class SeleniumTests(AdminSeleniumTestCase):
alert.dismiss()
def test_save_with_changes_warns_on_pending_action(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
Parent.objects.create(name='parent')
@ -1550,13 +1558,13 @@ class SeleniumTests(AdminSeleniumTestCase):
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_changelist_parent_changelist'))
name_input = self.selenium.find_element_by_id('id_form-0-name')
name_input = self.selenium.find_element(By.ID, 'id_form-0-name')
name_input.clear()
name_input.send_keys('other name')
Select(
self.selenium.find_element_by_name('action')
self.selenium.find_element(By.NAME, 'action')
).select_by_value('delete_selected')
self.selenium.find_element_by_name('_save').click()
self.selenium.find_element(By.NAME, '_save').click()
alert = self.selenium.switch_to.alert
try:
self.assertEqual(
@ -1569,6 +1577,7 @@ class SeleniumTests(AdminSeleniumTestCase):
alert.dismiss()
def test_save_without_changes_warns_on_pending_action(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
Parent.objects.create(name='parent')
@ -1577,9 +1586,9 @@ class SeleniumTests(AdminSeleniumTestCase):
self.selenium.get(self.live_server_url + reverse('admin:admin_changelist_parent_changelist'))
Select(
self.selenium.find_element_by_name('action')
self.selenium.find_element(By.NAME, 'action')
).select_by_value('delete_selected')
self.selenium.find_element_by_name('_save').click()
self.selenium.find_element(By.NAME, '_save').click()
alert = self.selenium.switch_to.alert
try:
self.assertEqual(

View File

@ -1275,6 +1275,7 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
The "Add another XXX" link correctly adds items to the stacked formset.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder4_add'))
@ -1282,12 +1283,12 @@ class SeleniumTests(AdminSeleniumTestCase):
rows_selector = '%s .dynamic-inner4stacked_set' % inline_id
self.assertCountSeleniumElements(rows_selector, 3)
add_button = self.selenium.find_element_by_link_text(
'Add another Inner4 stacked')
add_button = self.selenium.find_element(By.LINK_TEXT, 'Add another Inner4 stacked')
add_button.click()
self.assertCountSeleniumElements(rows_selector, 4)
def test_delete_stackeds(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder4_add'))
@ -1296,19 +1297,19 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements(rows_selector, 3)
add_button = self.selenium.find_element_by_link_text(
'Add another Inner4 stacked')
add_button = self.selenium.find_element(By.LINK_TEXT, 'Add another Inner4 stacked')
add_button.click()
add_button.click()
self.assertCountSeleniumElements(rows_selector, 5)
for delete_link in self.selenium.find_elements_by_css_selector('%s .inline-deletelink' % inline_id):
for delete_link in self.selenium.find_elements(By.CSS_SELECTOR, '%s .inline-deletelink' % inline_id):
delete_link.click()
with self.disable_implicit_wait():
self.assertCountSeleniumElements(rows_selector, 0)
def test_delete_invalid_stacked_inlines(self):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder4_add'))
@ -1317,42 +1318,46 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements(rows_selector, 3)
add_button = self.selenium.find_element_by_link_text(
'Add another Inner4 stacked')
add_button = self.selenium.find_element(
By.LINK_TEXT,
'Add another Inner4 stacked',
)
add_button.click()
add_button.click()
self.assertCountSeleniumElements('#id_inner4stacked_set-4-dummy', 1)
# Enter some data and click 'Save'.
self.selenium.find_element_by_name('dummy').send_keys('1')
self.selenium.find_element_by_name('inner4stacked_set-0-dummy').send_keys('100')
self.selenium.find_element_by_name('inner4stacked_set-1-dummy').send_keys('101')
self.selenium.find_element_by_name('inner4stacked_set-2-dummy').send_keys('222')
self.selenium.find_element_by_name('inner4stacked_set-3-dummy').send_keys('103')
self.selenium.find_element_by_name('inner4stacked_set-4-dummy').send_keys('222')
self.selenium.find_element(By.NAME, 'dummy').send_keys('1')
self.selenium.find_element(By.NAME, 'inner4stacked_set-0-dummy').send_keys('100')
self.selenium.find_element(By.NAME, 'inner4stacked_set-1-dummy').send_keys('101')
self.selenium.find_element(By.NAME, 'inner4stacked_set-2-dummy').send_keys('222')
self.selenium.find_element(By.NAME, 'inner4stacked_set-3-dummy').send_keys('103')
self.selenium.find_element(By.NAME, 'inner4stacked_set-4-dummy').send_keys('222')
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# Sanity check.
self.assertCountSeleniumElements(rows_selector, 5)
errorlist = self.selenium.find_element_by_css_selector(
'%s .dynamic-inner4stacked_set .errorlist li' % inline_id
errorlist = self.selenium.find_element(
By.CSS_SELECTOR,
'%s .dynamic-inner4stacked_set .errorlist li' % inline_id,
)
self.assertEqual('Please correct the duplicate values below.', errorlist.text)
delete_link = self.selenium.find_element_by_css_selector('#inner4stacked_set-4 .inline-deletelink')
delete_link = self.selenium.find_element(By.CSS_SELECTOR, '#inner4stacked_set-4 .inline-deletelink')
delete_link.click()
self.assertCountSeleniumElements(rows_selector, 4)
with self.disable_implicit_wait(), self.assertRaises(NoSuchElementException):
self.selenium.find_element_by_css_selector('%s .dynamic-inner4stacked_set .errorlist li' % inline_id)
self.selenium.find_element(By.CSS_SELECTOR, '%s .dynamic-inner4stacked_set .errorlist li' % inline_id)
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# The objects have been created in the database.
self.assertEqual(Inner4Stacked.objects.all().count(), 4)
def test_delete_invalid_tabular_inlines(self):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder4_add'))
@ -1361,39 +1366,43 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements(rows_selector, 3)
add_button = self.selenium.find_element_by_link_text(
'Add another Inner4 tabular')
add_button = self.selenium.find_element(
By.LINK_TEXT,
'Add another Inner4 tabular'
)
add_button.click()
add_button.click()
self.assertCountSeleniumElements('#id_inner4tabular_set-4-dummy', 1)
# Enter some data and click 'Save'.
self.selenium.find_element_by_name('dummy').send_keys('1')
self.selenium.find_element_by_name('inner4tabular_set-0-dummy').send_keys('100')
self.selenium.find_element_by_name('inner4tabular_set-1-dummy').send_keys('101')
self.selenium.find_element_by_name('inner4tabular_set-2-dummy').send_keys('222')
self.selenium.find_element_by_name('inner4tabular_set-3-dummy').send_keys('103')
self.selenium.find_element_by_name('inner4tabular_set-4-dummy').send_keys('222')
self.selenium.find_element(By.NAME, 'dummy').send_keys('1')
self.selenium.find_element(By.NAME, 'inner4tabular_set-0-dummy').send_keys('100')
self.selenium.find_element(By.NAME, 'inner4tabular_set-1-dummy').send_keys('101')
self.selenium.find_element(By.NAME, 'inner4tabular_set-2-dummy').send_keys('222')
self.selenium.find_element(By.NAME, 'inner4tabular_set-3-dummy').send_keys('103')
self.selenium.find_element(By.NAME, 'inner4tabular_set-4-dummy').send_keys('222')
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# Sanity Check.
self.assertCountSeleniumElements(rows_selector, 5)
# Non-field errorlist is in its own <tr> just before
# tr#inner4tabular_set-3:
errorlist = self.selenium.find_element_by_css_selector(
errorlist = self.selenium.find_element(
By.CSS_SELECTOR,
'%s #inner4tabular_set-3 + .row-form-errors .errorlist li' % inline_id
)
self.assertEqual('Please correct the duplicate values below.', errorlist.text)
delete_link = self.selenium.find_element_by_css_selector('#inner4tabular_set-4 .inline-deletelink')
delete_link = self.selenium.find_element(By.CSS_SELECTOR, '#inner4tabular_set-4 .inline-deletelink')
delete_link.click()
self.assertCountSeleniumElements(rows_selector, 4)
with self.disable_implicit_wait(), self.assertRaises(NoSuchElementException):
self.selenium.find_element_by_css_selector('%s .dynamic-inner4tabular_set .errorlist li' % inline_id)
self.selenium.find_element(By.CSS_SELECTOR, '%s .dynamic-inner4tabular_set .errorlist li' % inline_id)
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# The objects have been created in the database.
self.assertEqual(Inner4Tabular.objects.all().count(), 4)
@ -1402,50 +1411,51 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
The "Add another XXX" link correctly adds items to the inline form.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_profilecollection_add'))
# There's only one inline to start with and it has the correct ID.
self.assertCountSeleniumElements('.dynamic-profile_set', 1)
self.assertEqual(
self.selenium.find_elements_by_css_selector('.dynamic-profile_set')[0].get_attribute('id'),
self.selenium.find_elements(By.CSS_SELECTOR, '.dynamic-profile_set')[0].get_attribute('id'),
'profile_set-0',
)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-0 input[name=profile_set-0-first_name]', 1)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-0 input[name=profile_set-0-last_name]', 1)
# Add an inline
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
# The inline has been added, it has the right id, and it contains the
# correct fields.
self.assertCountSeleniumElements('.dynamic-profile_set', 2)
self.assertEqual(
self.selenium.find_elements_by_css_selector('.dynamic-profile_set')[1].get_attribute('id'),
self.selenium.find_elements(By.CSS_SELECTOR, '.dynamic-profile_set')[1].get_attribute('id'),
'profile_set-1',
)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-1 input[name=profile_set-1-first_name]', 1)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-1 input[name=profile_set-1-last_name]', 1)
# Let's add another one to be sure
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
self.assertCountSeleniumElements('.dynamic-profile_set', 3)
self.assertEqual(
self.selenium.find_elements_by_css_selector('.dynamic-profile_set')[2].get_attribute('id'),
self.selenium.find_elements(By.CSS_SELECTOR, '.dynamic-profile_set')[2].get_attribute('id'),
'profile_set-2',
)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-2 input[name=profile_set-2-first_name]', 1)
self.assertCountSeleniumElements('.dynamic-profile_set#profile_set-2 input[name=profile_set-2-last_name]', 1)
# Enter some data and click 'Save'
self.selenium.find_element_by_name('profile_set-0-first_name').send_keys('0 first name 1')
self.selenium.find_element_by_name('profile_set-0-last_name').send_keys('0 last name 2')
self.selenium.find_element_by_name('profile_set-1-first_name').send_keys('1 first name 1')
self.selenium.find_element_by_name('profile_set-1-last_name').send_keys('1 last name 2')
self.selenium.find_element_by_name('profile_set-2-first_name').send_keys('2 first name 1')
self.selenium.find_element_by_name('profile_set-2-last_name').send_keys('2 last name 2')
self.selenium.find_element(By.NAME, 'profile_set-0-first_name').send_keys('0 first name 1')
self.selenium.find_element(By.NAME, 'profile_set-0-last_name').send_keys('0 last name 2')
self.selenium.find_element(By.NAME, 'profile_set-1-first_name').send_keys('1 first name 1')
self.selenium.find_element(By.NAME, 'profile_set-1-last_name').send_keys('1 last name 2')
self.selenium.find_element(By.NAME, 'profile_set-2-first_name').send_keys('2 first name 1')
self.selenium.find_element(By.NAME, 'profile_set-2-last_name').send_keys('2 last name 2')
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# The objects have been created in the database
self.assertEqual(ProfileCollection.objects.all().count(), 1)
@ -1453,6 +1463,7 @@ class SeleniumTests(AdminSeleniumTestCase):
def test_add_inline_link_absent_for_view_only_parent_model(self):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
user = User.objects.create_user('testing', password='password', is_staff=True)
user.user_permissions.add(
Permission.objects.get(codename='view_poll', content_type=ContentType.objects.get_for_model(Poll))
@ -1468,17 +1479,18 @@ class SeleniumTests(AdminSeleniumTestCase):
self.selenium.get(self.live_server_url + change_url)
with self.disable_implicit_wait():
with self.assertRaises(NoSuchElementException):
self.selenium.find_element_by_link_text('Add another Question')
self.selenium.find_element(By.LINK_TEXT, 'Add another Question')
def test_delete_inlines(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_profilecollection_add'))
# Add a few inlines
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element_by_link_text('Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Profile').click()
self.assertCountSeleniumElements('#profile_set-group table tr.dynamic-profile_set', 5)
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-0', 1)
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-1', 1)
@ -1486,10 +1498,14 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-3', 1)
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-4', 1)
# Click on a few delete buttons
self.selenium.find_element_by_css_selector(
'form#profilecollection_form tr.dynamic-profile_set#profile_set-1 td.delete a').click()
self.selenium.find_element_by_css_selector(
'form#profilecollection_form tr.dynamic-profile_set#profile_set-2 td.delete a').click()
self.selenium.find_element(
By.CSS_SELECTOR,
'form#profilecollection_form tr.dynamic-profile_set#profile_set-1 td.delete a',
).click()
self.selenium.find_element(
By.CSS_SELECTOR,
'form#profilecollection_form tr.dynamic-profile_set#profile_set-2 td.delete a',
).click()
# The rows are gone and the IDs have been re-sequenced
self.assertCountSeleniumElements('#profile_set-group table tr.dynamic-profile_set', 3)
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-0', 1)
@ -1497,18 +1513,20 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements('form#profilecollection_form tr.dynamic-profile_set#profile_set-2', 1)
def test_collapsed_inlines(self):
from selenium.webdriver.common.by import By
# Collapsed inlines have SHOW/HIDE links.
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_author_add'))
# One field is in a stacked inline, other in a tabular one.
test_fields = ['#id_nonautopkbook_set-0-title', '#id_nonautopkbook_set-2-0-title']
show_links = self.selenium.find_elements_by_link_text('SHOW')
show_links = self.selenium.find_elements(By.LINK_TEXT, 'SHOW')
self.assertEqual(len(show_links), 3)
for show_index, field_name in enumerate(test_fields, 0):
self.wait_until_invisible(field_name)
show_links[show_index].click()
self.wait_until_visible(field_name)
hide_links = self.selenium.find_elements_by_link_text('HIDE')
hide_links = self.selenium.find_elements(By.LINK_TEXT, 'HIDE')
self.assertEqual(len(hide_links), 2)
for hide_index, field_name in enumerate(test_fields, 0):
self.wait_until_visible(field_name)
@ -1516,17 +1534,18 @@ class SeleniumTests(AdminSeleniumTestCase):
self.wait_until_invisible(field_name)
def test_added_stacked_inline_with_collapsed_fields(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_teacher_add'))
self.selenium.find_element_by_link_text('Add another Child').click()
self.selenium.find_element(By.LINK_TEXT, 'Add another Child').click()
test_fields = ['#id_child_set-0-name', '#id_child_set-1-name']
show_links = self.selenium.find_elements_by_link_text('SHOW')
show_links = self.selenium.find_elements(By.LINK_TEXT, 'SHOW')
self.assertEqual(len(show_links), 2)
for show_index, field_name in enumerate(test_fields, 0):
self.wait_until_invisible(field_name)
show_links[show_index].click()
self.wait_until_visible(field_name)
hide_links = self.selenium.find_elements_by_link_text('HIDE')
hide_links = self.selenium.find_elements(By.LINK_TEXT, 'HIDE')
self.assertEqual(len(hide_links), 2)
for hide_index, field_name in enumerate(test_fields, 0):
self.wait_until_visible(field_name)
@ -1561,31 +1580,32 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertIn(element.value_of_css_property(prop), colors)
def test_inline_formset_error_input_border(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder5_add'))
self.wait_until_visible('#id_dummy')
self.selenium.find_element_by_id('id_dummy').send_keys(1)
self.selenium.find_element(By.ID, 'id_dummy').send_keys(1)
fields = ['id_inner5stacked_set-0-dummy', 'id_inner5tabular_set-0-dummy']
show_links = self.selenium.find_elements_by_link_text('SHOW')
show_links = self.selenium.find_elements(By.LINK_TEXT, 'SHOW')
for show_index, field_name in enumerate(fields):
show_links[show_index].click()
self.wait_until_visible('#' + field_name)
self.selenium.find_element_by_id(field_name).send_keys(1)
self.selenium.find_element(By.ID, field_name).send_keys(1)
# Before save all inputs have default border
for inline in ('stacked', 'tabular'):
for field_name in ('name', 'select', 'text'):
element_id = 'id_inner5%s_set-0-%s' % (inline, field_name)
self.assertBorder(
self.selenium.find_element_by_id(element_id),
self.selenium.find_element(By.ID, element_id),
'1px solid #cccccc',
)
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
# Test the red border around inputs by css selectors
stacked_selectors = ['.errors input', '.errors select', '.errors textarea']
for selector in stacked_selectors:
self.assertBorder(
self.selenium.find_element_by_css_selector(selector),
self.selenium.find_element(By.CSS_SELECTOR, selector),
'1px solid #ba2121',
)
tabular_selectors = [
@ -1593,20 +1613,21 @@ class SeleniumTests(AdminSeleniumTestCase):
]
for selector in tabular_selectors:
self.assertBorder(
self.selenium.find_element_by_css_selector(selector),
self.selenium.find_element(By.CSS_SELECTOR, selector),
'1px solid #ba2121',
)
def test_inline_formset_error(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_holder5_add'))
stacked_inline_formset_selector = 'div#inner5stacked_set-group fieldset.module.collapse'
tabular_inline_formset_selector = 'div#inner5tabular_set-group fieldset.module.collapse'
# Inlines without errors, both inlines collapsed
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.assertCountSeleniumElements(stacked_inline_formset_selector + '.collapsed', 1)
self.assertCountSeleniumElements(tabular_inline_formset_selector + '.collapsed', 1)
show_links = self.selenium.find_elements_by_link_text('SHOW')
show_links = self.selenium.find_elements(By.LINK_TEXT, 'SHOW')
self.assertEqual(len(show_links), 2)
# Inlines with errors, both inlines expanded
@ -1614,8 +1635,8 @@ class SeleniumTests(AdminSeleniumTestCase):
for show_index, field_name in enumerate(test_fields):
show_links[show_index].click()
self.wait_until_visible(field_name)
self.selenium.find_element_by_id(field_name[1:]).send_keys(1)
hide_links = self.selenium.find_elements_by_link_text('HIDE')
self.selenium.find_element(By.ID, field_name[1:]).send_keys(1)
hide_links = self.selenium.find_elements(By.LINK_TEXT, 'HIDE')
self.assertEqual(len(hide_links), 2)
for hide_index, field_name in enumerate(test_fields):
hide_link = hide_links[hide_index]
@ -1623,7 +1644,7 @@ class SeleniumTests(AdminSeleniumTestCase):
hide_link.click()
self.wait_until_invisible(field_name)
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
with self.disable_implicit_wait():
self.assertCountSeleniumElements(stacked_inline_formset_selector + '.collapsed', 0)
self.assertCountSeleniumElements(tabular_inline_formset_selector + '.collapsed', 0)
@ -1635,10 +1656,11 @@ class SeleniumTests(AdminSeleniumTestCase):
The item added by the "Add another XXX" link must use the correct
verbose_name in the inline form.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret')
# Hide sidebar.
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_course_add'))
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
toggle_button = self.selenium.find_element(By.CSS_SELECTOR, '#toggle-nav-sidebar')
toggle_button.click()
# Each combination of horizontal/vertical filter with stacked/tabular
# inlines.
@ -1654,16 +1676,16 @@ class SeleniumTests(AdminSeleniumTestCase):
with self.subTest(url=url_name):
self.selenium.get(self.live_server_url + reverse(url_name))
# First inline shows the verbose_name.
available, chosen = self.selenium.find_elements_by_css_selector(css_selector % 0)
available, chosen = self.selenium.find_elements(By.CSS_SELECTOR, css_selector % 0)
self.assertEqual(available.text, 'AVAILABLE ATTENDANT')
self.assertEqual(chosen.text, 'CHOSEN ATTENDANT')
# Added inline should also have the correct verbose_name.
self.selenium.find_element_by_link_text('Add another Class').click()
available, chosen = self.selenium.find_elements_by_css_selector(css_selector % 1)
self.selenium.find_element(By.LINK_TEXT, 'Add another Class').click()
available, chosen = self.selenium.find_elements(By.CSS_SELECTOR, css_selector % 1)
self.assertEqual(available.text, 'AVAILABLE ATTENDANT')
self.assertEqual(chosen.text, 'CHOSEN ATTENDANT')
# Third inline should also have the correct verbose_name.
self.selenium.find_element_by_link_text('Add another Class').click()
available, chosen = self.selenium.find_elements_by_css_selector(css_selector % 2)
self.selenium.find_element(By.LINK_TEXT, 'Add another Class').click()
available, chosen = self.selenium.find_elements(By.CSS_SELECTOR, css_selector % 2)
self.assertEqual(available.text, 'AVAILABLE ATTENDANT')
self.assertEqual(chosen.text, 'CHOSEN ATTENDANT')

View File

@ -331,11 +331,13 @@ class SeleniumTests(AdminSeleniumTestCase):
@contextmanager
def select2_ajax_wait(self, timeout=10):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
yield
with self.disable_implicit_wait():
try:
loading_element = self.selenium.find_element_by_css_selector(
loading_element = self.selenium.find_element(
By.CSS_SELECTOR,
'li.select2-results__option.loading-results'
)
except NoSuchElementException:
@ -344,24 +346,25 @@ class SeleniumTests(AdminSeleniumTestCase):
self.wait_until(ec.staleness_of(loading_element), timeout=timeout)
def test_select(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
self.selenium.get(self.live_server_url + reverse('autocomplete_admin:admin_views_answer_add'))
elem = self.selenium.find_element_by_css_selector('.select2-selection')
elem = self.selenium.find_element(By.CSS_SELECTOR, '.select2-selection')
elem.click() # Open the autocomplete dropdown.
results = self.selenium.find_element_by_css_selector('.select2-results')
results = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results')
self.assertTrue(results.is_displayed())
option = self.selenium.find_element_by_css_selector('.select2-results__option')
option = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results__option')
self.assertEqual(option.text, 'No results found')
elem.click() # Close the autocomplete dropdown.
q1 = Question.objects.create(question='Who am I?')
Question.objects.bulk_create(Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10))
elem.click() # Reopen the dropdown now that some objects exist.
result_container = self.selenium.find_element_by_css_selector('.select2-results')
result_container = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results')
self.assertTrue(result_container.is_displayed())
# PAGINATOR_SIZE results and "Loading more results".
self.assertCountSeleniumElements('.select2-results__option', PAGINATOR_SIZE + 1, root_element=result_container)
search = self.selenium.find_element_by_css_selector('.select2-search__field')
search = self.selenium.find_element(By.CSS_SELECTOR, '.select2-search__field')
# Load next page of results by scrolling to the bottom of the list.
with self.select2_ajax_wait():
for _ in range(PAGINATOR_SIZE + 1):
@ -386,27 +389,28 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements('.select2-results__option', 1, root_element=result_container)
# Select the result.
search.send_keys(Keys.RETURN)
select = Select(self.selenium.find_element_by_id('id_question'))
select = Select(self.selenium.find_element(By.ID, 'id_question'))
self.assertEqual(select.first_selected_option.get_attribute('value'), str(q1.pk))
def test_select_multiple(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
self.selenium.get(self.live_server_url + reverse('autocomplete_admin:admin_views_question_add'))
elem = self.selenium.find_element_by_css_selector('.select2-selection')
elem = self.selenium.find_element(By.CSS_SELECTOR, '.select2-selection')
elem.click() # Open the autocomplete dropdown.
results = self.selenium.find_element_by_css_selector('.select2-results')
results = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results')
self.assertTrue(results.is_displayed())
option = self.selenium.find_element_by_css_selector('.select2-results__option')
option = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results__option')
self.assertEqual(option.text, 'No results found')
elem.click() # Close the autocomplete dropdown.
Question.objects.create(question='Who am I?')
Question.objects.bulk_create(Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10))
elem.click() # Reopen the dropdown now that some objects exist.
result_container = self.selenium.find_element_by_css_selector('.select2-results')
result_container = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results')
self.assertTrue(result_container.is_displayed())
self.assertCountSeleniumElements('.select2-results__option', PAGINATOR_SIZE + 1, root_element=result_container)
search = self.selenium.find_element_by_css_selector('.select2-search__field')
search = self.selenium.find_element(By.CSS_SELECTOR, '.select2-search__field')
# Load next page of results by scrolling to the bottom of the list.
with self.select2_ajax_wait():
for _ in range(PAGINATOR_SIZE + 1):
@ -427,25 +431,27 @@ class SeleniumTests(AdminSeleniumTestCase):
elem.click()
search.send_keys(Keys.ARROW_DOWN)
search.send_keys(Keys.RETURN)
select = Select(self.selenium.find_element_by_id('id_related_questions'))
select = Select(self.selenium.find_element(By.ID, 'id_related_questions'))
self.assertEqual(len(select.all_selected_options), 2)
def test_inline_add_another_widgets(self):
from selenium.webdriver.common.by import By
def assertNoResults(row):
elem = row.find_element_by_css_selector('.select2-selection')
elem = row.find_element(By.CSS_SELECTOR, '.select2-selection')
elem.click() # Open the autocomplete dropdown.
results = self.selenium.find_element_by_css_selector('.select2-results')
results = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results')
self.assertTrue(results.is_displayed())
option = self.selenium.find_element_by_css_selector('.select2-results__option')
option = self.selenium.find_element(By.CSS_SELECTOR, '.select2-results__option')
self.assertEqual(option.text, 'No results found')
# Autocomplete works in rows present when the page loads.
self.selenium.get(self.live_server_url + reverse('autocomplete_admin:admin_views_book_add'))
rows = self.selenium.find_elements_by_css_selector('.dynamic-authorship_set')
rows = self.selenium.find_elements(By.CSS_SELECTOR, '.dynamic-authorship_set')
self.assertEqual(len(rows), 3)
assertNoResults(rows[0])
# Autocomplete works in rows added using the "Add another" button.
self.selenium.find_element_by_link_text('Add another Authorship').click()
rows = self.selenium.find_elements_by_css_selector('.dynamic-authorship_set')
self.selenium.find_element(By.LINK_TEXT, 'Add another Authorship').click()
rows = self.selenium.find_elements(By.CSS_SELECTOR, '.dynamic-authorship_set')
self.assertEqual(len(rows), 4)
assertNoResults(rows[-1])

View File

@ -115,53 +115,57 @@ class SeleniumTests(AdminSeleniumTestCase):
self.selenium.execute_script("localStorage.removeItem('django.admin.navSidebarIsOpen')")
def test_sidebar_starts_open(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
main_element = self.selenium.find_element_by_css_selector('#main')
main_element = self.selenium.find_element(By.CSS_SELECTOR, '#main')
self.assertIn('shifted', main_element.get_attribute('class').split())
def test_sidebar_can_be_closed(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
toggle_button = self.selenium.find_element(By.CSS_SELECTOR, '#toggle-nav-sidebar')
self.assertEqual(toggle_button.tag_name, 'button')
self.assertEqual(toggle_button.get_attribute('aria-label'), 'Toggle navigation')
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
for link in self.selenium.find_elements(By.CSS_SELECTOR, '#nav-sidebar a'):
self.assertEqual(link.get_attribute('tabIndex'), '0')
toggle_button.click()
# Hidden sidebar is not reachable via keyboard navigation.
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
for link in self.selenium.find_elements(By.CSS_SELECTOR, '#nav-sidebar a'):
self.assertEqual(link.get_attribute('tabIndex'), '-1')
main_element = self.selenium.find_element_by_css_selector('#main')
main_element = self.selenium.find_element(By.CSS_SELECTOR, '#main')
self.assertNotIn('shifted', main_element.get_attribute('class').split())
def test_sidebar_state_persists(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
self.assertIsNone(self.selenium.execute_script("return localStorage.getItem('django.admin.navSidebarIsOpen')"))
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
toggle_button = self.selenium.find_element(By.CSS_SELECTOR, '#toggle-nav-sidebar')
toggle_button.click()
self.assertEqual(
self.selenium.execute_script("return localStorage.getItem('django.admin.navSidebarIsOpen')"),
'false',
)
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
main_element = self.selenium.find_element_by_css_selector('#main')
main_element = self.selenium.find_element(By.CSS_SELECTOR, '#main')
self.assertNotIn('shifted', main_element.get_attribute('class').split())
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
toggle_button = self.selenium.find_element(By.CSS_SELECTOR, '#toggle-nav-sidebar')
# Hidden sidebar is not reachable via keyboard navigation.
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
for link in self.selenium.find_elements(By.CSS_SELECTOR, '#nav-sidebar a'):
self.assertEqual(link.get_attribute('tabIndex'), '-1')
toggle_button.click()
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
for link in self.selenium.find_elements(By.CSS_SELECTOR, '#nav-sidebar a'):
self.assertEqual(link.get_attribute('tabIndex'), '0')
self.assertEqual(
self.selenium.execute_script("return localStorage.getItem('django.admin.navSidebarIsOpen')"),
'true',
)
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
main_element = self.selenium.find_element_by_css_selector('#main')
main_element = self.selenium.find_element(By.CSS_SELECTOR, '#main')
self.assertIn('shifted', main_element.get_attribute('class').split())
def test_sidebar_filter_persists(self):
from selenium.webdriver.common.by import By
self.selenium.get(
self.live_server_url +
reverse('test_with_sidebar:auth_user_changelist')
@ -170,6 +174,6 @@ class SeleniumTests(AdminSeleniumTestCase):
"return sessionStorage.getItem('django.admin.navSidebarFilterValue')"
)
self.assertIsNone(self.selenium.execute_script(filter_value_script))
filter_input = self.selenium.find_element_by_css_selector('#nav-filter')
filter_input = self.selenium.find_element(By.CSS_SELECTOR, '#nav-filter')
filter_input.send_keys('users')
self.assertEqual(self.selenium.execute_script(filter_value_script), 'users')

View File

@ -4578,8 +4578,9 @@ class SeleniumTests(AdminSeleniumTestCase):
self.p1 = PrePopulatedPost.objects.create(title='A Long Title', published=True, slug='a-long-title')
def test_login_button_centered(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + reverse('admin:login'))
button = self.selenium.find_element_by_css_selector('.submit-row input')
button = self.selenium.find_element(By.CSS_SELECTOR, '.submit-row input')
offset_left = button.get_property('offsetLeft')
offset_right = (
button.get_property('offsetParent').get_property('offsetWidth') -
@ -4594,52 +4595,53 @@ class SeleniumTests(AdminSeleniumTestCase):
and with stacked and tabular inlines.
Refs #13068, #9264, #9983, #9784.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(self.live_server_url + reverse('admin:admin_views_mainprepopulated_add'))
self.wait_for('.select2')
# Main form ----------------------------------------------------------
self.selenium.find_element_by_id('id_pubdate').send_keys('2012-02-18')
self.selenium.find_element(By.ID, 'id_pubdate').send_keys('2012-02-18')
self.select_option('#id_status', 'option two')
self.selenium.find_element_by_id('id_name').send_keys(' the mAin nÀMë and it\'s awεšomeıııİ')
slug1 = self.selenium.find_element_by_id('id_slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_slug2').get_attribute('value')
slug3 = self.selenium.find_element_by_id('id_slug3').get_attribute('value')
self.selenium.find_element(By.ID, 'id_name').send_keys(' the mAin nÀMë and it\'s awεšomeıııİ')
slug1 = self.selenium.find_element(By.ID, 'id_slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_slug2').get_attribute('value')
slug3 = self.selenium.find_element(By.ID, 'id_slug3').get_attribute('value')
self.assertEqual(slug1, 'the-main-name-and-its-awesomeiiii-2012-02-18')
self.assertEqual(slug2, 'option-two-the-main-name-and-its-awesomeiiii')
self.assertEqual(slug3, 'the-main-n\xe0m\xeb-and-its-aw\u03b5\u0161ome\u0131\u0131\u0131i')
# Stacked inlines ----------------------------------------------------
# Initial inline
self.selenium.find_element_by_id('id_relatedprepopulated_set-0-pubdate').send_keys('2011-12-17')
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-0-pubdate').send_keys('2011-12-17')
self.select_option('#id_relatedprepopulated_set-0-status', 'option one')
self.selenium.find_element_by_id('id_relatedprepopulated_set-0-name').send_keys(
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-0-name').send_keys(
' here is a sŤāÇkeð inline ! '
)
slug1 = self.selenium.find_element_by_id('id_relatedprepopulated_set-0-slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_relatedprepopulated_set-0-slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-0-slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-0-slug2').get_attribute('value')
self.assertEqual(slug1, 'here-is-a-stacked-inline-2011-12-17')
self.assertEqual(slug2, 'option-one-here-is-a-stacked-inline')
initial_select2_inputs = self.selenium.find_elements_by_class_name('select2-selection')
initial_select2_inputs = self.selenium.find_elements(By.CLASS_NAME, 'select2-selection')
# Inline formsets have empty/invisible forms.
# Only the 4 visible select2 inputs are initialized.
num_initial_select2_inputs = len(initial_select2_inputs)
self.assertEqual(num_initial_select2_inputs, 4)
# Add an inline
self.selenium.find_elements_by_link_text('Add another Related prepopulated')[0].click()
self.selenium.find_elements(By.LINK_TEXT, 'Add another Related prepopulated')[0].click()
self.assertEqual(
len(self.selenium.find_elements_by_class_name('select2-selection')),
len(self.selenium.find_elements(By.CLASS_NAME, 'select2-selection')),
num_initial_select2_inputs + 2
)
self.selenium.find_element_by_id('id_relatedprepopulated_set-1-pubdate').send_keys('1999-01-25')
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-1-pubdate').send_keys('1999-01-25')
self.select_option('#id_relatedprepopulated_set-1-status', 'option two')
self.selenium.find_element_by_id('id_relatedprepopulated_set-1-name').send_keys(
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-1-name').send_keys(
' now you haVe anöther sŤāÇkeð inline with a very ... '
'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text... '
)
slug1 = self.selenium.find_element_by_id('id_relatedprepopulated_set-1-slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_relatedprepopulated_set-1-slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-1-slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-1-slug2').get_attribute('value')
# 50 characters maximum for slug1 field
self.assertEqual(slug1, 'now-you-have-another-stacked-inline-with-a-very-lo')
# 60 characters maximum for slug2 field
@ -4647,47 +4649,47 @@ class SeleniumTests(AdminSeleniumTestCase):
# Tabular inlines ----------------------------------------------------
# Initial inline
element = self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-status')
element = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-0-status')
self.selenium.execute_script('window.scrollTo(0, %s);' % element.location['y'])
self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-pubdate').send_keys('1234-12-07')
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-0-pubdate').send_keys('1234-12-07')
self.select_option('#id_relatedprepopulated_set-2-0-status', 'option two')
self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-name').send_keys(
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-0-name').send_keys(
'And now, with a tÃbűlaŘ inline !!!'
)
slug1 = self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-0-slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-0-slug2').get_attribute('value')
self.assertEqual(slug1, 'and-now-with-a-tabular-inline-1234-12-07')
self.assertEqual(slug2, 'option-two-and-now-with-a-tabular-inline')
# Add an inline
# Button may be outside the browser frame.
element = self.selenium.find_elements_by_link_text('Add another Related prepopulated')[1]
element = self.selenium.find_elements(By.LINK_TEXT, 'Add another Related prepopulated')[1]
self.selenium.execute_script('window.scrollTo(0, %s);' % element.location['y'])
element.click()
self.assertEqual(
len(self.selenium.find_elements_by_class_name('select2-selection')),
len(self.selenium.find_elements(By.CLASS_NAME, 'select2-selection')),
num_initial_select2_inputs + 4
)
self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-pubdate').send_keys('1981-08-22')
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-1-pubdate').send_keys('1981-08-22')
self.select_option('#id_relatedprepopulated_set-2-1-status', 'option one')
self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-name').send_keys(
self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-1-name').send_keys(
r'tÃbűlaŘ inline with ignored ;"&*^\%$#@-/`~ characters'
)
slug1 = self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-1-slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_relatedprepopulated_set-2-1-slug2').get_attribute('value')
self.assertEqual(slug1, 'tabular-inline-with-ignored-characters-1981-08-22')
self.assertEqual(slug2, 'option-one-tabular-inline-with-ignored-characters')
# Add an inline without an initial inline.
# The button is outside of the browser frame.
self.selenium.execute_script("window.scrollTo(0, document.body.scrollHeight);")
self.selenium.find_elements_by_link_text('Add another Related prepopulated')[2].click()
self.selenium.find_elements(By.LINK_TEXT, 'Add another Related prepopulated')[2].click()
self.assertEqual(
len(self.selenium.find_elements_by_class_name('select2-selection')),
len(self.selenium.find_elements(By.CLASS_NAME, 'select2-selection')),
num_initial_select2_inputs + 6
)
# Save and check that everything is properly stored in the database
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.assertEqual(MainPrepopulated.objects.all().count(), 1)
MainPrepopulated.objects.get(
name=' the mAin nÀMë and it\'s awεšomeıııİ',
@ -4733,6 +4735,8 @@ class SeleniumTests(AdminSeleniumTestCase):
The prepopulation works for existing objects too, as long as
the original field is empty (#19082).
"""
from selenium.webdriver.common.by import By
# Slugs are empty to start with.
item = MainPrepopulated.objects.create(
name=' this is the mAin nÀMë',
@ -4746,24 +4750,24 @@ class SeleniumTests(AdminSeleniumTestCase):
object_url = self.live_server_url + reverse('admin:admin_views_mainprepopulated_change', args=(item.id,))
self.selenium.get(object_url)
self.selenium.find_element_by_id('id_name').send_keys(' the best')
self.selenium.find_element(By.ID, 'id_name').send_keys(' the best')
# The slugs got prepopulated since they were originally empty
slug1 = self.selenium.find_element_by_id('id_slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_slug2').get_attribute('value')
self.assertEqual(slug1, 'this-is-the-main-name-the-best-2012-02-18')
self.assertEqual(slug2, 'option-two-this-is-the-main-name-the-best')
# Save the object
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.get(object_url)
self.selenium.find_element_by_id('id_name').send_keys(' hello')
self.selenium.find_element(By.ID, 'id_name').send_keys(' hello')
# The slugs got prepopulated didn't change since they were originally not empty
slug1 = self.selenium.find_element_by_id('id_slug1').get_attribute('value')
slug2 = self.selenium.find_element_by_id('id_slug2').get_attribute('value')
slug1 = self.selenium.find_element(By.ID, 'id_slug1').get_attribute('value')
slug2 = self.selenium.find_element(By.ID, 'id_slug2').get_attribute('value')
self.assertEqual(slug1, 'this-is-the-main-name-the-best-2012-02-18')
self.assertEqual(slug2, 'option-two-this-is-the-main-name-the-best')
@ -4772,22 +4776,25 @@ class SeleniumTests(AdminSeleniumTestCase):
The 'collapse' class in fieldsets definition allows to
show/hide the appropriate field section.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(self.live_server_url + reverse('admin:admin_views_article_add'))
self.assertFalse(self.selenium.find_element_by_id('id_title').is_displayed())
self.selenium.find_elements_by_link_text('Show')[0].click()
self.assertTrue(self.selenium.find_element_by_id('id_title').is_displayed())
self.assertEqual(self.selenium.find_element_by_id('fieldsetcollapser0').text, "Hide")
self.assertFalse(self.selenium.find_element(By.ID, 'id_title').is_displayed())
self.selenium.find_elements(By.LINK_TEXT, 'Show')[0].click()
self.assertTrue(self.selenium.find_element(By.ID, 'id_title').is_displayed())
self.assertEqual(self.selenium.find_element(By.ID, 'fieldsetcollapser0').text, "Hide")
def test_first_field_focus(self):
"""JavaScript-assisted auto-focus on first usable form field."""
from selenium.webdriver.common.by import By
# First form field has a single widget
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
with self.wait_page_loaded():
self.selenium.get(self.live_server_url + reverse('admin:admin_views_picture_add'))
self.assertEqual(
self.selenium.switch_to.active_element,
self.selenium.find_element_by_id('id_name')
self.selenium.find_element(By.ID, 'id_name')
)
# First form field has a MultiWidget
@ -4795,19 +4802,20 @@ class SeleniumTests(AdminSeleniumTestCase):
self.selenium.get(self.live_server_url + reverse('admin:admin_views_reservation_add'))
self.assertEqual(
self.selenium.switch_to.active_element,
self.selenium.find_element_by_id('id_start_date_0')
self.selenium.find_element(By.ID, 'id_start_date_0')
)
def test_cancel_delete_confirmation(self):
"Cancelling the deletion of an object takes the user back one page."
from selenium.webdriver.common.by import By
pizza = Pizza.objects.create(name="Double Cheese")
url = reverse('admin:admin_views_pizza_change', args=(pizza.id,))
full_url = self.live_server_url + url
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(full_url)
self.selenium.find_element_by_class_name('deletelink').click()
self.selenium.find_element(By.CLASS_NAME, 'deletelink').click()
# Click 'cancel' on the delete page.
self.selenium.find_element_by_class_name('cancel-link').click()
self.selenium.find_element(By.CLASS_NAME, 'cancel-link').click()
# Wait until we're back on the change page.
self.wait_for_text('#content h1', 'Change pizza')
self.assertEqual(self.selenium.current_url, full_url)
@ -4818,6 +4826,7 @@ class SeleniumTests(AdminSeleniumTestCase):
Cancelling the deletion of an object with relations takes the user back
one page.
"""
from selenium.webdriver.common.by import By
pizza = Pizza.objects.create(name="Double Cheese")
topping1 = Topping.objects.create(name="Cheddar")
topping2 = Topping.objects.create(name="Mozzarella")
@ -4826,9 +4835,9 @@ class SeleniumTests(AdminSeleniumTestCase):
full_url = self.live_server_url + url
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(full_url)
self.selenium.find_element_by_class_name('deletelink').click()
self.selenium.find_element(By.CLASS_NAME, 'deletelink').click()
# Click 'cancel' on the delete page.
self.selenium.find_element_by_class_name('cancel-link').click()
self.selenium.find_element(By.CLASS_NAME, 'cancel-link').click()
# Wait until we're back on the change page.
self.wait_for_text('#content h1', 'Change pizza')
self.assertEqual(self.selenium.current_url, full_url)
@ -4839,6 +4848,7 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
list_editable foreign keys have add/change popups.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
s1 = Section.objects.create(name='Test section')
Article.objects.create(
@ -4850,109 +4860,114 @@ class SeleniumTests(AdminSeleniumTestCase):
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(self.live_server_url + reverse('admin:admin_views_article_changelist'))
# Change popup
self.selenium.find_element_by_id('change_id_form-0-section').click()
self.selenium.find_element(By.ID, 'change_id_form-0-section').click()
self.wait_for_and_switch_to_popup()
self.wait_for_text('#content h1', 'Change section')
name_input = self.selenium.find_element_by_id('id_name')
name_input = self.selenium.find_element(By.ID, 'id_name')
name_input.clear()
name_input.send_keys('<i>edited section</i>')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
# Hide sidebar.
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
toggle_button = self.selenium.find_element(By.CSS_SELECTOR, '#toggle-nav-sidebar')
toggle_button.click()
select = Select(self.selenium.find_element_by_id('id_form-0-section'))
select = Select(self.selenium.find_element(By.ID, 'id_form-0-section'))
self.assertEqual(select.first_selected_option.text, '<i>edited section</i>')
# Rendered select2 input.
select2_display = self.selenium.find_element_by_class_name('select2-selection__rendered')
select2_display = self.selenium.find_element(By.CLASS_NAME, 'select2-selection__rendered')
# Clear button (×\n) is included in text.
self.assertEqual(select2_display.text, '×\n<i>edited section</i>')
# Add popup
self.selenium.find_element_by_id('add_id_form-0-section').click()
self.selenium.find_element(By.ID, 'add_id_form-0-section').click()
self.wait_for_and_switch_to_popup()
self.wait_for_text('#content h1', 'Add section')
self.selenium.find_element_by_id('id_name').send_keys('new section')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.ID, 'id_name').send_keys('new section')
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_form-0-section'))
select = Select(self.selenium.find_element(By.ID, 'id_form-0-section'))
self.assertEqual(select.first_selected_option.text, 'new section')
select2_display = self.selenium.find_element_by_class_name('select2-selection__rendered')
select2_display = self.selenium.find_element(By.CLASS_NAME, 'select2-selection__rendered')
# Clear button (×\n) is included in text.
self.assertEqual(select2_display.text, '×\nnew section')
def test_inline_uuid_pk_edit_with_popup(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
parent = ParentWithUUIDPK.objects.create(title='test')
related_with_parent = RelatedWithUUIDPKModel.objects.create(parent=parent)
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('change_id_parent').click()
self.selenium.find_element(By.ID, 'change_id_parent').click()
self.wait_for_and_switch_to_popup()
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_parent'))
select = Select(self.selenium.find_element(By.ID, 'id_parent'))
self.assertEqual(select.first_selected_option.text, str(parent.id))
self.assertEqual(select.first_selected_option.get_attribute('value'), str(parent.id))
def test_inline_uuid_pk_add_with_popup(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
self.selenium.get(self.live_server_url + reverse('admin:admin_views_relatedwithuuidpkmodel_add'))
self.selenium.find_element_by_id('add_id_parent').click()
self.selenium.find_element(By.ID, 'add_id_parent').click()
self.wait_for_and_switch_to_popup()
self.selenium.find_element_by_id('id_title').send_keys('test')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test')
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_parent'))
select = Select(self.selenium.find_element(By.ID, 'id_parent'))
uuid_id = str(ParentWithUUIDPK.objects.first().id)
self.assertEqual(select.first_selected_option.text, uuid_id)
self.assertEqual(select.first_selected_option.get_attribute('value'), uuid_id)
def test_inline_uuid_pk_delete_with_popup(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
parent = ParentWithUUIDPK.objects.create(title='test')
related_with_parent = RelatedWithUUIDPKModel.objects.create(parent=parent)
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('delete_id_parent').click()
self.selenium.find_element(By.ID, 'delete_id_parent').click()
self.wait_for_and_switch_to_popup()
self.selenium.find_element_by_xpath('//input[@value="Yes, Im sure"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Yes, Im sure"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_parent'))
select = Select(self.selenium.find_element(By.ID, 'id_parent'))
self.assertEqual(ParentWithUUIDPK.objects.count(), 0)
self.assertEqual(select.first_selected_option.text, '---------')
self.assertEqual(select.first_selected_option.get_attribute('value'), '')
def test_inline_with_popup_cancel_delete(self):
"""Clicking ""No, take me back" on a delete popup closes the window."""
from selenium.webdriver.common.by import By
parent = ParentWithUUIDPK.objects.create(title='test')
related_with_parent = RelatedWithUUIDPKModel.objects.create(parent=parent)
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('delete_id_parent').click()
self.selenium.find_element(By.ID, 'delete_id_parent').click()
self.wait_for_and_switch_to_popup()
self.selenium.find_element_by_xpath('//a[text()="No, take me back"]').click()
self.selenium.find_element(By.XPATH, '//a[text()="No, take me back"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
self.assertEqual(len(self.selenium.window_handles), 1)
def test_list_editable_raw_id_fields(self):
from selenium.webdriver.common.by import By
parent = ParentWithUUIDPK.objects.create(title='test')
parent2 = ParentWithUUIDPK.objects.create(title='test2')
RelatedWithUUIDPKModel.objects.create(parent=parent)
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_changelist', current_app=site2.name)
self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('lookup_id_form-0-parent').click()
self.selenium.find_element(By.ID, 'lookup_id_form-0-parent').click()
self.wait_for_and_switch_to_popup()
# Select "parent2" in the popup.
self.selenium.find_element_by_link_text(str(parent2.pk)).click()
self.selenium.find_element(By.LINK_TEXT, str(parent2.pk)).click()
self.selenium.switch_to.window(self.selenium.window_handles[0])
# The newly selected pk should appear in the raw id input.
value = self.selenium.find_element_by_id('id_form-0-parent').get_attribute('value')
value = self.selenium.find_element(By.ID, 'id_form-0-parent').get_attribute('value')
self.assertEqual(value, str(parent2.pk))
def test_input_element_font(self):
@ -4960,8 +4975,9 @@ class SeleniumTests(AdminSeleniumTestCase):
Browsers' default stylesheets override the font of inputs. The admin
adds additional CSS to handle this.
"""
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + reverse('admin:login'))
element = self.selenium.find_element_by_id('id_username')
element = self.selenium.find_element(By.ID, 'id_username')
# Some browsers quotes the fonts, some don't.
fonts = [
font.strip().strip('"')
@ -4973,13 +4989,14 @@ class SeleniumTests(AdminSeleniumTestCase):
)
def test_search_input_filtered_page(self):
from selenium.webdriver.common.by import By
Person.objects.create(name='Guido van Rossum', gender=1, alive=True)
Person.objects.create(name='Grace Hopper', gender=1, alive=False)
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
person_url = reverse('admin:admin_views_person_changelist') + '?q=Gui'
self.selenium.get(self.live_server_url + person_url)
self.assertGreater(
self.selenium.find_element_by_id('searchbar').rect['width'],
self.selenium.find_element(By.ID, 'searchbar').rect['width'],
50,
)
@ -4987,41 +5004,42 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
Create a chain of 'self' related objects via popups.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
add_url = reverse('admin:admin_views_box_add', current_app=site.name)
self.selenium.get(self.live_server_url + add_url)
base_window = self.selenium.current_window_handle
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup()
popup_window_test = self.selenium.current_window_handle
self.selenium.find_element_by_id('id_title').send_keys('test')
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test')
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup(num_windows=3)
popup_window_test2 = self.selenium.current_window_handle
self.selenium.find_element_by_id('id_title').send_keys('test2')
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test2')
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup(num_windows=4)
self.selenium.find_element_by_id('id_title').send_keys('test3')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test3')
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(popup_window_test2)
select = Select(self.selenium.find_element_by_id('id_next_box'))
select = Select(self.selenium.find_element(By.ID, 'id_next_box'))
next_box_id = str(Box.objects.get(title="test3").id)
self.assertEqual(select.first_selected_option.get_attribute('value'), next_box_id)
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(popup_window_test)
select = Select(self.selenium.find_element_by_id('id_next_box'))
select = Select(self.selenium.find_element(By.ID, 'id_next_box'))
next_box_id = str(Box.objects.get(title="test2").id)
self.assertEqual(select.first_selected_option.get_attribute('value'), next_box_id)
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.selenium.switch_to.window(base_window)
select = Select(self.selenium.find_element_by_id('id_next_box'))
select = Select(self.selenium.find_element(By.ID, 'id_next_box'))
next_box_id = str(Box.objects.get(title="test").id)
self.assertEqual(select.first_selected_option.get_attribute('value'), next_box_id)
@ -5029,32 +5047,33 @@ class SeleniumTests(AdminSeleniumTestCase):
"""
Cleanup child popups when closing a parent popup.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url=reverse('admin:index'))
add_url = reverse('admin:admin_views_box_add', current_app=site.name)
self.selenium.get(self.live_server_url + add_url)
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup()
test_window = self.selenium.current_window_handle
self.selenium.find_element_by_id('id_title').send_keys('test')
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test')
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup(num_windows=3)
test2_window = self.selenium.current_window_handle
self.selenium.find_element_by_id('id_title').send_keys('test2')
self.selenium.find_element_by_id('add_id_next_box').click()
self.selenium.find_element(By.ID, 'id_title').send_keys('test2')
self.selenium.find_element(By.ID, 'add_id_next_box').click()
self.wait_for_and_switch_to_popup(num_windows=4)
self.assertEqual(len(self.selenium.window_handles), 4)
self.selenium.switch_to.window(test2_window)
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_until(lambda d: len(d.window_handles) == 2, 1)
self.assertEqual(len(self.selenium.window_handles), 2)
# Close final popup to clean up test.
self.selenium.switch_to.window(test_window)
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
self.selenium.switch_to.window(self.selenium.window_handles[-1])

View File

@ -789,6 +789,7 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
Pressing the ESC key or clicking on a widget value closes the date and
time picker widgets.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
self.admin_login(username='super', password='secret', login_url='/')
@ -796,51 +797,51 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
# First, with the date picker widget ---------------------------------
cal_icon = self.selenium.find_element_by_id('calendarlink0')
cal_icon = self.selenium.find_element(By.ID, 'calendarlink0')
# The date picker is hidden
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
self.assertFalse(self.selenium.find_element(By.ID, 'calendarbox0').is_displayed())
# Click the calendar icon
cal_icon.click()
# The date picker is visible
self.assertTrue(self.selenium.find_element_by_id('calendarbox0').is_displayed())
self.assertTrue(self.selenium.find_element(By.ID, 'calendarbox0').is_displayed())
# Press the ESC key
self.selenium.find_element_by_tag_name('body').send_keys([Keys.ESCAPE])
self.selenium.find_element(By.TAG_NAME, 'body').send_keys([Keys.ESCAPE])
# The date picker is hidden again
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
self.assertFalse(self.selenium.find_element(By.ID, 'calendarbox0').is_displayed())
# Click the calendar icon, then on the 15th of current month
cal_icon.click()
self.selenium.find_element_by_xpath("//a[contains(text(), '15')]").click()
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
self.selenium.find_element(By.XPATH, "//a[contains(text(), '15')]").click()
self.assertFalse(self.selenium.find_element(By.ID, 'calendarbox0').is_displayed())
self.assertEqual(
self.selenium.find_element_by_id('id_birthdate_0').get_attribute('value'),
self.selenium.find_element(By.ID, 'id_birthdate_0').get_attribute('value'),
datetime.today().strftime('%Y-%m-') + '15',
)
# Then, with the time picker widget ----------------------------------
time_icon = self.selenium.find_element_by_id('clocklink0')
time_icon = self.selenium.find_element(By.ID, 'clocklink0')
# The time picker is hidden
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
self.assertFalse(self.selenium.find_element(By.ID, 'clockbox0').is_displayed())
# Click the time icon
time_icon.click()
# The time picker is visible
self.assertTrue(self.selenium.find_element_by_id('clockbox0').is_displayed())
self.assertTrue(self.selenium.find_element(By.ID, 'clockbox0').is_displayed())
self.assertEqual(
[
x.text for x in
self.selenium.find_elements_by_xpath("//ul[@class='timelist']/li/a")
self.selenium.find_elements(By.XPATH, "//ul[@class='timelist']/li/a")
],
['Now', 'Midnight', '6 a.m.', 'Noon', '6 p.m.']
)
# Press the ESC key
self.selenium.find_element_by_tag_name('body').send_keys([Keys.ESCAPE])
self.selenium.find_element(By.TAG_NAME, 'body').send_keys([Keys.ESCAPE])
# The time picker is hidden again
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
self.assertFalse(self.selenium.find_element(By.ID, 'clockbox0').is_displayed())
# Click the time icon, then select the 'Noon' value
time_icon.click()
self.selenium.find_element_by_xpath("//a[contains(text(), 'Noon')]").click()
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
self.selenium.find_element(By.XPATH, "//a[contains(text(), 'Noon')]").click()
self.assertFalse(self.selenium.find_element(By.ID, 'clockbox0').is_displayed())
self.assertEqual(
self.selenium.find_element_by_id('id_birthdate_1').get_attribute('value'),
self.selenium.find_element(By.ID, 'id_birthdate_1').get_attribute('value'),
'12:00:00',
)
@ -849,19 +850,20 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
Ensure cells that are not days of the month have the `nonday` CSS class.
Refs #4574.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
# Open a page that has a date and time picker widgets
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
# fill in the birth date.
self.selenium.find_element_by_id('id_birthdate_0').send_keys('2013-06-01')
self.selenium.find_element(By.ID, 'id_birthdate_0').send_keys('2013-06-01')
# Click the calendar icon
self.selenium.find_element_by_id('calendarlink0').click()
self.selenium.find_element(By.ID, 'calendarlink0').click()
# get all the tds within the calendar
calendar0 = self.selenium.find_element_by_id('calendarin0')
tds = calendar0.find_elements_by_tag_name('td')
calendar0 = self.selenium.find_element(By.ID, 'calendarin0')
tds = calendar0.find_elements(By.TAG_NAME, 'td')
# make sure the first and last 6 cells have class nonday
for td in tds[:6] + tds[-6:]:
@ -872,19 +874,20 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
Ensure cell for the day in the input has the `selected` CSS class.
Refs #4574.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
# Open a page that has a date and time picker widgets
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
# fill in the birth date.
self.selenium.find_element_by_id('id_birthdate_0').send_keys('2013-06-01')
self.selenium.find_element(By.ID, 'id_birthdate_0').send_keys('2013-06-01')
# Click the calendar icon
self.selenium.find_element_by_id('calendarlink0').click()
self.selenium.find_element(By.ID, 'calendarlink0').click()
# get all the tds within the calendar
calendar0 = self.selenium.find_element_by_id('calendarin0')
tds = calendar0.find_elements_by_tag_name('td')
calendar0 = self.selenium.find_element(By.ID, 'calendarin0')
tds = calendar0.find_elements(By.TAG_NAME, 'td')
# verify the selected cell
selected = tds[6]
@ -897,16 +900,17 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
Ensure no cells are given the selected class when the field is empty.
Refs #4574.
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
# Open a page that has a date and time picker widgets
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
# Click the calendar icon
self.selenium.find_element_by_id('calendarlink0').click()
self.selenium.find_element(By.ID, 'calendarlink0').click()
# get all the tds within the calendar
calendar0 = self.selenium.find_element_by_id('calendarin0')
tds = calendar0.find_elements_by_tag_name('td')
calendar0 = self.selenium.find_element(By.ID, 'calendarin0')
tds = calendar0.find_elements(By.TAG_NAME, 'td')
# verify there are no cells with the selected class
selected = [td for td in tds if td.get_attribute('class') == 'selected']
@ -918,6 +922,7 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
The calendar shows the date from the input field for every locale
supported by Django.
"""
from selenium.webdriver.common.by import By
self.selenium.set_window_size(1024, 768)
self.admin_login(username='super', password='secret', login_url='/')
@ -948,7 +953,7 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
url = reverse('admin:admin_widgets_member_change', args=(member.pk,))
self.selenium.get(self.live_server_url + url)
# Click on the calendar icon
self.selenium.find_element_by_id('calendarlink0').click()
self.selenium.find_element(By.ID, 'calendarlink0').click()
# Make sure that the right month and year are displayed
self.wait_for_text('#calendarin0 caption', expected_caption)
@ -965,6 +970,7 @@ class DateTimePickerShortcutsSeleniumTests(AdminWidgetSeleniumTestCase):
in the default time zone "America/Chicago" despite `override_settings` changing
the time zone to "Asia/Singapore".
"""
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
error_margin = timedelta(seconds=10)
@ -979,14 +985,14 @@ class DateTimePickerShortcutsSeleniumTests(AdminWidgetSeleniumTestCase):
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
self.selenium.find_element_by_id('id_name').send_keys('test')
self.selenium.find_element(By.ID, 'id_name').send_keys('test')
# Click on the "today" and "now" shortcuts.
shortcuts = self.selenium.find_elements_by_css_selector('.field-birthdate .datetimeshortcuts')
shortcuts = self.selenium.find_elements(By.CSS_SELECTOR, '.field-birthdate .datetimeshortcuts')
now = datetime.now()
for shortcut in shortcuts:
shortcut.find_element_by_tag_name('a').click()
shortcut.find_element(By.TAG_NAME, 'a').click()
# There is a time zone mismatch warning.
# Warning: This would effectively fail if the TIME_ZONE defined in the
@ -996,7 +1002,7 @@ class DateTimePickerShortcutsSeleniumTests(AdminWidgetSeleniumTestCase):
# Submit the form.
with self.wait_page_loaded():
self.selenium.find_element_by_name('_save').click()
self.selenium.find_element(By.NAME, '_save').click()
# Make sure that "now" in JavaScript is within 10 seconds
# from "now" on the server side.
@ -1038,6 +1044,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.assertEqual(self.has_css_class(remove_all_link, 'active'), remove_all)
def execute_basic_operations(self, mode, field_name):
from selenium.webdriver.common.by import By
original_url = self.selenium.current_url
from_box = '#id_%s_from' % field_name
@ -1058,13 +1065,13 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Click 'Choose all' --------------------------------------------------
if mode == 'horizontal':
self.selenium.find_element_by_id(choose_all_link).click()
self.selenium.find_element(By.ID, choose_all_link).click()
elif mode == 'vertical':
# There 's no 'Choose all' button in vertical mode, so individually
# select all options and click 'Choose'.
for option in self.selenium.find_elements_by_css_selector(from_box + ' > option'):
for option in self.selenium.find_elements(By.CSS_SELECTOR, from_box + ' > option'):
option.click()
self.selenium.find_element_by_id(choose_link).click()
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectOptions(from_box, [])
self.assertSelectOptions(to_box, [
str(self.lisa.id), str(self.peter.id),
@ -1076,13 +1083,13 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Click 'Remove all' --------------------------------------------------
if mode == 'horizontal':
self.selenium.find_element_by_id(remove_all_link).click()
self.selenium.find_element(By.ID, remove_all_link).click()
elif mode == 'vertical':
# There 's no 'Remove all' button in vertical mode, so individually
# select all options and click 'Remove'.
for option in self.selenium.find_elements_by_css_selector(to_box + ' > option'):
for option in self.selenium.find_elements(By.CSS_SELECTOR, to_box + ' > option'):
option.click()
self.selenium.find_element_by_id(remove_link).click()
self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectOptions(from_box, [
str(self.lisa.id), str(self.peter.id),
str(self.arthur.id), str(self.bob.id),
@ -1093,7 +1100,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.assertActiveButtons(mode, field_name, False, False, True, False)
# Choose some options ------------------------------------------------
from_lisa_select_option = self.selenium.find_element_by_css_selector(
from_lisa_select_option = self.selenium.find_element(
By.CSS_SELECTOR,
'{} > option[value="{}"]'.format(from_box, self.lisa.id)
)
@ -1105,7 +1113,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.select_option(from_box, str(self.bob.id))
self.select_option(from_box, str(self.john.id))
self.assertActiveButtons(mode, field_name, True, False, True, False)
self.selenium.find_element_by_id(choose_link).click()
self.selenium.find_element(By.ID, choose_link).click()
self.assertActiveButtons(mode, field_name, False, False, True, True)
self.assertSelectOptions(from_box, [
@ -1118,7 +1126,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
])
# Check the tooltip is still there after moving: ticket #20821
to_lisa_select_option = self.selenium.find_element_by_css_selector(
to_lisa_select_option = self.selenium.find_element(
By.CSS_SELECTOR,
'{} > option[value="{}"]'.format(to_box, self.lisa.id)
)
self.assertEqual(to_lisa_select_option.get_attribute('title'), to_lisa_select_option.get_attribute('text'))
@ -1127,7 +1136,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.select_option(to_box, str(self.lisa.id))
self.select_option(to_box, str(self.bob.id))
self.assertActiveButtons(mode, field_name, False, True, True, True)
self.selenium.find_element_by_id(remove_link).click()
self.selenium.find_element(By.ID, remove_link).click()
self.assertActiveButtons(mode, field_name, False, False, True, True)
self.assertSelectOptions(from_box, [
@ -1140,7 +1149,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Choose some more options --------------------------------------------
self.select_option(from_box, str(self.arthur.id))
self.select_option(from_box, str(self.cliff.id))
self.selenium.find_element_by_id(choose_link).click()
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectOptions(from_box, [
str(self.peter.id), str(self.jenny.id),
@ -1157,7 +1166,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Confirm they're selected after clicking inactive buttons: ticket #26575
self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
self.selenium.find_element_by_id(remove_link).click()
self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
# Unselect the options ------------------------------------------------
@ -1170,7 +1179,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Confirm they're selected after clicking inactive buttons: ticket #26575
self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
self.selenium.find_element_by_id(choose_link).click()
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
# Unselect the options ------------------------------------------------
@ -1181,6 +1190,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.assertEqual(self.selenium.current_url, original_url)
def test_basic(self):
from selenium.webdriver.common.by import By
self.selenium.set_window_size(1024, 768)
self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter])
@ -1193,7 +1203,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
self.execute_basic_operations('horizontal', 'alumni')
# Save and check that everything is properly stored in the database ---
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_page_ready()
self.school = School.objects.get(id=self.school.id) # Reload from database
self.assertEqual(list(self.school.students.all()), [self.arthur, self.cliff, self.jason, self.john])
@ -1204,6 +1214,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
Typing in the search box filters out options displayed in the 'from'
box.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
self.selenium.set_window_size(1024, 768)
@ -1218,7 +1229,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
to_box = '#id_%s_to' % field_name
choose_link = 'id_%s_add_link' % field_name
remove_link = 'id_%s_remove_link' % field_name
input = self.selenium.find_element_by_id('id_%s_input' % field_name)
input = self.selenium.find_element(By.ID, 'id_%s_input' % field_name)
# Initial values
self.assertSelectOptions(from_box, [
@ -1248,14 +1259,14 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
input.send_keys('a')
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.jason.id)])
self.select_option(from_box, str(self.jason.id))
self.selenium.find_element_by_id(choose_link).click()
self.selenium.find_element(By.ID, choose_link).click()
self.assertSelectOptions(from_box, [str(self.arthur.id)])
self.assertSelectOptions(to_box, [
str(self.lisa.id), str(self.peter.id), str(self.jason.id),
])
self.select_option(to_box, str(self.lisa.id))
self.selenium.find_element_by_id(remove_link).click()
self.selenium.find_element(By.ID, remove_link).click()
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.lisa.id)])
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
@ -1271,7 +1282,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Pressing enter on a filtered option sends it properly to
# the 'to' box.
self.select_option(to_box, str(self.jason.id))
self.selenium.find_element_by_id(remove_link).click()
self.selenium.find_element(By.ID, remove_link).click()
input.send_keys('ja')
self.assertSelectOptions(from_box, [str(self.jason.id)])
input.send_keys([Keys.ENTER])
@ -1280,7 +1291,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
# Save and check that everything is properly stored in the database ---
with self.wait_page_loaded():
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.school = School.objects.get(id=self.school.id) # Reload from database
self.assertEqual(list(self.school.students.all()), [self.jason, self.peter])
self.assertEqual(list(self.school.alumni.all()), [self.jason, self.peter])
@ -1291,13 +1302,14 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
and then clicking the browser's back button would clear the
filter_horizontal/filter_vertical widgets (#13614).
"""
from selenium.webdriver.common.by import By
self.school.students.set([self.lisa, self.peter])
self.school.alumni.set([self.lisa, self.peter])
self.admin_login(username='super', password='secret', login_url='/')
change_url = reverse('admin:admin_widgets_school_change', args=(self.school.id,))
self.selenium.get(self.live_server_url + change_url)
# Navigate away and go back to the change form page.
self.selenium.find_element_by_link_text('Home').click()
self.selenium.find_element(By.LINK_TEXT, 'Home').click()
self.selenium.back()
expected_unselected_values = [
str(self.arthur.id), str(self.bob.id), str(self.cliff.id),
@ -1341,17 +1353,18 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
Band.objects.create(id=98, name='Green Potatoes')
def test_ForeignKey(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_event_add'))
main_window = self.selenium.current_window_handle
# No value has been selected yet
self.assertEqual(self.selenium.find_element_by_id('id_main_band').get_attribute('value'), '')
self.assertEqual(self.selenium.find_element(By.ID, 'id_main_band').get_attribute('value'), '')
# Open the popup window and click on a band
self.selenium.find_element_by_id('lookup_id_main_band').click()
self.selenium.find_element(By.ID, 'lookup_id_main_band').click()
self.wait_for_and_switch_to_popup()
link = self.selenium.find_element_by_link_text('Bogey Blues')
link = self.selenium.find_element(By.LINK_TEXT, 'Bogey Blues')
self.assertIn('/band/42/', link.get_attribute('href'))
link.click()
@ -1360,9 +1373,9 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
self.wait_for_value('#id_main_band', '42')
# Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_main_band').click()
self.selenium.find_element(By.ID, 'lookup_id_main_band').click()
self.wait_for_and_switch_to_popup()
link = self.selenium.find_element_by_link_text('Green Potatoes')
link = self.selenium.find_element(By.LINK_TEXT, 'Green Potatoes')
self.assertIn('/band/98/', link.get_attribute('href'))
link.click()
@ -1371,23 +1384,24 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
self.wait_for_value('#id_main_band', '98')
def test_many_to_many(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_event_add'))
main_window = self.selenium.current_window_handle
# No value has been selected yet
self.assertEqual(self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'), '')
self.assertEqual(self.selenium.find_element(By.ID, 'id_supporting_bands').get_attribute('value'), '')
# Help text for the field is displayed
self.assertEqual(
self.selenium.find_element_by_css_selector('.field-supporting_bands div.help').text,
self.selenium.find_element(By.CSS_SELECTOR, '.field-supporting_bands div.help').text,
'Supporting Bands.'
)
# Open the popup window and click on a band
self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
self.selenium.find_element(By.ID, 'lookup_id_supporting_bands').click()
self.wait_for_and_switch_to_popup()
link = self.selenium.find_element_by_link_text('Bogey Blues')
link = self.selenium.find_element(By.LINK_TEXT, 'Bogey Blues')
self.assertIn('/band/42/', link.get_attribute('href'))
link.click()
@ -1396,9 +1410,9 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
self.wait_for_value('#id_supporting_bands', '42')
# Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
self.selenium.find_element(By.ID, 'lookup_id_supporting_bands').click()
self.wait_for_and_switch_to_popup()
link = self.selenium.find_element_by_link_text('Green Potatoes')
link = self.selenium.find_element(By.LINK_TEXT, 'Green Potatoes')
self.assertIn('/band/98/', link.get_attribute('href'))
link.click()
@ -1410,42 +1424,43 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
def test_ForeignKey_using_to_field(self):
from selenium.webdriver.common.by import By
self.admin_login(username='super', password='secret', login_url='/')
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_profile_add'))
main_window = self.selenium.current_window_handle
# Click the Add User button to add new
self.selenium.find_element_by_id('add_id_user').click()
self.selenium.find_element(By.ID, 'add_id_user').click()
self.wait_for_and_switch_to_popup()
password_field = self.selenium.find_element_by_id('id_password')
password_field = self.selenium.find_element(By.ID, 'id_password')
password_field.send_keys('password')
username_field = self.selenium.find_element_by_id('id_username')
username_field = self.selenium.find_element(By.ID, 'id_username')
username_value = 'newuser'
username_field.send_keys(username_value)
save_button_css_selector = '.submit-row > input[type=submit]'
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
self.selenium.find_element(By.CSS_SELECTOR, save_button_css_selector).click()
self.selenium.switch_to.window(main_window)
# The field now contains the new user
self.selenium.find_element_by_css_selector('#id_user option[value=newuser]')
self.selenium.find_element(By.CSS_SELECTOR, '#id_user option[value=newuser]')
# Click the Change User button to change it
self.selenium.find_element_by_id('change_id_user').click()
self.selenium.find_element(By.ID, 'change_id_user').click()
self.wait_for_and_switch_to_popup()
username_field = self.selenium.find_element_by_id('id_username')
username_field = self.selenium.find_element(By.ID, 'id_username')
username_value = 'changednewuser'
username_field.clear()
username_field.send_keys(username_value)
save_button_css_selector = '.submit-row > input[type=submit]'
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
self.selenium.find_element(By.CSS_SELECTOR, save_button_css_selector).click()
self.selenium.switch_to.window(main_window)
self.selenium.find_element_by_css_selector('#id_user option[value=changednewuser]')
self.selenium.find_element(By.CSS_SELECTOR, '#id_user option[value=changednewuser]')
# Go ahead and submit the form to make sure it works
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
self.selenium.find_element(By.CSS_SELECTOR, save_button_css_selector).click()
self.wait_for_text('li.success', 'The profile “changednewuser” was added successfully.')
profiles = Profile.objects.all()
self.assertEqual(len(profiles), 1)

View File

@ -14,8 +14,9 @@ class LiveWidgetTests(AdminSeleniumTestCase):
"""
A roundtrip on a ModelForm doesn't alter the TextField value
"""
from selenium.webdriver.common.by import By
article = Article.objects.create(content="\nTst\n")
self.selenium.get(self.live_server_url + reverse('article_form', args=[article.pk]))
self.selenium.find_element_by_id('submit').click()
self.selenium.find_element(By.ID, 'submit').click()
article = Article.objects.get(pk=article.pk)
self.assertEqual(article.content, "\r\nTst\r\n")

View File

@ -438,23 +438,23 @@ class I18nSeleniumTests(SeleniumTestCase):
@override_settings(LANGUAGE_CODE='de')
def test_javascript_gettext(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + '/jsi18n_template/')
elem = self.selenium.find_element_by_id("gettext")
elem = self.selenium.find_element(By.ID, "gettext")
self.assertEqual(elem.text, "Entfernen")
elem = self.selenium.find_element_by_id("ngettext_sing")
elem = self.selenium.find_element(By.ID, "ngettext_sing")
self.assertEqual(elem.text, "1 Element")
elem = self.selenium.find_element_by_id("ngettext_plur")
elem = self.selenium.find_element(By.ID, "ngettext_plur")
self.assertEqual(elem.text, "455 Elemente")
elem = self.selenium.find_element_by_id("ngettext_onnonplural")
elem = self.selenium.find_element(By.ID, "ngettext_onnonplural")
self.assertEqual(elem.text, "Bild")
elem = self.selenium.find_element_by_id("pgettext")
elem = self.selenium.find_element(By.ID, "pgettext")
self.assertEqual(elem.text, "Kann")
elem = self.selenium.find_element_by_id("npgettext_sing")
elem = self.selenium.find_element(By.ID, "npgettext_sing")
self.assertEqual(elem.text, "1 Resultat")
elem = self.selenium.find_element_by_id("npgettext_plur")
elem = self.selenium.find_element(By.ID, "npgettext_plur")
self.assertEqual(elem.text, "455 Resultate")
elem = self.selenium.find_element_by_id("formats")
elem = self.selenium.find_element(By.ID, "formats")
self.assertEqual(
elem.text,
"DATE_INPUT_FORMATS is an object; DECIMAL_SEPARATOR is a string; FIRST_DAY_OF_WEEK is a number;"
@ -463,9 +463,9 @@ class I18nSeleniumTests(SeleniumTestCase):
@modify_settings(INSTALLED_APPS={'append': ['view_tests.app1', 'view_tests.app2']})
@override_settings(LANGUAGE_CODE='fr')
def test_multiple_catalogs(self):
from selenium.webdriver.common.by import By
self.selenium.get(self.live_server_url + '/jsi18n_multi_catalogs/')
elem = self.selenium.find_element_by_id('app1string')
elem = self.selenium.find_element(By.ID, 'app1string')
self.assertEqual(elem.text, 'il faut traduire cette chaîne de caractères de app1')
elem = self.selenium.find_element_by_id('app2string')
elem = self.selenium.find_element(By.ID, 'app2string')
self.assertEqual(elem.text, 'il faut traduire cette chaîne de caractères de app2')