def test_highlight_is_not_created_until_a_color_is_selected( selenium, base_url, book_slug, page_slug): """A highlight is not created until the highlight color is selected.""" # GIVEN: the Astronomy book section 1.0 introduction is displayed # AND: a user is logged in # AND: all content is visible book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() while book.notification_present: book.notification.got_it() book.navbar.click_login() Signup(selenium).register() book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() total_highlight_count = book.content.highlight_count # WHEN: they select some text content paragraph = random.choice(book.content.paragraphs) book.content.highlight(target=paragraph, offset=Highlight.RANDOM, color=None) new_highlight_count = book.content.highlight_count # THEN: the create note box is displayed # AND: the selected text is not a saved highlight try: book.content.highlight_box except NoSuchElementException: pytest.fail("the create note box is not open") assert(new_highlight_count == total_highlight_count), \ "a new highlight was found" total_highlight_count = book.content.highlight_count # WHEN: they select a highlight color book.content.highlight_box.toggle_color(Highlight.random_color()) new_highlight_count = book.content.highlight_count # THEN: the highlight is created assert(new_highlight_count > total_highlight_count), \ "the new highlight was not found"
def test_note_indicator_not_present_for_highlights_without_notes( selenium, base_url, book_slug, page_slug): """The note indicator is not present on highlights without notes. .. note:: The note indicator has been temporarily been replaced by off-color underlining (bottom border). """ # GIVEN: a book page is displayed # AND: a user is logged in # AND: all content is visible # AND: some content is highlighted without a note # AND: some content is highlighted with a note book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email = Signup(selenium).register() book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() # making a highlight requires a non-mobile window width temporarily width, height = book.get_window_size() if width < DESKTOP[0]: selenium.set_window_size(width=DESKTOP[0], height=height) paragraphs = random.sample(book.content.paragraphs, 2) book.content.highlight(target=paragraphs[0], offset=Highlight.ENTIRE, color=Highlight.random_color()) no_note = list(set(book.content.highlight_ids))[0] book.content.highlight(target=paragraphs[1], offset=Highlight.ENTIRE, color=Highlight.random_color(), note=Utilities.random_string()) _ids = book.content.highlight_ids with_note = _ids[0] if _ids[0] != no_note else _ids[1] if width != DESKTOP[0]: # reset the window width for a mobile test selenium.set_window_size(width=width, height=height) # WHEN: # THEN: the note indicator is not present on the highlight # AND: the note indicator is present on the highlight with a note no_note = book.content.get_highlight(by_id=no_note)[0] assert(not selenium.execute_script(HAS_INDICATOR, no_note)), \ "indicator found on a highlight without a note" with_note = book.content.get_highlight(by_id=with_note)[0] assert(selenium.execute_script(HAS_INDICATOR, with_note)), \ "indicator not found on a highlight with a note"
def test_clicking_on_the_note_indicator_opens_the_note_card( selenium, base_url, book_slug, page_slug): """Open the note card for various highlights with notes.""" # GIVEN: a book page is displayed # AND: a user is logged in # AND: all content is visible # AND: several types of content are highlighted with notes book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email = Signup(selenium).register() book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() # making a highlight requires a non-mobile window width temporarily width, height = book.get_window_size() if width < DESKTOP[0]: selenium.set_window_size(width=DESKTOP[0], height=height) highlight_options = [ (random.choice(book.content.figures_and_captions), Utilities.random_string()), (random.choice(book.content.lists), Utilities.random_string()), (random.choice(book.content.math), Utilities.random_string()), (random.choice(book.content.paragraphs), Utilities.random_string()), (random.choice(book.content.tables), Utilities.random_string()) ] highlight_list = {} for content, note in highlight_options: highlight_ids = book.content.highlight_ids book.content.highlight(target=content, offset=Highlight.ENTIRE, color=Highlight.random_color(), note=note) new_id = list(set(book.content.highlight_ids) - set(highlight_ids))[0] highlight_list[new_id] = note if width != DESKTOP[0]: # reset the window width for a mobile test selenium.set_window_size(width=width, height=height) for _id in book.content.highlight_ids: # WHEN: they select each highlight Utilities.click_option( driver=selenium, element=book.content.get_highlight(by_id=_id)[0], scroll_to=-130) # THEN: the highlight card and note are displayed try: book.content.highlight_box except NoSuchElementException: pytest.fail("the highlight note card is not open") assert(book.content.highlight_box.note == highlight_list[_id]), \ "Displayed note does not match"
def test_lengthy_highlights_summary_page_has_a_floating_back_to_top_link( selenium, base_url, book_slug, page_slug): """My Highlights and Notes summary has a floating back to top button.""" # GIVEN: a book section is displayed # AND: a user is logged in # AND: all content is visible # AND: several sections are highlighted book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email, password = Signup(selenium).register(True) logging.info(f'"{email.address}":"{password}"') book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() # making a highlight requires a non-mobile window width temporarily width, height = book.get_window_size() if width <= DESKTOP[0]: selenium.set_window_size(width=DESKTOP[0], height=height) for _ in range(10): Highlight.force_highlight(book=book, by=random.choice, group=book.content.paragraphs, offset=Highlight.ENTIRE, color=Highlight.random_color()) if width != DESKTOP[0]: # reset the window width for a mobile test selenium.set_window_size(width=width, height=height) # WHEN: they open the highlights summary modal # AND: the modal is scrolled down my_highlights = book.toolbar.my_highlights() initial_scroll_top = my_highlights.scroll_position within = max(initial_scroll_top * 0.01, 10.0) Utilities.scroll_to(selenium, element=my_highlights.all_highlights[-1].root) sleep(0.33) # THEN: a floating back to top button is displayed in the lower right # side of the modal assert my_highlights.scroll_position > initial_scroll_top, "modal not scrolled down" assert my_highlights.back_to_top_available, "back to top button not found" # WHEN: they click the back to top button my_highlights = my_highlights.back_to_top() # THEN: the modal is scrolled to the top # AND: the back to top button is not available return_position = my_highlights.scroll_position assert isclose(return_position, initial_scroll_top, rel_tol=within), ( r"return scroll position not within 1% of the initial scroll position " "({low} <= {target} <= {high})".format( low=initial_scroll_top - within, high=initial_scroll_top + within, target=return_position, )) assert not my_highlights.back_to_top_available, "back to top button still available"
def test_my_highlights_summary_shows_all_types_of_content( selenium, base_url, book_slug, page_slug): """My Highlights and Notes summary shows all types of page content.""" # GIVEN: the Chemistry 2e book section 1.4 is displayed # AND: a user is logged in # AND: all content is visible # AND: a long text paragraph, an image, a figure description, a bulleted # or numbered list, a table, a footnote, a link, and a rendered math # equation are highlighted book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email, password = Signup(selenium).register(True) logging.info(f'"{email.address}":"{password}"') book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() # making a highlight requires a non-mobile window width temporarily width, height = book.get_window_size() if width <= DESKTOP[0]: selenium.set_window_size(width=DESKTOP[0], height=height) Highlight.force_highlight(book=book, by=random.choice, group=book.content.paragraphs, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Paragraph") Highlight.force_highlight(book=book, by=random.choice, group=book.content.images, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Image") Highlight.force_highlight(book=book, by=random.choice, group=book.content.figures, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Figure") Highlight.force_highlight(book=book, by=random.choice, group=book.content.captions, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Caption") Highlight.force_highlight(book=book, by=random.choice, group=book.content.lists, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="List") Highlight.force_highlight(book=book, by=random.choice, group=book.content.tables, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Table") Highlight.force_highlight(book=book, by=random.choice, group=book.content.footnotes, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Footnote") Highlight.force_highlight(book=book, by=random.choice, group=book.content.links, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Link") Highlight.force_highlight(book=book, by=random.choice, group=book.content.math, offset=Highlight.ENTIRE, color=Highlight.random_color(), name="Math") highlight_ids = set(book.content.highlight_ids) if width != DESKTOP[0]: # reset the window width for a mobile test selenium.set_window_size(width=width, height=height) # WHEN: they click on the My highlights button my_highlights = book.toolbar.my_highlights() # THEN: the My Highlights and Notes modal is displayed # AND: all of the highlighted content is displayed in the summary page assert book.my_highlights_open, "My Highlights modal not open" assert my_highlights.root.is_displayed( ), "My Highlights modal not displayed" summary_highlights = len(my_highlights.all_highlights) assert summary_highlights == len(highlight_ids), ( "number of summary highlights different from page highlights " f"(found {summary_highlights}, expected {len(highlight_ids)})")
def test_filter_state_not_preserved_for_MH_in_new_tab(selenium, base_url, book_slug, page_slug): """Filter state is not preserved if MH page is opened in a new tab.""" # GIVEN: Login book page book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() toolbar = book.toolbar toc = book.sidebar.toc while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email = Signup(selenium).register() book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() content_highlight_ids = book.content.highlight_ids my_highlights = book.toolbar.my_highlights() mh_highlight_ids = my_highlights.highlights.mh_highlight_ids # AND: Highlights are present in different chapter pages page_slug = [ "1-3-types-of-microorganisms", "2-4-staining-microscopic-specimens", "4-2-proteobacteria", "5-introduction", ] for page in page_slug: book = Content(selenium, base_url, book_slug=book_slug, page_slug=page).open() Highlight.force_highlight(book=book, by=random.choice, group=book.content.paragraphs, offset=Highlight.ENTIRE, color=Highlight.random_color()) content_highlight_ids = content_highlight_ids + list( set(book.content.highlight_ids) - set(content_highlight_ids)) my_highlights = book.toolbar.my_highlights() mh_highlight_ids = mh_highlight_ids + list( set(my_highlights.highlights.mh_highlight_ids) - set(mh_highlight_ids)) # THEN: MH page displays all the content highlights assert mh_highlight_ids == content_highlight_ids # WHEN: Change the MH chapter filters to remove 2 chapters my_highlights = book.toolbar.my_highlights() filterbar = my_highlights.filter_bar # Use chapter dropdown to remove one chapter filterbar.toggle_chapter_dropdown_menu() filterbar.chapter_filters.chapters[2].click() filterbar.toggle_chapter_dropdown_menu() # Use filter tag to remove one chapter x = filterbar.active_filter_tags x[1].remove_tag() my_highlights = book.toolbar.my_highlights() mh_filtered_list = my_highlights.highlights.mh_highlight_ids my_highlights.close() # AND: Open MH page from a chapter that does not have highlights if book.is_mobile: toolbar.click_toc_toggle_button() toc.expand_chapter(-3) toc.sections[-40].click() my_highlights = book.toolbar.my_highlights() mh_list_from_chapter_without_highlights = my_highlights.highlights.mh_highlight_ids # THEN: Filter changes made earlier are retained assert set(mh_list_from_chapter_without_highlights) == set( mh_filtered_list) # WHEN: Open MH page in new tab book.open_new_tab() book.switch_to_window(1) page_slug = "2-4-staining-microscopic-specimens" book1 = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() my_highlights_1 = book1.toolbar.my_highlights() # THEN: MH page in the new tab displays highlights from all the chapters mh_list_in_new_tab = my_highlights_1.highlights.mh_highlight_ids assert set(mh_list_in_new_tab) == set(content_highlight_ids)
def test_filter_state_preserved_throughout_session(selenium, base_url, book_slug, page_slug): """Filter state preserved throughout the session irrespective of chapter/section navigation.""" # GIVEN: Login book page book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() toolbar = book.toolbar toc = book.sidebar.toc while book.notification_present: book.notification.got_it() book.navbar.click_login() name, email = Signup(selenium).register() book.wait_for_page_to_load() while book.notification_present: book.notification.got_it() book.content.show_solutions() content_highlight_ids = book.content.highlight_ids my_highlights = book.toolbar.my_highlights() mh_highlight_ids = my_highlights.highlights.mh_highlight_ids # AND: Highlights are present in different chapter pages page_slug = [ "1-3-types-of-microorganisms", "2-4-staining-microscopic-specimens", "4-2-proteobacteria", "5-introduction", ] for page in page_slug: book = Content(selenium, base_url, book_slug=book_slug, page_slug=page).open() Highlight.force_highlight(book=book, by=random.choice, group=book.content.paragraphs, offset=Highlight.ENTIRE, color=Highlight.random_color()) content_highlight_ids = content_highlight_ids + list( set(book.content.highlight_ids) - set(content_highlight_ids)) my_highlights = book.toolbar.my_highlights() mh_highlight_ids = mh_highlight_ids + list( set(my_highlights.highlights.mh_highlight_ids) - set(mh_highlight_ids)) # THEN: MH page displays all the content highlights assert mh_highlight_ids == content_highlight_ids # WHEN: Change the MH chapter filters to remove 2 chapters my_highlights = book.toolbar.my_highlights() filterbar = my_highlights.filter_bar # Use chapter dropdown to remove one chapter filterbar.toggle_chapter_dropdown_menu() filterbar.chapter_filters.chapters[2].click() filterbar.toggle_chapter_dropdown_menu() # Use filter tag to remove one chapter x = filterbar.active_filter_tags x[1].remove_tag() my_highlights = book.toolbar.my_highlights() mh_filtered_list = my_highlights.highlights.mh_highlight_ids my_highlights.close() # AND: Open MH page from a chapter page that has highlights but removed via MH filter if book.is_mobile: toolbar.click_toc_toggle_button() toc.expand_chapter(2) toc.sections[14].click() my_highlights = book.toolbar.my_highlights() mh_list_from_chapter_with_highlights = my_highlights.highlights.mh_highlight_ids # THEN: Filter changes made earlier are retained assert set(mh_list_from_chapter_with_highlights) == set(mh_filtered_list) my_highlights.close() # WHEN: Open MH page from a chapter that does not have highlights if book.is_mobile: toolbar.click_toc_toggle_button() toc.expand_chapter(-3) toc.sections[-40].click() my_highlights = book.toolbar.my_highlights() mh_list_from_chapter_without_highlights = my_highlights.highlights.mh_highlight_ids # THEN: Filter changes made earlier are retained assert set(mh_list_from_chapter_without_highlights) == set( mh_filtered_list) # WHEN: Re-add one of the removed chapter filterbar = my_highlights.filter_bar filterbar.toggle_chapter_dropdown_menu() filterbar.chapter_filters.chapters[2].click() filterbar.toggle_chapter_dropdown_menu() mh_updated_filtered_list = my_highlights.highlights.mh_highlight_ids my_highlights.close() # AND: Navigate to another chapter if book.is_mobile: toolbar.click_toc_toggle_button() toc.expand_chapter(0) toc.sections[4].click() # THEN: The MH list is updated with the highlight from re-added chapter my_highlights = book.toolbar.my_highlights() mh_list_after_page_navigation = my_highlights.highlights.mh_highlight_ids assert set(mh_list_after_page_navigation) == set(mh_updated_filtered_list) # WHEN: Reload the page book.reload() # THEN: MH filters resets to display highlights from all the chapters my_highlights = book.toolbar.my_highlights() mh_list_after_reload = my_highlights.highlights.mh_highlight_ids assert set(mh_list_after_reload) == set(content_highlight_ids)