class TestErrorHandling(TestCommon):
    """
    Setup a session and test that e-Obs error handling works
    """
    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.patient_list_page.go_to_patient_list()

    def test_news_error(self):
        """
        Test that entering incorrect data into a NEWS ob will cause an error
        """
        incorrect_score = INCORRECT_EWS_DATA

        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)
        PatientPage(self.driver).open_form(OPEN_OBS_MENU_NEWS_ITEM)
        PatientPage(self.driver).enter_obs_data(incorrect_score)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(TASK_FORM_INVALID_SUBMIT)
        )

        response = self.driver.find_element(
            *TASK_FORM_INVALID_SUBMIT).is_displayed()

        self.assertEqual(response, True,
                         'Incorrect error handling on NEWS form')

    def test_barcode_error(self):
        """
        Test that entering incorrect data into a barcode scan will
        cause an error
        """
        no_patient_id = self.patient_list_page.patient_scan_helper(99)
        self.patient_list_page.do_barcode_scan(
            no_patient_id['other_identifier'])

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(SERVER_ERROR)
        )

        response = self.driver.find_element(
            *SERVER_ERROR).is_displayed()

        self.assertEqual(response, True,
                         'Incorrect error handling on barcode scan')
class TestErrorHandling(TestCommon):
    """
    Setup a session and test that e-Obs error handling works
    """
    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.patient_list_page.go_to_patient_list()

    def test_news_error(self):
        """
        Test that entering incorrect data into a NEWS ob will cause an error
        """
        incorrect_score = INCORRECT_EWS_DATA

        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)
        PatientPage(self.driver).open_form(OPEN_OBS_MENU_NEWS_ITEM)
        PatientPage(self.driver).enter_obs_data(incorrect_score)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(TASK_FORM_INVALID_SUBMIT)
        )

        response = self.driver.find_element(
            *TASK_FORM_INVALID_SUBMIT).is_displayed()

        self.assertEqual(response, True,
                         'Incorrect error handling on NEWS form')

    def test_barcode_error(self):
        """
        Test that entering incorrect data into a barcode scan will
        cause an error
        """
        no_patient_id = self.patient_list_page.patient_scan_helper(99)
        self.patient_list_page.do_barcode_scan(
            no_patient_id['other_identifier'])

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(SERVER_ERROR)
        )

        response = self.driver.find_element(
            *SERVER_ERROR).is_displayed()

        self.assertEqual(response, True,
                         'Incorrect error handling on barcode scan')
class TestPatientListPage(TestCommon):
    """
    Setup a session and test that the task list page works correctly
    """

    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.patient_list_page.go_to_patient_list()

    def test_can_logout(self):
        """
        Test that the title of the login page is Open-eObs
        """
        self.patient_list_page.logout()
        self.assertTrue(PageConfirm(self.driver).is_login_page(),
                        'Did not get to the logout page correctly')

    def test_can_go_task_list_page(self):
        """
        Test that can go to task list page
        """
        self.patient_list_page.go_to_task_list()
        self.assertTrue(PageConfirm(self.driver).is_task_list_page(),
                        'Did not get to the task list page correctly')

    def test_patient_list_page(self):
        """
        Test that can go to the patient list page
        """
        self.patient_list_page.go_to_patient_list()
        self.assertTrue(PageConfirm(self.driver).is_patient_list_page(),
                        'Did not get to patient list page correctly')

    def test_can_go_stand_in_page(self):
        """
        Test that can navigate to the stand in page
        """
        self.patient_list_page.go_to_standin()
        self.assertTrue(PageConfirm(self.driver).is_stand_in_page(),
                        'Did not get to stand in page correctly')

    def test_can_do_barcode_scan(self):
        """
        Test that can do a barcode scan
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        patient_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        id_to_use = self.patient_list_page.patient_scan_helper(int(patient_id))
        self.patient_list_page.do_barcode_scan(id_to_use['other_identifier'])

    def test_view_patient_details(self):
        """
        Test that clicking on a work item tasks user to carry out the task
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        patient_url = patient_to_test.get_attribute('href')
        patient_to_test.click()
        self.assertTrue(PageConfirm(self.driver).is_patient_page(),
                        'Did not get to patient page correctly')
        self.assertEqual(self.driver.current_url, patient_url,
                         'Incorrect url')

    def test_patient_name_in_list(self):
        """
        Test that the patient name is in the list item
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        name_to_use = task_data['full_name']
        patient_name = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_NAME
        )
        self.assertEqual(patient_name.text, name_to_use.strip(),
                         'Incorrect name')

    def test_patient_location_in_list(self):
        """
        Test that the patient name is in the list item
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        location = task_data['location']
        parent_location = task_data['parent_location']
        bed_to_use = '{0}, {1}'.format(location, parent_location)
        patient_location = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_LOCATION
        )
        self.assertEqual(bed_to_use, patient_location.text,
                         'Incorrect location')

    def test_score_and_trend_in_list(self):
        """
        Test that the score and trend are present in list item
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        score = task_data['ews_score']
        trend = task_data['ews_trend']
        score_str = '({0} )'.format(score)
        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )
        trend_str = 'icon-{0}-arrow'.format(trend)
        self.assertEqual(patient_trend.get_attribute('class'), trend_str,
                         'Incorrect trend')
        self.assertIn(score_str, patient_to_test.text, 'Incorrect score')

    def test_down_trend(self):
        """
        Test that the trend is lowered after submitting a high risk ob,
        followed by a low risk ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_high_risk_observation(int(task_id))
        PatientPage(self.driver).add_low_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'),
                         'icon-down-arrow',
                         'Incorrect trend')

    def test_up_trend(self):
        """
        Test that the trend is increased after submitting a low risk ob,
        followed by a high risk ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_low_risk_observation(int(task_id))
        PatientPage(self.driver).add_high_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'), 'icon-up-arrow',
                         'Incorrect trend')

    def test_same_trend(self):
        """
        Test that the trend stays the same after submitting two of the same ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_medium_risk_observation(int(task_id))
        PatientPage(self.driver).add_medium_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'),
                         'icon-same-arrow',
                         'Incorrect trend')

    def test_task_deadline_in_list(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        deadline = task_data['next_ews_time']
        task_deadline = self.driver.find_element(
            *list_page_locators.LIST_ITEM_DEADLINE
        )
        self.assertEqual(deadline, task_deadline.text,
                         'Incorrect deadline')

    def test_shows_patients(self):
        """
        Test that the patient list shows patients for the user
        """
        patient_list = []
        for patient in self.patient_list_page.get_list_items():
            patient_list.append(patient)

        self.assertNotEquals(patient_list, [],
                             'Patient list not showing patients')

    def test_shows_patient_data(self):
        """
        Test that a patient record shows data (graph/table) for the patient
        """
        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)

        ui.WebDriverWait(self.driver, 1).until(
            ec.visibility_of_element_located(patient_page_locators.GRAPH_CHART)
        )

        patient_graph = self.driver.find_element(*
                                                 patient_page_locators
                                                 .GRAPH_CHART)

        self.assertEqual(patient_graph.is_displayed(), True, 'Graph not found')

        self.driver.find_element(*patient_page_locators
                                 .TABLE_TAB_BUTTON).click()

        ui.WebDriverWait(self.driver, 1).until(
            ec.visibility_of_element_located
            (patient_page_locators.TABLE_CONTAINER_TABLE))

        patient_table = self.driver.find_element(*
                                                 patient_page_locators
                                                 .TABLE_CONTAINER_TABLE)
        self.assertEqual(patient_table.is_displayed(), True, 'Table not found')
class TestTaskListPage(TestCommon):
    """
    Setup a session and test that the task list page works correctly
    """

    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.task_list_page = ListPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.task_list_page.go_to_task_list()

    def test_can_logout(self):
        """
        Test that the title of the login page is Open-eObs
        """
        self.task_list_page.logout()
        self.assertTrue(PageConfirm(self.driver).is_login_page(),
                        'Did not get to the logout page correctly')

    def test_can_go_to_task_list_page(self):
        """
        Test that can go to task list page
        """
        self.task_list_page.go_to_task_list()
        self.assertTrue(PageConfirm(self.driver).is_task_list_page(),
                        'Did not get to the task list page correctly')

    def test_go_to_patient_list_page(self):
        """
        Test that can go to the patient list page
        """
        self.task_list_page.go_to_patient_list()
        self.assertTrue(PageConfirm(self.driver).is_patient_list_page(),
                        'Did not get to patient list page correctly')

    def test_can_go_to_stand_in_page(self):
        """
        Test that can navigate to the stand in page
        """
        self.task_list_page.go_to_standin()
        self.assertTrue(PageConfirm(self.driver).is_stand_in_page(),
                        'Did not get to stand in page correctly')

    def test_can_carry_out_barcode_scan(self):
        """
        Test that can do a barcode scan
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        id_to_use = self.task_list_page.task_scan_helper(task_id)
        self.task_list_page.do_barcode_scan(id_to_use['other_identifier'])

    def test_click_list_item(self):
        """
        Test that clicking on a work item tasks user to carry out the task
        """
        tasks = self.task_list_page.get_list_items()
        task_to_test = tasks[0]
        task_url = task_to_test.get_attribute('href')
        task_to_test.click()
        self.assertTrue(PageConfirm(self.driver).is_task_page(),
                        'Did not get to task page correctly')
        self.assertEqual(self.driver.current_url, task_url,
                         'Incorrect url')

    def test_list_item_patient_name(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        name_to_use = task_data['full_name']
        patient_name = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_NAME
        )
        self.assertEqual(patient_name.text, name_to_use.strip(),
                         'Incorrect name')

    def test_list_item_patient_location(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        location = task_data['location']
        parent_location = task_data['parent_location']
        bed_to_use = '{0}, {1}'.format(location, parent_location)
        patient_location = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_LOCATION
        )
        self.assertEqual(bed_to_use, patient_location.text,
                         'Incorrect location')

    def test_list_item_score_trend(self):
        """
        Test that the score and trend are present in list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        score = task_data['ews_score']
        trend = task_data['ews_trend']
        score_str = '({0} )'.format(score)
        patient_trend = self.driver.find_elements(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )
        for item in patient_trend:
            if item.get_attribute('class') == 'icon-alert':
                patient_trend.remove(item)

        trend_str = 'icon-{0}-arrow'.format(trend)
        self.assertEqual(patient_trend[0].get_attribute('class'), trend_str,
                         'Incorrect trend')
        self.assertIn(score_str, patient_to_test.text, 'Incorrect score')

    def test_list_item_task_deadline(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        deadline = task_data['deadline_time']
        task_deadline = self.driver.find_element(
            *list_page_locators.LIST_ITEM_DEADLINE
        )
        self.assertEqual(deadline, task_deadline.text,
                         'Incorrect deadline')

    def test_list_item_task_summary(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        summary = task_data['summary']
        task_summary = self.driver.find_element(
            *list_page_locators.LIST_ITEM_TITLE
        )
        self.assertEqual(summary, task_summary.text,
                         'Incorrect summary')

    def test_shows_tasks(self):
        """
        Test that the task list shows tasks for the user
        """
        task_list = []
        for patient in self.task_list_page.get_list_items():
            task_list.append(patient)

        self.assertNotEquals(task_list, [], 'Task list not showing tasks')

    def test_take_task_news_obs(self):
        """
        Submit adhoc observation which creates News observation task
        and Take task NEWS Observation from task list, submit news score
        """

        # Enter high risk observation to create News Observation task in task
        # list
        self.patient_list_page.go_to_patient_list()
        high_score = HIGH_RISK_SCORE_9_EWS_DATA
        no_risk = NO_RISK_EWS_DATA
        news_task = []
        success = 'Successfully Submitted NEWS Observation'

        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)
        PatientPage(self.driver).open_form(OPEN_OBS_MENU_NEWS_ITEM)
        PatientPage(self.driver).enter_obs_data(high_score)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(CONFIRM_SUBMIT)
        )
        self.driver.find_element(*CONFIRM_SUBMIT).click()

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located((
                SUCCESSFUL_SUBMIT))
        )
        response = self.driver.find_element(*SUCCESSFUL_SUBMIT)
        self.assertEqual(success, response.text,
                         'NEWS observation unsuccessful')

        self.driver.find_element(*GO_TO_MY_TASK).click()

        # Click on the first news score task from Task list
        self.task_list_page.go_to_task_list()

        self.driver.refresh()
        for task in self.task_list_page.get_list_task():
            # print(task.text)
            if task.text == 'NEWS Observation':
                news_task.append(task)
        news_task[0].click()

        # enter low risk observation
        PatientPage(self.driver).enter_obs_data(no_risk)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(CONFIRM_SUBMIT)
        )

        self.driver.find_element(*CONFIRM_SUBMIT).click()

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located((
                SUCCESSFUL_SUBMIT))
        )
        response = self.driver.find_element(*SUCCESSFUL_SUBMIT)
        self.assertEqual(success, response.text,
                         'NEWS observation unsuccessful')
        # print(news_task)

    def test_confirm_clinical(self):
        """
        Test that a clinical notification can be confirmed
        """
        self.patient_list_page.go_to_patient_list()
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        PatientPage(self.driver).remove_observations_for_patient(int(task_id))
        TaskPage(self.driver).open_clinical(task_id, self.patient_list_page)

        success = 'Submission successful'
        response = TaskPage(self.driver).confirm_clinical()
        self.assertEqual(success, response, 'Error confirming clinical')

    def test_cancel_clinical(self):
        """
        Test that a clinical notification can be cancelled
        """
        self.patient_list_page.go_to_patient_list()
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        PatientPage(self.driver).remove_observations_for_patient(int(task_id))
        TaskPage(self.driver).open_clinical(task_id, self.patient_list_page)

        success = 'Cancellation successful'
        response = TaskPage(self.driver).cancel_clinical()
        self.assertEqual(success, response, 'Error cancelling clinical')
class TestTaskListPage(TestCommon):
    """
    Setup a session and test that the task list page works correctly
    """

    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.task_list_page = ListPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.task_list_page.go_to_task_list()

    def test_can_logout(self):
        """
        Test that the title of the login page is Open-eObs
        """
        self.task_list_page.logout()
        self.assertTrue(PageConfirm(self.driver).is_login_page(),
                        'Did not get to the logout page correctly')

    def test_can_go_to_task_list_page(self):
        """
        Test that can go to task list page
        """
        self.task_list_page.go_to_task_list()
        self.assertTrue(PageConfirm(self.driver).is_task_list_page(),
                        'Did not get to the task list page correctly')

    def test_go_to_patient_list_page(self):
        """
        Test that can go to the patient list page
        """
        self.task_list_page.go_to_patient_list()
        self.assertTrue(PageConfirm(self.driver).is_patient_list_page(),
                        'Did not get to patient list page correctly')

    def test_can_go_to_stand_in_page(self):
        """
        Test that can navigate to the stand in page
        """
        self.task_list_page.go_to_standin()
        self.assertTrue(PageConfirm(self.driver).is_stand_in_page(),
                        'Did not get to stand in page correctly')

    def test_can_carry_out_barcode_scan(self):
        """
        Test that can do a barcode scan
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        id_to_use = self.task_list_page.task_scan_helper(task_id)
        self.task_list_page.do_barcode_scan(id_to_use['other_identifier'])

    def test_click_list_item(self):
        """
        Test that clicking on a work item tasks user to carry out the task
        """
        tasks = self.task_list_page.get_list_items()
        task_to_test = tasks[0]
        task_url = task_to_test.get_attribute('href')
        task_to_test.click()
        self.assertTrue(PageConfirm(self.driver).is_task_page(),
                        'Did not get to task page correctly')
        self.assertEqual(self.driver.current_url, task_url,
                         'Incorrect url')

    def test_list_item_patient_name(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        name_to_use = task_data['full_name']
        patient_name = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_NAME
        )
        self.assertEqual(patient_name.text, name_to_use.strip(),
                         'Incorrect name')

    def test_list_item_patient_location(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        location = task_data['location']
        parent_location = task_data['parent_location']
        bed_to_use = '{0}, {1}'.format(location, parent_location)
        patient_location = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_LOCATION
        )
        self.assertEqual(bed_to_use, patient_location.text,
                         'Incorrect location')

    def test_list_item_score_trend(self):
        """
        Test that the score and trend are present in list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        score = task_data['ews_score']
        trend = task_data['ews_trend']
        score_str = '({0} )'.format(score)
        patient_trend = self.driver.find_elements(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )
        for item in patient_trend:
            if item.get_attribute('class') == 'icon-alert':
                patient_trend.remove(item)

        trend_str = 'icon-{0}-arrow'.format(trend)
        self.assertEqual(patient_trend[0].get_attribute('class'), trend_str,
                         'Incorrect trend')
        self.assertIn(score_str, patient_to_test.text, 'Incorrect score')

    def test_list_item_task_deadline(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        deadline = task_data['deadline_time']
        task_deadline = self.driver.find_element(
            *list_page_locators.LIST_ITEM_DEADLINE
        )
        self.assertEqual(deadline, task_deadline.text,
                         'Incorrect deadline')

    def test_list_item_task_summary(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.task_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            TASK_PAGE, ''
        )
        task_data = self.task_list_page.task_helper(task_id)[0]
        summary = task_data['summary']
        task_summary = self.driver.find_element(
            *list_page_locators.LIST_ITEM_TITLE
        )
        self.assertEqual(summary, task_summary.text,
                         'Incorrect summary')

    def test_shows_tasks(self):
        """
        Test that the task list shows tasks for the user
        """
        task_list = []
        for patient in self.task_list_page.get_list_items():
            task_list.append(patient)

        self.assertNotEquals(task_list, [], 'Task list not showing tasks')

    def test_take_task_news_obs(self):
        """
        Submit adhoc observation which creates News observation task
        and Take task NEWS Observation from task list, submit news score
        """

        # Enter high risk observation to create News Observation task in task
        # list
        self.patient_list_page.go_to_patient_list()
        high_score = HIGH_RISK_SCORE_9_EWS_DATA
        no_risk = NO_RISK_EWS_DATA
        news_task = []
        success = 'Successfully Submitted NEWS Observation'

        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)
        PatientPage(self.driver).open_form(OPEN_OBS_MENU_NEWS_ITEM)
        PatientPage(self.driver).enter_obs_data(high_score)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(CONFIRM_SUBMIT)
        )
        self.driver.find_element(*CONFIRM_SUBMIT).click()

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located((
                SUCCESSFUL_SUBMIT))
        )
        response = self.driver.find_element(*SUCCESSFUL_SUBMIT)
        self.assertEqual(success, response.text,
                         'NEWS observation unsuccessful')

        self.driver.find_element(*GO_TO_MY_TASK).click()

        # Click on the first news score task from Task list
        self.task_list_page.go_to_task_list()

        self.driver.refresh()
        for task in self.task_list_page.get_list_task():
            # print(task.text)
            if task.text == 'NEWS Observation':
                news_task.append(task)
        news_task[0].click()

        # enter low risk observation
        PatientPage(self.driver).enter_obs_data(no_risk)

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located(CONFIRM_SUBMIT)
        )

        self.driver.find_element(*CONFIRM_SUBMIT).click()

        ui.WebDriverWait(self.driver, 5).until(
            ec.visibility_of_element_located((
                SUCCESSFUL_SUBMIT))
        )
        response = self.driver.find_element(*SUCCESSFUL_SUBMIT)
        self.assertEqual(success, response.text,
                         'NEWS observation unsuccessful')
class TestPatientListPage(TestCommon):
    """
    Setup a session and test that the task list page works correctly
    """

    def setUp(self):
        self.driver.get(MOB_LOGIN)
        self.login_page = LoginPage(self.driver)
        self.patient_list_page = ListPage(self.driver)
        self.login_page.login(NURSE_USERNM1, NURSE_PWD1)
        self.patient_list_page.go_to_patient_list()

    def test_can_logout(self):
        """
        Test that the title of the login page is Open-eObs
        """
        self.patient_list_page.logout()
        self.assertTrue(PageConfirm(self.driver).is_login_page(),
                        'Did not get to the logout page correctly')

    def test_can_go_task_list_page(self):
        """
        Test that can go to task list page
        """
        self.patient_list_page.go_to_task_list()
        self.assertTrue(PageConfirm(self.driver).is_task_list_page(),
                        'Did not get to the task list page correctly')

    def test_patient_list_page(self):
        """
        Test that can go to the patient list page
        """
        self.patient_list_page.go_to_patient_list()
        self.assertTrue(PageConfirm(self.driver).is_patient_list_page(),
                        'Did not get to patient list page correctly')

    def test_can_go_stand_in_page(self):
        """
        Test that can navigate to the stand in page
        """
        self.patient_list_page.go_to_standin()
        self.assertTrue(PageConfirm(self.driver).is_stand_in_page(),
                        'Did not get to stand in page correctly')

    def test_can_do_barcode_scan(self):
        """
        Test that can do a barcode scan
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        patient_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        id_to_use = self.patient_list_page.patient_scan_helper(int(patient_id))
        self.patient_list_page.do_barcode_scan(id_to_use['other_identifier'])

    def test_view_patient_details(self):
        """
        Test that clicking on a work item tasks user to carry out the task
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        patient_url = patient_to_test.get_attribute('href')
        patient_to_test.click()
        self.assertTrue(PageConfirm(self.driver).is_patient_page(),
                        'Did not get to patient page correctly')
        self.assertEqual(self.driver.current_url, patient_url,
                         'Incorrect url')

    def test_patient_name_in_list(self):
        """
        Test that the patient name is in the list item
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        name_to_use = task_data['full_name']
        patient_name = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_NAME
        )
        self.assertEqual(patient_name.text, name_to_use.strip(),
                         'Incorrect name')

    def test_patient_location_in_list(self):
        """
        Test that the patient name is in the list item
        """
        patients = self.patient_list_page.get_list_items()
        patient_to_test = patients[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        location = task_data['location']
        parent_location = task_data['parent_location']
        bed_to_use = '{0}, {1}'.format(location, parent_location)
        patient_location = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_LOCATION
        )
        self.assertEqual(bed_to_use, patient_location.text,
                         'Incorrect location')

    def test_score_and_trend_in_list(self):
        """
        Test that the score and trend are present in list item
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        score = task_data['ews_score']
        trend = task_data['ews_trend']
        score_str = '({0} )'.format(score)
        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )
        trend_str = 'icon-{0}-arrow'.format(trend)
        self.assertEqual(patient_trend.get_attribute('class'), trend_str,
                         'Incorrect trend')
        self.assertIn(score_str, patient_to_test.text, 'Incorrect score')

    def test_down_trend(self):
        """
        Test that the trend is lowered after submitting a high risk ob,
        followed by a low risk ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_high_risk_observation(int(task_id))
        PatientPage(self.driver).add_low_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'),
                         'icon-down-arrow',
                         'Incorrect trend')

    def test_up_trend(self):
        """
        Test that the trend is increased after submitting a low risk ob,
        followed by a high risk ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_low_risk_observation(int(task_id))
        PatientPage(self.driver).add_high_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'), 'icon-up-arrow',
                         'Incorrect trend')

    def test_same_trend(self):
        """
        Test that the trend stays the same after submitting two of the same ob
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )

        PatientPage(self.driver).add_medium_risk_observation(int(task_id))
        PatientPage(self.driver).add_medium_risk_observation(int(task_id))
        self.driver.refresh()

        patient_trend = self.driver.find_element(
            *list_page_locators.LIST_ITEM_PATIENT_TREND
        )

        self.assertEqual(patient_trend.get_attribute('class'),
                         'icon-same-arrow',
                         'Incorrect trend')

    def test_task_deadline_in_list(self):
        """
        Test that the patient name is in the list item
        """
        tasks = self.patient_list_page.get_list_items()
        patient_to_test = tasks[0]
        task_id = patient_to_test.get_attribute('href').replace(
            PATIENT_PAGE, ''
        )
        task_data = self.patient_list_page.patient_helper(task_id)[0]
        deadline = task_data['next_ews_time']
        task_deadline = self.driver.find_element(
            *list_page_locators.LIST_ITEM_DEADLINE
        )
        self.assertEqual(deadline, task_deadline.text,
                         'Incorrect deadline')

    def test_shows_patients(self):
        """
        Test that the patient list shows patients for the user
        """
        patient_list = []
        for patient in self.patient_list_page.get_list_items():
            patient_list.append(patient)

        self.assertNotEquals(patient_list, [],
                             'Patient list not showing patients')

    def test_shows_patient_data(self):
        """
        Test that a patient record shows data (graph/table) for the patient
        """
        patients = self.patient_list_page.get_list_items()

        PatientPage(self.driver).select_patient(patients)

        ui.WebDriverWait(self.driver, 1).until(
            ec.visibility_of_element_located(patient_page_locators.GRAPH_CHART)
        )

        patient_graph = self.driver.find_element(*
                                                 patient_page_locators
                                                 .GRAPH_CHART)

        self.assertEqual(patient_graph.is_displayed(), True, 'Graph not found')

        self.driver.find_element(*patient_page_locators
                                 .TABLE_TAB_BUTTON).click()

        ui.WebDriverWait(self.driver, 1).until(
            ec.visibility_of_element_located
            (patient_page_locators.TABLE_CONTAINER_TABLE))

        patient_table = self.driver.find_element(*
                                                 patient_page_locators
                                                 .TABLE_CONTAINER_TABLE)
        self.assertEqual(patient_table.is_displayed(), True, 'Table not found')