def test_rex_login_state_when_redirected_from_osweb( selenium, base_url, book_slug, page_slug, email, password ): # GIVEN: Open osweb book details page osweb = WebBase(selenium, base_url, book_slug=book_slug).open() osweb.close_dialogs() osweb.click_login() # AND: Login as existing user accounts = Login(selenium) accounts.login(email, password) osweb.wait_for_load() # verify user is logged in and get the username assert osweb.user_is_logged_in osweb_username = osweb.osweb_username() # WHEN: Click the view online link in osweb book detail page osweb.fix_view_online_url(base_url) osweb.click_view_online() # THEN: The book page is opened in REX with the same user as openstax.org rex = Content(selenium) rex.wait_for_page_to_load() rex_nav = rex.navbar assert rex_nav.user_is_logged_in rex_username = rex.username(rex_nav.user_nav_toggle)[3:] assert rex_username == osweb_username # AND: The user stays logged-in while navigating to other pages in REX rex.click_next_link() assert rex_nav.user_is_logged_in
def test_attribution_collapses_on_navigating_to_new_page( selenium, base_url, book_slug, page_slug): # GIVEN: A page URL in the format of {base_url}/books/{book_slug}/pages/{page_slug} # AND: The citation/attribution tab is open content = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() attribution = content.attribution toolbar = content.toolbar toc = content.sidebar.toc attribution.click_attribution_link() # WHEN: Navigating via next link content.click_next_link() # THEN: The citation/attribution section is not open on the new page assert not attribution.is_open attribution.click_attribution_link() # WHEN: Navigating via Previous link content.click_previous_link() # THEN: The citation/attribution section is not open on the new page assert not attribution.is_open attribution.click_attribution_link() # WHEN: Navigating via TOC link if content.is_mobile: toolbar.click_toc_toggle_button() toc.sections[-1].click() # THEN: The citation/attribution section is not open on the new page assert not attribution.is_open
def test_my_highlights_summary_shows_highlights_and_notes_on_current_page( selenium, base_url, book_slug, page_slug): """My Highlights and Notes summary shows page highlights and notes.""" # SETUP: ONE, TWO, THREE, FOUR = range(4) color = Highlight.random_color highlight_ids = [""] * 4 highlight_colors = [color(), color(), color(), color()] highlight_notes = [ "", Utilities.random_string(), "", Utilities.random_string() ] highlight_partial_text = [""] * 4 chapter_one = ("1", "Essential Ideas") sections = [("", "Introduction"), ("1.1", "Chemistry in Context")] # GIVEN: the Chemistry 2e book section 1.0 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 # AND: some content is highlighted in section 1.1 without a note # AND: some content is highlighted in section 1.1 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, password = Signup(selenium).register(True) 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_colors[ONE]) highlight_ids[ONE] = book.content.highlight_ids[0] highlight_partial_text[ONE] = book.content.get_highlight( by_id=highlight_ids[ONE])[0].text book.content.highlight( target=paragraphs[1], offset=Highlight.ENTIRE, color=highlight_colors[TWO], note=highlight_notes[TWO], ) page_highlight_ids = book.content.highlight_ids highlight_ids[TWO] = (page_highlight_ids[1] if highlight_ids[ONE] == page_highlight_ids[0] else page_highlight_ids[0]) highlight_partial_text[TWO] = book.content.get_highlight( by_id=highlight_ids[TWO])[0].text book.click_next_link() paragraphs = random.sample(book.content.paragraphs, 2) book.content.highlight(target=paragraphs[0], offset=Highlight.ENTIRE, color=highlight_colors[THREE]) highlight_ids[THREE] = book.content.highlight_ids[0] highlight_partial_text[THREE] = book.content.get_highlight( by_id=highlight_ids[THREE])[0].text book.content.highlight( target=paragraphs[1], offset=Highlight.ENTIRE, color=highlight_colors[FOUR], note=highlight_notes[FOUR], ) page_highlight_ids = book.content.highlight_ids highlight_ids[FOUR] = (page_highlight_ids[1] if highlight_ids[THREE] == page_highlight_ids[0] else page_highlight_ids[0]) highlight_partial_text[FOUR] = book.content.get_highlight( by_id=highlight_ids[FOUR])[0].text 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: sections without highlights are not included in the placeholders # AND: all highlights for the chapter are displayed # AND: each highlight color matches the corresponding page highlight color # AND: each note matches the corresponding page highlight note assert book.my_highlights_open, "My Highlights modal not open" assert my_highlights.root.is_displayed( ), "My Highlights modal not displayed" assert (len(my_highlights.highlights.chapters) == 1 ), "chapter heading not displayed in the highlights summary" highlight_chapter = ( my_highlights.highlights.chapters[0].number, my_highlights.highlights.chapters[0].title, ) assert highlight_chapter == chapter_one, "chapter number and title do not match in the summary" assert len(my_highlights.highlights.sections ) == 2, "did not find two section headings" highlight_sections = [(section.number, section.title) for section in my_highlights.highlights.sections] assert set(highlight_sections) == set( sections), "mismatched section numbers and/or names" current_summary_highlights = len(my_highlights.all_highlights) assert current_summary_highlights == len(highlight_ids), ( "unexpected number of highlights found on the summary page (" f"found {current_summary_highlights}, expected {len(highlight_ids)})") # ordering could be in sequence or reversed so check against both section # highlights for index, highlight in enumerate(my_highlights.all_highlights): option_1, option_2 = (ONE, TWO) if index <= TWO else (THREE, FOUR) assert (highlight.color == highlight_colors[option_1] or highlight.color == highlight_colors[option_2] ), f"highlight color for highlight {index + 1} does not match" content = highlight.content assert ( highlight_partial_text[option_1] in content or highlight_partial_text[option_2] in content ), f"partial note text not found in summary highlight {index + 1}" # currently the summary trims extra spaces and removes carriage returns note_1 = highlight_notes[option_1].replace("\n", " ").replace(" ", " ") note_2 = highlight_notes[option_2].replace("\n", " ").replace(" ", " ") assert (highlight.note == note_1 or highlight.note == note_2 ), f"highlight note {index + 1} does not match content note"
def test_change_highlight_color_using_keyboard_content_page( selenium, base_url, book_slug, page_slug): """Highlight color can be changed using keyboard navigation.""" # GIVEN: Login book page 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() # AND: Highlight some text in the page without a note paragraphs = random.sample(book.content.paragraphs, 1) book.content.highlight(target=paragraphs[0], offset=Highlight.ENTIRE, color=Color.GREEN) highlight_no_note = book.content.highlight_ids[0] # AND: Navigate to next page and highlight some text with a note book.click_next_link() paragraphs = random.sample(book.content.paragraphs, 1) book.content.highlight( target=paragraphs[0], offset=Highlight.ENTIRE, color=Color.YELLOW, note=Utilities.random_string(), ) highlight_with_note = book.content.highlight_ids[0] book.reload() # WHEN: Tab to the highlight and hit H key (ActionChains(selenium).send_keys(Keys.TAB).send_keys( Keys.ENTER).perform()) (ActionChains(selenium).send_keys(Keys.TAB).send_keys("H").perform()) # AND: Change the highlight color in the active notecard using shift tab and spacebar keys (ActionChains(selenium).send_keys(Keys.TAB).send_keys( Keys.ENTER).perform()) (ActionChains(selenium).send_keys(Keys.TAB).send_keys( Keys.ENTER).perform()) (ActionChains(selenium).send_keys(Keys.SHIFT + Keys.TAB + Keys.SHIFT).send_keys( Keys.SPACE).perform()) # AND: Navigate out of the highlight (ActionChains(selenium).send_keys("H").perform()) # THEN: The highlight color is changed highlight_classes_0 = book.content.get_highlight( by_id=highlight_with_note)[0].get_attribute("class") highlight_0_color_after_color_change = Color.from_html_class( highlight_classes_0) assert ( highlight_0_color_after_color_change == Color.PINK ), f"Highlight color in the page {selenium.current_url} is {highlight_0_color_after_color_change}" # WHEN: Navigate to the previous page, tab to the highlight without note and hit H key (ActionChains(selenium).send_keys(Keys.TAB).send_keys( Keys.ENTER).perform()) (ActionChains(selenium).send_keys(Keys.TAB).send_keys( Keys.ENTER).perform()) (ActionChains(selenium).send_keys(Keys.TAB).send_keys("H").perform()) # AND: Change the highlight color and navigate out of the highlight (ActionChains(selenium).send_keys(Keys.SHIFT + Keys.TAB + Keys.SHIFT).perform()) (ActionChains(selenium).send_keys(Keys.SPACE).perform()) (ActionChains(selenium).send_keys("H").send_keys(Keys.TAB).perform()) # THEN: The highlight color is changed highlight_classes_1 = book.content.get_highlight( by_id=highlight_no_note)[0].get_attribute("class") highlight_1_color_after_color_change = Color.from_html_class( highlight_classes_1) assert ( highlight_1_color_after_color_change == Color.PINK ), f"Highlight color in the page {selenium.current_url} is {highlight_1_color_after_color_change}"
def test_modal_for_unsaved_notes_appears_on_page_navigation_using_prev_link( selenium, base_url, book_slug, page_slug ): """Discard modal appears when unsaved notes are present & clicking previous link.""" # GIVEN: Login book page book = Content(selenium, base_url, book_slug=book_slug, page_slug=page_slug).open() 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() # AND: Highlight a paragraph, add a note & do not save paragraphs = random.sample(book.content.paragraphs, 1) book.content.highlight(target=paragraphs[0], offset=Highlight.ENTIRE, close_box=False) note = Utilities.random_string() book.content.highlight_box.note = note highlight_id = book.content.highlight_ids[0] # WHEN: Click previous link book.offscreen_click(book.previous_link) # THEN: Discard modal is displayed assert book.discard_changes_modal_displayed assert book.discard_modal.content == "You have an unsaved note on this page." assert book.discard_modal.title == "Discard unsaved changes?" # WHEN: Click Cancel on the modal book.discard_modal.click_cancel_changes() # THEN: The modal is closed and the unsaved note is retained on the page assert book.content.highlight_box.is_open, "Highlight box not open" assert book.content.highlight_box.is_edit_box highlight = book.content.get_highlight(by_id=highlight_id)[0] assert "focus" in highlight.get_attribute("class"), "highlight is not in focus" assert book.content.highlight_box.note == note # WHEN: Click previous link again book.offscreen_click(book.previous_link) # AND: click Discard changes in the modal book.click_and_wait_for_load(book.discard_modal.discard_button) # THEN: New page is loaded assert toc.sections[0].is_active # AND: No highlight box is open in the new page with pytest.raises(NoSuchElementException) as e: book.content.highlight_box assert "No open highlight boxes found" in str(e.value), "highlight box is open in the new page" # AND: No existing highlights present in the new page try: assert not book.content.highlights except NoSuchElementException: pytest.fail("existing highlight present in the page") # WHEN: Navigate back to the initial page book.click_next_link() # THEN: The unsaved note in the initial page is not saved highlight = book.content.get_highlight(by_id=highlight_id)[0] assert not selenium.execute_script(HAS_INDICATOR, highlight), "note is saved for the highlight"