dashboard.py 3.26 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4 5
"""
Student dashboard page.
"""

6
from bok_choy.page_object import PageObject
7
from bok_choy.promise import EmptyPromise
8
from . import BASE_URL
9 10 11 12 13 14 15 16


class DashboardPage(PageObject):
    """
    Student dashboard, where the student can view
    courses she/he has registered for.
    """

17
    url = BASE_URL + "/dashboard"
18 19

    def is_browser_on_page(self):
20
        return self.q(css='section.my-courses').present
21

22
    @property
23
    def current_courses_text(self):
24 25 26 27 28
        """
        This is the title label for the section of the student dashboard that
        shows all the courses that the student is enrolled in.
        The string displayed is defined in lms/templates/dashboard.html.
        """
29
        text_items = self.q(css='section#my-courses').text
Julia Hansbrough committed
30 31 32 33 34 35
        if len(text_items) > 0:
            return text_items[0]
        else:
            return ""

    @property
36 37 38 39
    def available_courses(self):
        """
        Return list of the names of available courses (e.g. "999 edX Demonstration Course")
        """
40 41 42 43 44
        def _get_course_name(el):
            # The first component in the link text is the course number
            _, course_name = el.text.split(' ', 1)
            return course_name

45
        return self.q(css='section.info > hgroup > h3 > a').map(_get_course_name).results
46 47 48 49 50 51 52 53

    def view_course(self, course_id):
        """
        Go to the course with `course_id` (e.g. edx/Open_DemoX/edx_demo_course)
        """
        link_css = self._link_css(course_id)

        if link_css is not None:
54
            self.q(css=link_css).first.click()
55 56 57 58 59
        else:
            msg = "No links found for course {0}".format(course_id)
            self.warning(msg)

    def _link_css(self, course_id):
60 61 62
        """
        Return a CSS selector for the link to the course with `course_id`.
        """
63
        # Get the link hrefs for all courses
64
        all_links = self.q(css='a.enter-course').map(lambda el: el.get_attribute('href')).results
65 66 67 68 69 70 71 72 73 74 75 76

        # Search for the first link that matches the course id
        link_index = None
        for index in range(len(all_links)):
            if course_id in all_links[index]:
                link_index = index
                break

        if link_index is not None:
            return "a.enter-course:nth-of-type({0})".format(link_index + 1)
        else:
            return None
77

78 79 80 81
    def change_language(self, code):
        """
        Change the language on the dashboard to the language corresponding with `code`.
        """
82 83 84 85
        self.q(css=".edit-language").first.click()
        self.q(css='select[name="language"] option[value="{}"]'.format(code)).first.click()
        self.q(css="#submit-lang").first.click()

86 87 88 89
        # Clicking the submit-lang button does a jquery ajax post, so make sure that
        # has completed before continuing on.
        self.wait_for_ajax()

90 91 92 93
        self._changed_lang_promise(code).fulfill()

    def _changed_lang_promise(self, code):
        def _check_func():
94 95 96 97
            language_is_selected = self.q(css='select[name="language"] option[value="{}"]'.format(code)).selected
            modal_is_visible = self.q(css='section#change_language.modal').visible
            return (language_is_selected and not modal_is_visible)
        return EmptyPromise(_check_func, "language changed and modal hidden")