Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Collection of Practice
2019
1506722720-practice
Commits
35061fd9
Commit
35061fd9
authored
Oct 17, 2019
by
Dwi Nanda Susanto
Browse files
Testinggoat/ch12
parent
ce0ddd0f
Changes
3
Hide whitespace changes
Inline
Side-by-side
functional_test/base.py
0 → 100644
View file @
35061fd9
from
django.contrib.staticfiles.testing
import
StaticLiveServerTestCase
from
selenium
import
webdriver
from
selenium.common.exceptions
import
WebDriverException
MAX_WAIT
=
10
import
time
class
FunctionalTest
(
StaticLiveServerTestCase
):
def
setUp
(
self
):
self
.
browser
=
webdriver
.
Chrome
(
'C:/chromedriver.exe'
)
def
tearDown
(
self
):
self
.
browser
.
quit
()
def
wait_for
(
self
,
fn
):
start_time
=
time
.
time
()
while
True
:
try
:
return
fn
()
except
(
AssertionError
,
WebDriverException
)
as
e
:
if
time
.
time
()
-
start_time
>
MAX_WAIT
:
raise
e
time
.
sleep
(
0.5
)
def
wait_for_row_in_list_table
(
self
,
row_text
):
start_time
=
time
.
time
()
while
True
:
try
:
table
=
self
.
browser
.
find_element_by_id
(
'id_list_table'
)
rows
=
table
.
find_elements_by_tag_name
(
'tr'
)
self
.
assertIn
(
row_text
,
[
row
.
text
for
row
in
rows
])
return
except
(
AssertionError
,
WebDriverException
)
as
e
:
if
time
.
time
()
-
start_time
>
MAX_WAIT
:
raise
e
time
.
sleep
(
0.5
)
functional_test/test_layout_and_styling.py
0 → 100644
View file @
35061fd9
from
.base
import
FunctionalTest
from
selenium.webdriver.common.keys
import
Keys
class
LayoutAndStylingTest
(
FunctionalTest
):
def
test_layout_and_styling
(
self
):
# Edith goes to the home page
self
.
browser
.
get
(
self
.
live_server_url
)
self
.
browser
.
set_window_size
(
1024
,
768
)
# She notices the input box is nicely centered
inputbox
=
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
inputbox
.
send_keys
(
'testing'
)
inputbox
.
send_keys
(
Keys
.
ENTER
)
self
.
wait_for_row_in_list_table
(
'1: testing'
)
self
.
assertAlmostEqual
(
inputbox
.
location
[
'x'
]
+
inputbox
.
size
[
'width'
]
/
2
,
512
,
delta
=
10
)
functional_test/test_list_item_validation.py
0 → 100644
View file @
35061fd9
from
.base
import
FunctionalTest
from
selenium.webdriver.common.keys
import
Keys
class
ItemValidationTest
(
FunctionalTest
):
def
test_cannot_add_empty_list_items
(
self
):
# Edith goes to the home page and accidentally tries to submit
# an empty list item. She hits Enter on the empty input box
self
.
browser
.
get
(
self
.
live_server_url
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
# The home page refreshes, and there is an error message saying
# that list items cannot be blank
self
.
wait_for
(
lambda
:
self
.
assertEqual
(
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
).
text
,
"You can't have an empty list item"
))
# She tries again with some text for the item, which now works
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
'Buy milk'
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
self
.
wait_for_row_in_list_table
(
'1: Buy milk'
)
# Perversely, she now decides to submit a second blank list item
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
# She receives a similar warning on the list page
self
.
wait_for
(
lambda
:
self
.
assertEqual
(
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
).
text
,
"You can't have an empty list item"
))
# And she can correct it by filling some text in
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
'Make tea'
)
self
.
browser
.
find_element_by_id
(
'id_new_item'
).
send_keys
(
Keys
.
ENTER
)
self
.
wait_for_row_in_list_table
(
'1: Buy milk'
)
self
.
wait_for_row_in_list_table
(
'2: Make tea'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment