How to use switch_to_alert method in SeleniumBase

Best Python code snippet using SeleniumBase

test_modal_dialogs.py

Source:test_modal_dialogs.py Github

copy

Full Screen

...24 self.assertRaises(NoAlertPresentException, Alert(self.marionette).dismiss)25 def test_alert_accept(self):26 self.marionette.find_element('id', 'modal-alert').click()27 self.wait_for_alert()28 alert = self.marionette.switch_to_alert()29 alert.accept()30 def test_alert_dismiss(self):31 self.marionette.find_element('id', 'modal-alert').click()32 self.wait_for_alert()33 alert = self.marionette.switch_to_alert()34 alert.dismiss()35 def test_confirm_accept(self):36 self.marionette.find_element('id', 'modal-confirm').click()37 self.wait_for_alert()38 alert = self.marionette.switch_to_alert()39 alert.accept()40 self.wait_for_condition(lambda mn: mn.find_element('id', 'confirm-result').text == 'true')41 def test_confirm_dismiss(self):42 self.marionette.find_element('id', 'modal-confirm').click()43 self.wait_for_alert()44 alert = self.marionette.switch_to_alert()45 alert.dismiss()46 self.wait_for_condition(lambda mn: mn.find_element('id', 'confirm-result').text == 'false')47 def test_prompt_accept(self):48 self.marionette.find_element('id', 'modal-prompt').click()49 self.wait_for_alert()50 alert = self.marionette.switch_to_alert()51 alert.accept()52 self.wait_for_condition(lambda mn: mn.find_element('id', 'prompt-result').text == '')53 def test_prompt_dismiss(self):54 self.marionette.find_element('id', 'modal-prompt').click()55 self.wait_for_alert()56 alert = self.marionette.switch_to_alert()57 alert.dismiss()58 self.wait_for_condition(lambda mn: mn.find_element('id', 'prompt-result').text == 'null')59 def test_alert_text(self):60 with self.assertRaises(NoAlertPresentException):61 alert = self.marionette.switch_to_alert()62 alert.text63 self.marionette.find_element('id', 'modal-alert').click()64 self.wait_for_alert()65 alert = self.marionette.switch_to_alert()66 self.assertEqual(alert.text, 'Marionette alert')67 alert.accept()68 def test_prompt_text(self):69 with self.assertRaises(NoAlertPresentException):70 alert = self.marionette.switch_to_alert()71 alert.text72 self.marionette.find_element('id', 'modal-prompt').click()73 self.wait_for_alert()74 alert = self.marionette.switch_to_alert()75 self.assertEqual(alert.text, 'Marionette prompt')76 alert.accept()77 def test_confirm_text(self):78 with self.assertRaises(NoAlertPresentException):79 alert = self.marionette.switch_to_alert()80 alert.text81 self.marionette.find_element('id', 'modal-confirm').click()82 self.wait_for_alert()83 alert = self.marionette.switch_to_alert()84 self.assertEqual(alert.text, 'Marionette confirm')85 alert.accept()86 def test_set_text_throws(self):87 self.assertRaises(NoAlertPresentException, Alert(self.marionette).send_keys, "Foo")88 self.marionette.find_element('id', 'modal-alert').click()89 self.wait_for_alert()90 alert = self.marionette.switch_to_alert()91 self.assertRaises(ElementNotVisibleException, alert.send_keys, "Foo")92 alert.accept()93 def test_set_text_accept(self):94 self.marionette.find_element('id', 'modal-prompt').click()95 self.wait_for_alert()96 alert = self.marionette.switch_to_alert()97 alert.send_keys("Some text!");98 alert.accept()99 self.wait_for_condition(lambda mn: mn.find_element('id', 'prompt-result').text == 'Some text!')100 def test_set_text_dismiss(self):101 self.marionette.find_element('id', 'modal-prompt').click()102 self.wait_for_alert()103 alert = self.marionette.switch_to_alert()104 alert.send_keys("Some text!");105 alert.dismiss()106 self.wait_for_condition(lambda mn: mn.find_element('id', 'prompt-result').text == 'null')107 def test_onbeforeunload_dismiss(self):108 start_url = self.marionette.get_url()109 self.marionette.find_element('id', 'onbeforeunload-handler').click()110 self.wait_for_condition(111 lambda mn: mn.execute_script("""112 return window.onbeforeunload !== null;113 """))114 self.marionette.navigate("about:blank")115 self.wait_for_alert()116 alert = self.marionette.switch_to_alert()117 self.assertTrue(alert.text.startswith("This page is asking you to confirm"))118 alert.dismiss()119 self.assertTrue(self.marionette.get_url().startswith(start_url))120 def test_onbeforeunload_accept(self):121 self.marionette.find_element('id', 'onbeforeunload-handler').click()122 self.wait_for_condition(123 lambda mn: mn.execute_script("""124 return window.onbeforeunload !== null;125 """))126 self.marionette.navigate("about:blank")127 self.wait_for_alert()128 alert = self.marionette.switch_to_alert()129 self.assertTrue(alert.text.startswith("This page is asking you to confirm"))130 alert.accept()131 self.wait_for_condition(lambda mn: mn.get_url() == "about:blank")132 @skip_if_e10s133 def test_unrelated_command_when_alert_present(self):134 click_handler = self.marionette.find_element('id', 'click-handler')135 text = self.marionette.find_element('id', 'click-result').text136 self.assertEqual(text, '')137 self.marionette.find_element('id', 'modal-alert').click()138 self.wait_for_alert()139 140 141 text = self.marionette.find_element('id', 'click-result').text142 self.assertEqual(text, '')143 click_handler.click()144 text = self.marionette.find_element('id', 'click-result').text145 self.assertEqual(text, '')146 alert = self.marionette.switch_to_alert()147 alert.accept()148 Wait(self.marionette).until(lambda _: not self.alert_present())149 click_handler.click()150 text = self.marionette.find_element('id', 'click-result').text151 self.assertEqual(text, 'result')152class TestGlobalModals(TestTabModals):153 def setUp(self):154 MarionetteTestCase.setUp(self)155 self.marionette.enforce_gecko_prefs({"prompts.tab_modal.enabled": False})156 self.marionette.navigate(self.marionette.absolute_url('modal_dialogs.html'))157 def test_unrelated_command_when_alert_present(self):158 159 160 pass

Full Screen

Full Screen

alerts_test.py

Source:alerts_test.py Github

copy

Full Screen

...9 self.wait = wait.WebDriverWait(self.driver, 5, ignored_exceptions = [exceptions.NoSuchAlertException])10 self.driver.get(self.webserver.where_is('modal/res/alerts.html'))11 def tearDown(self):12 try:13 self.driver.switch_to_alert().dismiss()14 except exceptions.NoSuchAlertException:15 pass16 17 def test_should_allow_user_to_accept_an_alert(self):18 self.driver.find_element_by_css('#alert').click()19 alert = self.wait.until(lambda x: x.switch_to_alert())20 alert.accept()21 self.driver.get_current_url()22 def test_should_allow_user_to_accept_an_alert_with_no_text(self):23 self.driver.find_element_by_css('#empty-alert').click()24 alert = self.wait.until(lambda x: x.switch_to_alert())25 alert.accept()26 self.driver.get_current_url()27 def test_should_allow_user_to_dismiss_an_alert(self):28 self.driver.find_element_by_css('#alert').click()29 alert = self.wait.until(lambda x: x.switch_to_alert())30 alert.dismiss()31 self.driver.get_current_url()32 def test_should_allow_user_to_get_text_of_an_alert(self):33 self.driver.find_element_by_css('#alert').click()34 alert = self.wait.until(lambda x: x.switch_to_alert())35 value = alert.get_text()36 alert.accept()37 self.assertEquals('cheese', value)38 def test_setting_the_value_of_an_alert_throws(self):39 self.driver.find_element_by_css('#alert').click()40 alert = self.wait.until(lambda x: x.switch_to_alert())41 with self.assertRaises(exceptions.ElementNotVisibleException):42 alert.send_keys('cheese')43 alert.accept()44 def test_alert_should_not_allow_additional_commands_if_dismissed(self):45 self.driver.find_element_by_css('#alert').click()46 alert = self.wait.until(lambda x: x.switch_to_alert())47 alert.accept()48 with self.assertRaises(exceptions.NoSuchAlertException):49 alert.get_text()50 51 def test_should_allow_user_to_accept_a_prompt(self):52 self.driver.find_element_by_css('#prompt').click()53 alert = self.wait.until(lambda x: x.switch_to_alert())54 alert.accept()55 self.wait.until(lambda x: x.find_element_by_css('#text').text == '')56 def test_should_allow_user_to_dismiss_a_prompt(self):57 self.driver.find_element_by_css('#prompt').click()58 alert = self.wait.until(lambda x: x.switch_to_alert())59 alert.dismiss()60 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'null')61 def test_should_allow_user_to_set_the_value_of_a_prompt(self):62 self.driver.find_element_by_css('#prompt').click()63 alert = self.wait.until(lambda x: x.switch_to_alert())64 alert.send_keys('cheese')65 alert.accept()66 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'cheese')67 def test_should_allow_user_to_get_text_of_a_prompt(self):68 self.driver.find_element_by_css('#prompt').click()69 alert = self.wait.until(lambda x: x.switch_to_alert())70 value = alert.get_text()71 alert.accept()72 self.assertEquals('Enter something', value)73 def test_prompt_should_not_allow_additional_commands_if_dismissed(self):74 self.driver.find_element_by_css('#prompt').click()75 alert = self.wait.until(lambda x: x.switch_to_alert())76 alert.accept()77 with self.assertRaises(exceptions.NoSuchAlertException):78 alert.get_text()79 def test_prompt_should_use_default_value_if_no_keys_sent(self):80 self.driver.find_element_by_css('#prompt-with-default').click()81 alert = self.wait.until(lambda x: x.switch_to_alert())82 alert.accept()83 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'This is a default value')84 def test_prompt_should_have_null_value_if_dismissed(self):85 self.driver.find_element_by_css('#prompt-with-default').click()86 alert = self.wait.until(lambda x: x.switch_to_alert())87 alert.dismiss()88 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'null')89 90 def test_should_allow_user_to_accept_a_confirm(self):91 self.driver.find_element_by_css('#confirm').click()92 alert = self.wait.until(lambda x: x.switch_to_alert())93 alert.accept()94 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'true')95 def test_should_allow_user_to_dismiss_a_confirm(self):96 self.driver.find_element_by_css('#confirm').click()97 alert = self.wait.until(lambda x: x.switch_to_alert())98 alert.dismiss()99 self.wait.until(lambda x: x.find_element_by_css('#text').text == 'false')100 def test_setting_the_value_of_a_confirm_throws(self):101 self.driver.find_element_by_css('#confirm').click()102 alert = self.wait.until(lambda x: x.switch_to_alert())103 with self.assertRaises(exceptions.ElementNotVisibleException):104 alert.send_keys('cheese')105 alert.accept()106 def test_should_allow_user_to_get_text_of_a_confirm(self):107 self.driver.find_element_by_css('#confirm').click()108 alert = self.wait.until(lambda x: x.switch_to_alert())109 value = alert.get_text()110 alert.accept()111 self.assertEquals('cheese', value)112 def test_confirm_should_not_allow_additional_commands_if_dismissed(self):113 self.driver.find_element_by_css('#confirm').click()114 alert = self.wait.until(lambda x: x.switch_to_alert())115 alert.accept()116 with self.assertRaises(exceptions.NoSuchAlertException):117 alert.get_text()118 def test_switch_to_missing_alert_fails(self):119 with self.assertRaises(exceptions.NoSuchAlertException):120 self.driver.switch_to_alert()121if __name__ == '__main__':...

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