How to use is_element_visible method in SeleniumBase

Best Python code snippet using SeleniumBase

discussion.py

Source:discussion.py Github

copy

Full Screen

...70 None if no such element exists71 """72 text_list = self._find_within(selector).text73 return text_list[0] if text_list else None74 def is_element_visible(self, selector):75 """76 Returns true if the element matching the specified selector is visible.77 Args:78 selector (str): The CSS selector that matches the desired element.79 Returns:80 bool: True if the element is visible.81 """82 query = self._find_within(selector)83 return query.present and query.visible84 @contextmanager85 def secondary_action_menu_open(self, ancestor_selector):86 """87 Given the selector for an ancestor of a secondary menu, return a context88 manager that will open and close the menu89 """90 self.wait_for_ajax()91 self._find_within(ancestor_selector + " .action-more").click()92 EmptyPromise(93 lambda: self.is_element_visible(ancestor_selector + " .actions-dropdown"),94 "Secondary action menu opened"95 ).fulfill()96 yield97 if self.is_element_visible(ancestor_selector + " .actions-dropdown"):98 self._find_within(ancestor_selector + " .action-more").click()99 EmptyPromise(100 lambda: not self.is_element_visible(ancestor_selector + " .actions-dropdown"),101 "Secondary action menu closed"102 ).fulfill()103 def get_group_visibility_label(self):104 """105 Returns the group visibility label shown for the thread.106 """107 return self._get_element_text(".group-visibility-label")108 def get_response_total_text(self):109 """Returns the response count text, or None if not present"""110 self.wait_for_ajax()111 return self._get_element_text(".response-count")112 def get_num_displayed_responses(self):113 """Returns the number of responses actually rendered"""114 return len(self._find_within(".discussion-response"))115 def get_shown_responses_text(self):116 """Returns the shown response count text, or None if not present"""117 return self._get_element_text(".response-display-count")118 def get_load_responses_button_text(self):119 """Returns the load more responses button text, or None if not present"""120 return self._get_element_text(".load-response-button")121 def load_more_responses(self):122 """Clicks the load more responses button and waits for responses to load"""123 self._find_within(".load-response-button").click()124 EmptyPromise(125 self.is_ajax_finished,126 "Loading more Responses"127 ).fulfill()128 def has_add_response_button(self):129 """Returns true if the add response button is visible, false otherwise"""130 return self.is_element_visible(".add-response-btn")131 def has_discussion_reply_editor(self):132 """133 Returns true if the discussion reply editor is is visible134 """135 return self.is_element_visible(".discussion-reply-new")136 def click_add_response_button(self):137 """138 Clicks the add response button and ensures that the response text139 field receives focus140 """141 self._find_within(".add-response-btn").first.click()142 EmptyPromise(143 lambda: self._find_within(".discussion-reply-new textarea:focus").present,144 "Response field received focus"145 ).fulfill()146 @wait_for_js147 def is_response_editor_visible(self, response_id):148 """Returns true if the response editor is present, false otherwise"""149 return self.is_element_visible(".response_{} .edit-post-body".format(response_id))150 @wait_for_js151 def is_discussion_body_visible(self):152 return self.is_element_visible(".post-body")153 def verify_mathjax_preview_available(self):154 """ Checks that MathJax Preview css class is present """155 self.wait_for(156 lambda: len(self.q(css=".MathJax_Preview").text) > 0 and self.q(css=".MathJax_Preview").text[0] == "",157 description="MathJax Preview is rendered"158 )159 def verify_mathjax_rendered(self):160 """ Checks that MathJax css class is present """161 self.wait_for(162 lambda: self.is_element_visible(".MathJax_SVG"),163 description="MathJax Preview is rendered"164 )165 def is_response_visible(self, comment_id):166 """Returns true if the response is viewable onscreen"""167 self.wait_for_ajax()168 return self.is_element_visible(".response_{} .response-body".format(comment_id))169 def is_response_editable(self, response_id):170 """Returns true if the edit response button is present, false otherwise"""171 with self.secondary_action_menu_open(".response_{} .discussion-response".format(response_id)):172 return self.is_element_visible(".response_{} .discussion-response .action-edit".format(response_id))173 def is_response_deletable(self, response_id):174 """175 Returns true if the delete response button is present, false otherwise176 """177 with self.secondary_action_menu_open(".response_{} .discussion-response".format(response_id)):178 return self.is_element_visible(".response_{} .discussion-response .action-delete".format(response_id))179 def get_response_body(self, response_id):180 return self._get_element_text(".response_{} .response-body".format(response_id))181 def start_response_edit(self, response_id):182 """Click the edit button for the response, loading the editing view"""183 with self.secondary_action_menu_open(".response_{} .discussion-response".format(response_id)):184 self._find_within(".response_{} .discussion-response .action-edit".format(response_id)).first.click()185 EmptyPromise(186 lambda: self.is_response_editor_visible(response_id),187 "Response edit started"188 ).fulfill()189 def get_link_href(self):190 """Extracts href attribute of the referenced link"""191 link_href = self._find_within(".post-body p a").attrs('href')192 return link_href[0] if link_href else None193 def get_response_vote_count(self, response_id):194 vote_count_css = '.response_{} .discussion-response .action-vote'.format(response_id)195 vote_count_element = self.browser.find_element_by_css_selector(vote_count_css)196 # To get the vote count, one must hover over the element first.197 hover(self.browser, vote_count_element)198 return self._get_element_text(".response_{} .discussion-response .action-vote .vote-count".format(response_id))199 def vote_response(self, response_id):200 current_count = self.get_response_vote_count(response_id)201 self._find_within(".response_{} .discussion-response .action-vote".format(response_id)).first.click()202 self.wait_for(203 lambda: current_count != self.get_response_vote_count(response_id),204 description="Vote updated for {response_id}".format(response_id=response_id)205 )206 def cannot_vote_response(self, response_id):207 """Assert that the voting button is not visible on this response"""208 return not self.is_element_visible(".response_{} .discussion-response .action-vote".format(response_id))209 def is_response_reported(self, response_id):210 return self.is_element_visible(".response_{} .discussion-response .post-label-reported".format(response_id))211 def report_response(self, response_id):212 with self.secondary_action_menu_open(".response_{} .discussion-response".format(response_id)):213 self._find_within(".response_{} .discussion-response .action-report".format(response_id)).first.click()214 self.wait_for_ajax()215 EmptyPromise(216 lambda: self.is_response_reported(response_id),217 "Response is reported"218 ).fulfill()219 def cannot_report_response(self, response_id):220 """Assert that the reporting button is not visible on this response"""221 return not self.is_element_visible(".response_{} .discussion-response .action-report".format(response_id))222 def is_response_endorsed(self, response_id):223 return "endorsed" in self._get_element_text(".response_{} .discussion-response .posted-details".format(response_id))224 def endorse_response(self, response_id):225 self._find_within(".response_{} .discussion-response .action-endorse".format(response_id)).first.click()226 self.wait_for_ajax()227 EmptyPromise(228 lambda: self.is_response_endorsed(response_id),229 "Response edit started"230 ).fulfill()231 def set_response_editor_value(self, response_id, new_body):232 """Replace the contents of the response editor"""233 self._find_within(".response_{} .discussion-response .wmd-input".format(response_id)).fill(new_body)234 def verify_link_editor_error_messages_shown(self):235 """236 Confirm that the error messages are displayed in the editor.237 """238 def errors_visible():239 """240 Returns True if both errors are visible, False otherwise.241 """242 return (243 self.q(css="#new-url-input-field-message.has-error").visible and244 self.q(css="#new-url-desc-input-field-message.has-error").visible245 )246 self.wait_for(errors_visible, "Form errors should be visible.")247 def add_content_via_editor_button(self, content_type, response_id, url, description, is_decorative=False):248 """Replace the contents of the response editor"""249 self._find_within(250 "#wmd-{}-button-edit-post-body-{}".format(251 content_type,252 response_id,253 )254 ).click()255 self.q(css='#new-url-input').fill(url)256 self.q(css='#new-url-desc-input').fill(description)257 if is_decorative:258 self.q(css='#img-is-decorative').click()259 self.q(css='input[value="OK"]').click()260 def submit_response_edit(self, response_id, new_response_body):261 """Click the submit button on the response editor"""262 def submit_response_check_func():263 """264 Tries to click "Update post" and returns True if the post265 was successfully updated, False otherwise.266 """267 self._find_within(268 ".response_{} .discussion-response .post-update".format(269 response_id270 )271 ).first.click()272 return (273 not self.is_response_editor_visible(response_id) and274 self.is_response_visible(response_id) and275 self.get_response_body(response_id) == new_response_body276 )277 self.wait_for(submit_response_check_func, "Comment edit succeeded")278 def is_show_comments_visible(self, response_id):279 """Returns true if the "show comments" link is visible for a response"""280 return self.is_element_visible(".response_{} .action-show-comments".format(response_id))281 def show_comments(self, response_id):282 """Click the "show comments" link for a response"""283 self._find_within(".response_{} .action-show-comments".format(response_id)).first.click()284 EmptyPromise(285 lambda: self.is_element_visible(".response_{} .comments".format(response_id)),286 "Comments shown"287 ).fulfill()288 def is_add_comment_visible(self, response_id):289 """Returns true if the "add comment" form is visible for a response"""290 return self.is_element_visible("#wmd-input-comment-body-{}".format(response_id))291 def is_comment_visible(self, comment_id):292 """Returns true if the comment is viewable onscreen"""293 return self.is_element_visible("#comment_{} .response-body".format(comment_id))294 def get_comment_body(self, comment_id):295 return self._get_element_text("#comment_{} .response-body".format(comment_id))296 def is_comment_deletable(self, comment_id):297 """Returns true if the delete comment button is present, false otherwise"""298 with self.secondary_action_menu_open("#comment_{}".format(comment_id)):299 return self.is_element_visible("#comment_{} .action-delete".format(comment_id))300 def delete_comment(self, comment_id):301 with self.handle_alert():302 with self.secondary_action_menu_open("#comment_{}".format(comment_id)):303 self._find_within("#comment_{} .action-delete".format(comment_id)).first.click()304 EmptyPromise(305 lambda: not self.is_comment_visible(comment_id),306 "Deleted comment was removed"307 ).fulfill()308 def is_comment_editable(self, comment_id):309 """Returns true if the edit comment button is present, false otherwise"""310 with self.secondary_action_menu_open("#comment_{}".format(comment_id)):311 return self.is_element_visible("#comment_{} .action-edit".format(comment_id))312 def is_comment_editor_visible(self, comment_id):313 """Returns true if the comment editor is present, false otherwise"""314 return self.is_element_visible(".edit-comment-body[data-id='{}']".format(comment_id))315 def _get_comment_editor_value(self, comment_id):316 return self._find_within("#wmd-input-edit-comment-body-{}".format(comment_id)).text[0]317 def start_comment_edit(self, comment_id):318 """Click the edit button for the comment, loading the editing view"""319 old_body = self.get_comment_body(comment_id)320 with self.secondary_action_menu_open("#comment_{}".format(comment_id)):321 self._find_within("#comment_{} .action-edit".format(comment_id)).first.click()322 EmptyPromise(323 lambda: (324 self.is_comment_editor_visible(comment_id) and325 not self.is_comment_visible(comment_id) and326 self._get_comment_editor_value(comment_id) == old_body327 ),328 "Comment edit started"329 ).fulfill()330 def set_comment_editor_value(self, comment_id, new_body):331 """Replace the contents of the comment editor"""332 self._find_within("#comment_{} .wmd-input".format(comment_id)).fill(new_body)333 def submit_comment_edit(self, comment_id, new_comment_body):334 """Click the submit button on the comment editor"""335 self._find_within("#comment_{} .post-update".format(comment_id)).first.click()336 self.wait_for_ajax()337 EmptyPromise(338 lambda: (339 not self.is_comment_editor_visible(comment_id) and340 self.is_comment_visible(comment_id) and341 self.get_comment_body(comment_id) == new_comment_body342 ),343 "Comment edit succeeded"344 ).fulfill()345 def cancel_comment_edit(self, comment_id, original_body):346 """Click the cancel button on the comment editor"""347 self._find_within("#comment_{} .post-cancel".format(comment_id)).first.click()348 EmptyPromise(349 lambda: (350 not self.is_comment_editor_visible(comment_id) and351 self.is_comment_visible(comment_id) and352 self.get_comment_body(comment_id) == original_body353 ),354 "Comment edit was canceled"355 ).fulfill()356class DiscussionSortPreferencePage(CoursePage):357 """358 Page that contain the discussion board with sorting options359 """360 def __init__(self, browser, course_id):361 super(DiscussionSortPreferencePage, self).__init__(browser, course_id)362 self.url_path = "discussion/forum"363 def is_browser_on_page(self):364 """365 Return true if the browser is on the right page else false.366 """367 return self.q(css="body.discussion .forum-nav-sort-control").present368 def show_all_discussions(self):369 """ Show the list of all discussions. """370 self.q(css=".all-topics").click()371 def get_selected_sort_preference(self):372 """373 Return the text of option that is selected for sorting.374 """375 # Using this workaround (execute script) to make this test work with Chrome browser376 selected_value = self.browser.execute_script(377 'var selected_value = $(".forum-nav-sort-control").val(); return selected_value')378 return selected_value379 def change_sort_preference(self, sort_by):380 """381 Change the option of sorting by clicking on new option.382 """383 self.q(css=".forum-nav-sort-control option[value='{0}']".format(sort_by)).click()384 # Click initiates an ajax call, waiting for it to complete385 self.wait_for_ajax()386 def refresh_page(self):387 """388 Reload the page.389 """390 self.browser.refresh()391class DiscussionTabSingleThreadPage(CoursePage):392 def __init__(self, browser, course_id, discussion_id, thread_id):393 super(DiscussionTabSingleThreadPage, self).__init__(browser, course_id)394 self.thread_page = DiscussionThreadPage(395 browser,396 "body.discussion .discussion-article[data-id='{thread_id}']".format(thread_id=thread_id)397 )398 self.url_path = "discussion/forum/{discussion_id}/threads/{thread_id}".format(399 discussion_id=discussion_id, thread_id=thread_id400 )401 def is_browser_on_page(self):402 return self.thread_page.is_browser_on_page()403 def __getattr__(self, name):404 return getattr(self.thread_page, name)405 def show_all_discussions(self):406 """ Show the list of all discussions. """407 self.q(css=".all-topics").click()408 def close_open_thread(self):409 with self.thread_page.secondary_action_menu_open(".thread-main-wrapper"):410 self._find_within(".thread-main-wrapper .action-close").first.click()411 def _thread_is_rendered_successfully(self, thread_id):412 return self.q(css=".discussion-article[data-id='{}']".format(thread_id)).visible413 def click_and_open_thread(self, thread_id):414 """415 Click specific thread on the list.416 """417 thread_selector = "li[data-id='{}']".format(thread_id)418 self.show_all_discussions()419 self.q(css=thread_selector).first.click()420 EmptyPromise(421 lambda: self._thread_is_rendered_successfully(thread_id),422 "Thread has been rendered"423 ).fulfill()424 def check_threads_rendered_successfully(self, thread_count):425 """426 Count the number of threads available on page.427 """428 return len(self.q(css=".forum-nav-thread").results) == thread_count429class InlineDiscussionPage(PageObject, DiscussionPageMixin):430 """431 Acceptance tests for inline discussions.432 """433 url = None434 def __init__(self, browser, discussion_id):435 super(InlineDiscussionPage, self).__init__(browser)436 self.root_selector = (437 ".discussion-module[data-discussion-id='{discussion_id}'] ".format(438 discussion_id=discussion_id439 )440 )441 def _find_within(self, selector):442 """443 Returns a query corresponding to the given CSS selector within the scope444 of this discussion page445 """446 return self.q(css=self.root_selector + " " + selector)447 def is_browser_on_page(self):448 self.wait_for_ajax()449 return self.q(css=self.root_selector).present450 def is_discussion_expanded(self):451 return self._find_within(".discussion").present452 def expand_discussion(self):453 """Click the link to expand the discussion"""454 self._find_within(".discussion-show").first.click()455 EmptyPromise(456 self.is_discussion_expanded,457 "Discussion expanded"458 ).fulfill()459 def get_num_displayed_threads(self):460 return len(self._find_within(".forum-nav-thread"))461 def element_exists(self, selector):462 return self.q(css=self.root_selector + " " + selector).present463 def click_element(self, selector):464 self.wait_for_element_presence(465 "{discussion} {selector}".format(discussion=self.root_selector, selector=selector),466 "{selector} is visible".format(selector=selector)467 )468 self._find_within(selector).click()469 @wait_for_js470 def _is_element_visible(self, selector):471 query = self._find_within(selector)472 return query.present and query.visible473 def show_thread(self, thread_id):474 """475 Clicks the link for the specified thread to show the detailed view.476 """477 thread_selector = ".forum-nav-thread[data-id='{thread_id}'] .forum-nav-thread-link".format(thread_id=thread_id)478 self._find_within(thread_selector).first.click()479 self.thread_page = InlineDiscussionThreadPage(self.browser, thread_id) # pylint: disable=attribute-defined-outside-init480 self.thread_page.wait_for_page()481class InlineDiscussionThreadPage(DiscussionThreadPage):482 """483 Page object to manipulate an individual thread view in an inline discussion.484 """485 def __init__(self, browser, thread_id):486 super(InlineDiscussionThreadPage, self).__init__(487 browser,488 ".discussion-module .discussion-article[data-id='{thread_id}']".format(thread_id=thread_id)489 )490 def is_thread_anonymous(self):491 return not self.q(css=".posted-details > .username").present492 @wait_for_js493 def check_if_selector_is_focused(self, selector):494 """495 Check if selector is focused496 """497 return is_focused_on_element(self.browser, selector)498class DiscussionUserProfilePage(CoursePage):499 TEXT_NEXT = u'Next >'500 TEXT_PREV = u'< Previous'501 PAGING_SELECTOR = ".discussion-pagination[data-page-number]"502 def __init__(self, browser, course_id, user_id, username, page=1):503 super(DiscussionUserProfilePage, self).__init__(browser, course_id)504 self.url_path = "discussion/forum/dummy/users/{}?page={}".format(user_id, page)505 self.username = username506 def is_browser_on_page(self):507 return (508 self.q(css='.discussion-user-threads[data-course-id="{}"]'.format(self.course_id)).present509 and510 self.q(css='.user-name').present511 and512 self.q(css='.user-name').text[0] == self.username513 )514 @wait_for_js515 def is_window_on_top(self):516 return self.browser.execute_script("return $('html, body').offset().top") == 0517 def get_shown_thread_ids(self):518 elems = self.q(css="li.forum-nav-thread")519 return [elem.get_attribute("data-id") for elem in elems]520 def click_on_sidebar_username(self):521 self.wait_for_page()522 self.q(css='.user-name').first.click()523 def get_user_roles(self):524 """Get user roles"""525 return self.q(css='.user-roles').text[0]526class DiscussionTabHomePage(CoursePage, DiscussionPageMixin):527 ALERT_SELECTOR = ".discussion-body .forum-nav .search-alert"528 def __init__(self, browser, course_id):529 super(DiscussionTabHomePage, self).__init__(browser, course_id)530 self.url_path = "discussion/forum/"531 self.root_selector = None532 def is_browser_on_page(self):533 return self.q(css=".discussion-body section.home-header").present534 def perform_search(self, text="dummy"):535 self.q(css=".search-input").fill(text + chr(10))536 EmptyPromise(537 self.is_ajax_finished,538 "waiting for server to return result"539 ).fulfill()540 def is_element_visible(self, selector):541 """542 Returns true if the element matching the specified selector is visible.543 """544 query = self.q(css=selector)545 return query.present and query.visible546 def is_checkbox_selected(self, selector):547 """548 Returns true or false depending upon the matching checkbox is checked.549 """550 return self.q(css=selector).selected551 def refresh_and_wait_for_load(self):552 """553 Refresh the page and wait for all resources to load.554 """...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful