-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add selenium test for admin list page
Part of #30
- Loading branch information
Showing
16 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.contrib import admin | ||
|
||
from orderable.admin import OrderableAdmin | ||
from .models import Item | ||
|
||
|
||
admin.site.register(Item, OrderableAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 2.1.2 on 2018-10-26 21:17 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Item', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('sort_order', models.IntegerField(blank=True, db_index=True)), | ||
], | ||
options={ | ||
'ordering': ['sort_order'], | ||
'abstract': False, | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from orderable.models import Orderable | ||
|
||
|
||
class Item(Orderable): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
|
||
|
||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
SECRET_KEY = 'r+n-oywnjf7&b*#(!@zx-wa4j=4_yy7a%j-lep&q8nx6^=lwf5' | ||
ROOT_URLCONF = 'bdd.urls' | ||
STATIC_URL = '/static/' | ||
DATABASES = {'default': { | ||
'ENGINE': 'django.db.backends.postgresql_psycopg2', | ||
'NAME': 'orderable', | ||
'HOST': 'localhost' | ||
}} | ||
|
||
INSTALLED_APPS = [ | ||
# Project. | ||
'bdd', | ||
|
||
# 3rd party. | ||
'behave_django', | ||
'orderable', | ||
|
||
# Django. | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
] | ||
|
||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.conf.urls import url | ||
from django.contrib import admin | ||
|
||
|
||
urlpatterns = [ | ||
url('^admin/', admin.site.urls), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == '__main__': | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bdd.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) | ||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from selenium import webdriver | ||
|
||
|
||
class Browser(object): | ||
driver = webdriver.Chrome() | ||
driver.implicitly_wait(5) | ||
|
||
def close(context): | ||
context.driver.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from .browser import Browser | ||
from .pages import ItemListPage | ||
|
||
|
||
def before_all(context): | ||
context.browser = Browser() | ||
context.item_list_page = ItemListPage() | ||
|
||
|
||
def after_all(context): | ||
context.browser.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Feature: Ordering on the list page | ||
|
||
Scenario: Move an item to the end of the list | ||
Given the following items: | ||
| pk | | ||
| 1 | | ||
| 2 | | ||
| 3 | | ||
And we are on the item list page | ||
When item 1 is moved to position 3 | ||
Then the items should be ordered thus: | ||
| pk | | ||
| 2 | | ||
| 3 | | ||
| 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from browser import Browser | ||
from selenium.webdriver.common.action_chains import ActionChains | ||
|
||
|
||
class ItemListPage(Browser): | ||
def move_item(self, source, destination): | ||
|
||
template = '#neworder-{} .ui-sortable-handle' | ||
find = self.driver.find_element_by_css_selector | ||
offset = 2 if destination > source else -2 | ||
( | ||
ActionChains(self.driver) | ||
.click_and_hold(find(template.format(source))) | ||
.move_to_element(find(template.format(destination))) | ||
.move_by_offset(0, offset) | ||
.release() | ||
.perform() | ||
) | ||
|
||
def open(self, context): | ||
url = context.get_url('admin:bdd_item_changelist') | ||
self.driver.get(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import time | ||
|
||
from behave import given, then, when | ||
from django.contrib.auth.models import User | ||
from seleniumlogin import force_login | ||
|
||
from bdd.models import Item | ||
|
||
|
||
@given(u'the following items') | ||
def set_up_items(context): | ||
Item.objects.bulk_create([ | ||
Item(pk=row['pk'], sort_order=i) | ||
for i, row | ||
in enumerate(context.table) | ||
]) | ||
|
||
|
||
@given(u'we are on the item list page') | ||
def go_to_admin_page(context): | ||
user = User.objects.create_superuser( | ||
email='[email protected]', | ||
password='password', | ||
username='myuser', | ||
) | ||
force_login(user, context.browser.driver, context.test.live_server_url) | ||
context.item_list_page.open(context) | ||
|
||
|
||
@when(u'item {initial_position:d} is moved to position {new_position:d}') | ||
def move_items(context, initial_position, new_position): | ||
context.item_list_page.move_item(initial_position, new_position) | ||
time.sleep(.1) | ||
|
||
|
||
@then(u'the items should be ordered thus') | ||
def check_item_order(context): | ||
items = list(Item.objects.values_list('pk', flat=True)) | ||
expected = [] | ||
for row in context.table: | ||
expected.append(int(row['pk'])) | ||
|
||
assert items == expected, (items, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
behave==1.2.6 | ||
behave_django==1.1.0 | ||
coverage==3.7.1 | ||
django-selenium-login==1.0.2 | ||
flake8==3.6.0 | ||
flake8-import-order==0.18 | ||
hypothesis==2.0.0 | ||
psycopg2==2.7.4 | ||
selenium==3.14.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters