Fakultas Ilmu Komputer UI

Commit 26636921 authored by Izzatul Muttaqin's avatar Izzatul Muttaqin
Browse files

Change tests

parent 17a78714
Pipeline #28142 passed with stages
in 5 minutes and 6 seconds
stages:
- test
- deploy
- functional_test
- deploy
UnitTest:
image: python:3.6
......@@ -20,7 +20,7 @@ UnitTest:
TestFunctional:
image: python:3.6
stage: test_functional
stage: functional_test
before_script:
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
......
import sys
import time
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
from django.test import LiveServerTestCase
import time
import unittest
import environ
MAX_WAIT = 5
MAX_WAIT = 10
def wait(fn):
def modified_fn(*args, **kwargs):
start_time = time.time()
while True:
try:
return fn(*args, **kwargs)
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
return modified_fn
class FunctionalTest(StaticLiveServerTestCase):
class NewVisitorTest(LiveServerTestCase):
@classmethod
def setUpClass(cls):
for arg in sys.argv:
if 'liveserver' in arg:
cls.server_url = 'http://' + arg.split('=')[1]
return
super().setUpClass()
cls.server_url = cls.live_server_url
def setUp(self):
self.browser = webdriver.Chrome()
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
try:
self.browser = webdriver.Chrome('./chromedriver.exe', chrome_options=chrome_options)
except:
self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
def tearDown(self):
self.browser.quit()
def check_for_row(self, row_text):
def check_for_row_in_list_table(self, row_text):
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])
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get(self.live_server_url)
def get_item_input_box(self):
return self.browser.find_element_by_id('id_new_item')
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)
introduction = self.browser.find_element_by_id("intro")
self.assertIn('Izzatul', introduction.text)
comment = self.browser.find_element_by_id('comment')
self.assertEqual(comment.text, 'yey, waktunya libur')
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertEqual(
inputbox.get_attribute('placeholder'),
'Enter a to-do item'
)
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER)
WebDriverWait(self.browser, 10).until(
expected_conditions.text_to_be_present_in_element(
(By.ID, 'id_list_table'), 'Buy peacock feathers'))
self.check_for_row('1: Buy peacock feathers')
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly')
inputbox.send_keys(Keys.ENTER)
@wait
def wait_for(self, fn):
return fn()
@wait
def wait_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.check_for_row('1: Buy peacock feathers')
WebDriverWait(self.browser, 10).until(
expected_conditions.text_to_be_present_in_element(
(By.ID, 'id_list_table'), 'Use peacock feathers to make a fly'))
self.check_for_row('2: Use peacock feathers to make a fly')
# self.fail('Finish the test!')
self.assertIn(row_text, [row.text for row in rows])
if __name__ == '__main__':
unittest.main(warnings='ignore')
@wait
def wait_to_be_logged_in(self, email):
self.browser.find_element_by_link_text('Log out')
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(email, navbar.text)
@wait
def wait_to_be_logged_out(self, email):
self.browser.find_element_by_name('email')
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertNotIn(email, navbar.text)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment