From 4dffe2e7c54acac2c65197167ada87766f22a7fb Mon Sep 17 00:00:00 2001 From: eaccmk Date: Fri, 8 Sep 2023 21:43:38 -0700 Subject: [PATCH 1/3] Fix for #1333 - python alert automation example --- .../python/tests/interactions/test_alerts.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/python/tests/interactions/test_alerts.py diff --git a/examples/python/tests/interactions/test_alerts.py b/examples/python/tests/interactions/test_alerts.py new file mode 100644 index 00000000000..fe842814841 --- /dev/null +++ b/examples/python/tests/interactions/test_alerts.py @@ -0,0 +1,60 @@ +import time + +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + + +driver = webdriver.Chrome() + +driver.get("https://www.selenium.dev/selenium/web/alerts.html") + +def accept_alert_with_delay_in_seconds(driver, delay): + time.sleep(delay) + alert = driver.switch_to.alert + alert.accept() + + +def asert_and_accept_alert_with_delay_in_seconds(driver, delay, assertAlertText=None): + time.sleep(delay) + alert = driver.switch_to.alert + + if assertAlertText is not None: + assert alert.text == assertAlertText + + alert.accept() + + +driver.find_element(By.ID, "alert").click() +asert_and_accept_alert_with_delay_in_seconds(driver,1,"cheese") + +driver.find_element(By.ID, "empty-alert").click() +asert_and_accept_alert_with_delay_in_seconds(driver,1,"") + +driver.find_element(By.ID, "prompt").click() +accept_alert_with_delay_in_seconds(driver,1) + +driver.find_element(By.ID, "prompt-with-default").click() +accept_alert_with_delay_in_seconds(driver,1) + +driver.find_element(By.ID, "double-prompt").click() +accept_alert_with_delay_in_seconds(driver,1) +accept_alert_with_delay_in_seconds(driver,1) + +driver.find_element(By.ID, "slow-alert").click() +asert_and_accept_alert_with_delay_in_seconds(driver,1,"Slow") + +driver.find_element(By.ID, "confirm").click() +asert_and_accept_alert_with_delay_in_seconds(driver,1,"Are you sure?") +driver.back() + +# Does not work because of this bug https://github.com/SeleniumHQ/seleniumhq.github.io/issues/1469 +# driver.find_element(By.ID, "dialog").click() + +driver.find_element(By.ID, "open-page-with-onunload-alert").click() +driver.back() +time.sleep(0.5) + + +driver.quit() \ No newline at end of file From 2bbb9549b50c7dc628d3a0e91fabf6161e28d988 Mon Sep 17 00:00:00 2001 From: eaccmk Date: Mon, 11 Sep 2023 21:26:54 -0700 Subject: [PATCH 2/3] Add 3 example alerts in python interactions --- .../python/tests/interactions/test_alerts.py | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/examples/python/tests/interactions/test_alerts.py b/examples/python/tests/interactions/test_alerts.py index fe842814841..320927125e8 100644 --- a/examples/python/tests/interactions/test_alerts.py +++ b/examples/python/tests/interactions/test_alerts.py @@ -10,51 +10,49 @@ driver.get("https://www.selenium.dev/selenium/web/alerts.html") -def accept_alert_with_delay_in_seconds(driver, delay): - time.sleep(delay) - alert = driver.switch_to.alert - alert.accept() +# Click the link to activate the alert +driver.find_element(By.ID, "alert").click() +# Store the alert in a variable +alert = driver.switch_to.alert -def asert_and_accept_alert_with_delay_in_seconds(driver, delay, assertAlertText=None): - time.sleep(delay) - alert = driver.switch_to.alert +# Store the alert text in a variable +text = alert.text - if assertAlertText is not None: - assert alert.text == assertAlertText +#assert alert text +assert alert.text == "cheese" - alert.accept() +# Press the OK button +alert.accept() -driver.find_element(By.ID, "alert").click() -asert_and_accept_alert_with_delay_in_seconds(driver,1,"cheese") +# Click the link to activate the empty-alert driver.find_element(By.ID, "empty-alert").click() -asert_and_accept_alert_with_delay_in_seconds(driver,1,"") -driver.find_element(By.ID, "prompt").click() -accept_alert_with_delay_in_seconds(driver,1) +# Store the alert in a variable +alert = driver.switch_to.alert + +# Store the alert text in a variable +text = alert.text -driver.find_element(By.ID, "prompt-with-default").click() -accept_alert_with_delay_in_seconds(driver,1) +#assert alert text +assert alert.text == "" -driver.find_element(By.ID, "double-prompt").click() -accept_alert_with_delay_in_seconds(driver,1) -accept_alert_with_delay_in_seconds(driver,1) +# Press the OK button +alert.accept() -driver.find_element(By.ID, "slow-alert").click() -asert_and_accept_alert_with_delay_in_seconds(driver,1,"Slow") -driver.find_element(By.ID, "confirm").click() -asert_and_accept_alert_with_delay_in_seconds(driver,1,"Are you sure?") -driver.back() -# Does not work because of this bug https://github.com/SeleniumHQ/seleniumhq.github.io/issues/1469 -# driver.find_element(By.ID, "dialog").click() +# Click the link to activate the prompt +driver.find_element(By.ID, "prompt").click() + +# Store the alert in a variable +alert = driver.switch_to.alert + +# Press the Cancel button +alert.dismiss() -driver.find_element(By.ID, "open-page-with-onunload-alert").click() -driver.back() -time.sleep(0.5) driver.quit() \ No newline at end of file From f9d12557814a9f71083d4c5352305751307b3e62 Mon Sep 17 00:00:00 2001 From: eaccmk Date: Sat, 16 Sep 2023 19:33:12 -0700 Subject: [PATCH 3/3] Remove comments and used 3 exampls to match doco --- .../python/tests/interactions/test_alerts.py | 48 +++++-------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/examples/python/tests/interactions/test_alerts.py b/examples/python/tests/interactions/test_alerts.py index 35f0096e38a..81caa8773dd 100644 --- a/examples/python/tests/interactions/test_alerts.py +++ b/examples/python/tests/interactions/test_alerts.py @@ -1,58 +1,32 @@ -import time - from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC - +from selenium.webdriver.common.alert import Alert +from selenium.webdriver.support import expected_conditions driver = webdriver.Chrome() +wait = WebDriverWait(driver, timeout=2) driver.get("https://www.selenium.dev/selenium/web/alerts.html") -# Click the link to activate the alert driver.find_element(By.ID, "alert").click() - -# Store the alert in a variable -alert = driver.switch_to.alert - -# Store the alert text in a variable +alert = wait.until(expected_conditions.alert_is_present()) text = alert.text - -#assert alert text -assert alert.text == "cheese" - -# Press the OK button alert.accept() - -# Click the link to activate the empty-alert -driver.find_element(By.ID, "empty-alert").click() - -# Store the alert in a variable +driver.find_element(By.LINK_TEXT, "test confirm").click() +wait.until(expected_conditions.alert_is_present()) alert = driver.switch_to.alert - -# Store the alert text in a variable text = alert.text - -#assert alert text -assert alert.text == "" - -# Press the OK button -alert.accept() - +alert.dismiss() -# Click the link to activate the prompt driver.find_element(By.ID, "prompt").click() - -# Store the alert in a variable -alert = driver.switch_to.alert - -# Press the Cancel button -alert.dismiss() - +wait.until(expected_conditions.alert_is_present()) +alert = Alert(driver) +alert.send_keys("Selenium") +alert.accept() driver.quit()