Path: blob/trunk/py/test/selenium/webdriver/common/alerts_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import sys1819import pytest2021from selenium.common.exceptions import (22InvalidElementStateException,23NoAlertPresentException,24UnexpectedAlertPresentException,25)26from selenium.webdriver.common.by import By27from selenium.webdriver.support import expected_conditions as EC28from selenium.webdriver.support.wait import WebDriverWait293031@pytest.fixture(autouse=True)32def close_alert(driver):33yield34try:35driver.switch_to.alert.dismiss()36except Exception:37pass383940def test_should_be_able_to_override_the_window_alert_method(driver, pages):41pages.load("alerts.html")42driver.execute_script("window.alert = function(msg) { document.getElementById('text').innerHTML = msg; }")43driver.find_element(by=By.ID, value="alert").click()44try:45assert driver.find_element(By.ID, "text").text == "cheese"46except Exception as e:47# if we're here, likely the alert is displayed48# not dismissing it will affect other tests49try:50_wait_for_alert(driver).dismiss()51except Exception:52pass53raise e545556def test_should_allow_users_to_accept_an_alert_manually(driver, pages):57pages.load("alerts.html")58driver.find_element(by=By.ID, value="alert").click()59alert = _wait_for_alert(driver)60alert.accept()61# If we can perform any action, we're good to go62assert "Testing Alerts" == driver.title636465def test_should_allow_users_to_accept_an_alert_with_no_text_manually(driver, pages):66pages.load("alerts.html")67driver.find_element(By.ID, "empty-alert").click()68alert = _wait_for_alert(driver)69alert.accept()7071# If we can perform any action, we're good to go72assert "Testing Alerts" == driver.title737475def test_should_get_text_of_alert_opened_in_set_timeout(driver, pages):76pages.load("alerts.html")77driver.find_element(By.ID, "slow-alert").click()7879# DO NOT WAIT OR SLEEP HERE80# This is a regression test for a bug where only the first switchTo call would throw,81# and only if it happens before the alert actually loads.8283alert = _wait_for_alert(driver)84try:85assert "Slow" == alert.text86finally:87alert.accept()888990def test_should_allow_users_to_dismiss_an_alert_manually(driver, pages):91pages.load("alerts.html")92driver.find_element(by=By.ID, value="alert").click()93alert = _wait_for_alert(driver)94alert.dismiss()95# If we can perform any action, we're good to go96assert "Testing Alerts" == driver.title979899def test_should_allow_auser_to_accept_aprompt(driver, pages):100pages.load("alerts.html")101driver.find_element(by=By.ID, value="prompt").click()102alert = _wait_for_alert(driver)103alert.accept()104105# If we can perform any action, we're good to go106assert "Testing Alerts" == driver.title107108109def test_should_allow_auser_to_dismiss_aprompt(driver, pages):110pages.load("alerts.html")111driver.find_element(by=By.ID, value="prompt").click()112alert = _wait_for_alert(driver)113alert.dismiss()114115# If we can perform any action, we're good to go116assert "Testing Alerts" == driver.title117118119def test_should_allow_auser_to_set_the_value_of_aprompt(driver, pages):120pages.load("alerts.html")121driver.find_element(by=By.ID, value="prompt").click()122alert = _wait_for_alert(driver)123alert.send_keys("cheese")124alert.accept()125126result = driver.find_element(by=By.ID, value="text").text127assert "cheese" == result128129130@pytest.mark.xfail_firefox131@pytest.mark.xfail_remote132def test_setting_the_value_of_an_alert_throws(driver, pages):133pages.load("alerts.html")134driver.find_element(By.ID, "alert").click()135136alert = _wait_for_alert(driver)137with pytest.raises(InvalidElementStateException):138alert.send_keys("cheese")139alert.accept()140141142@pytest.mark.xfail_chrome(143condition=sys.platform == "darwin", reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=26", run=False144)145@pytest.mark.xfail_edge(146condition=sys.platform == "darwin", reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=26", run=False147)148def test_alert_should_not_allow_additional_commands_if_dimissed(driver, pages):149pages.load("alerts.html")150driver.find_element(By.ID, "alert").click()151152alert = _wait_for_alert(driver)153alert.dismiss()154155with pytest.raises(NoAlertPresentException):156alert.text157158159@pytest.mark.xfail_firefox(reason="Fails on travis")160@pytest.mark.xfail_remote(reason="Fails on travis")161@pytest.mark.xfail_safari162def test_should_allow_users_to_accept_an_alert_in_aframe(driver, pages):163pages.load("alerts.html")164driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithAlert"))165driver.find_element(By.ID, "alertInFrame").click()166167alert = _wait_for_alert(driver)168alert.accept()169170assert "Testing Alerts" == driver.title171172173@pytest.mark.xfail_firefox(reason="Fails on travis")174@pytest.mark.xfail_remote(reason="Fails on travis")175@pytest.mark.xfail_safari176def test_should_allow_users_to_accept_an_alert_in_anested_frame(driver, pages):177pages.load("alerts.html")178driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithIframe"))179driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithAlert"))180181driver.find_element(By.ID, "alertInFrame").click()182183alert = _wait_for_alert(driver)184alert.accept()185186assert "Testing Alerts" == driver.title187188189@pytest.mark.xfail_safari190def test_should_throw_an_exception_if_an_alert_has_not_been_dealt_with_and_dismiss_the_alert(driver, pages):191pages.load("alerts.html")192driver.find_element(By.ID, "alert").click()193WebDriverWait(driver, 5).until(EC.alert_is_present())194195with pytest.raises(UnexpectedAlertPresentException):196driver.find_element(By.ID, "select").click()197# Alert would be dismissed automatically198199200def test_prompt_should_use_default_value_if_no_keys_sent(driver, pages):201pages.load("alerts.html")202driver.find_element(By.ID, "prompt-with-default").click()203204alert = _wait_for_alert(driver)205alert.accept()206207txt = driver.find_element(By.ID, "text").text208assert "This is a default value" == txt209210211def test_prompt_should_have_null_value_if_dismissed(driver, pages):212pages.load("alerts.html")213driver.find_element(By.ID, "prompt-with-default").click()214alert = _wait_for_alert(driver)215alert.dismiss()216217assert "null" == driver.find_element(By.ID, "text").text218219220def test_handles_two_alerts_from_one_interaction(driver, pages):221pages.load("alerts.html")222223driver.find_element(By.ID, "double-prompt").click()224225alert1 = _wait_for_alert(driver)226alert1.send_keys("brie")227alert1.accept()228229alert2 = _wait_for_alert(driver)230alert2.send_keys("cheddar")231alert2.accept()232233assert driver.find_element(By.ID, "text1").text == "brie"234assert driver.find_element(By.ID, "text2").text == "cheddar"235236237@pytest.mark.xfail_safari238def test_should_handle_alert_on_page_load(driver, pages):239pages.load("alerts.html")240driver.find_element(By.ID, "open-page-with-onload-alert").click()241alert = _wait_for_alert(driver)242value = alert.text243alert.accept()244assert "onload" == value245246247def test_should_handle_alert_on_page_load_using_get(driver, pages):248pages.load("pageWithOnLoad.html")249alert = _wait_for_alert(driver)250value = alert.text251alert.accept()252253assert "onload" == value254WebDriverWait(driver, 3).until(255EC.text_to_be_present_in_element((By.TAG_NAME, "p"), "Page with onload event handler")256)257258259@pytest.mark.xfail_chrome(reason="Non W3C conformant")260@pytest.mark.xfail_edge(reason="Non W3C conformant")261def test_should_handle_alert_on_page_before_unload(driver, pages):262pages.load("pageWithOnBeforeUnloadMessage.html")263264element = driver.find_element(By.ID, "navigate")265element.click()266WebDriverWait(driver, 3).until(EC.title_is("Testing Alerts"))267268269def test_should_allow_the_user_to_get_the_text_of_an_alert(driver, pages):270pages.load("alerts.html")271driver.find_element(by=By.ID, value="alert").click()272alert = _wait_for_alert(driver)273value = alert.text274alert.accept()275assert "cheese" == value276277278def test_should_allow_the_user_to_get_the_text_of_aprompt(driver, pages):279pages.load("alerts.html")280driver.find_element(By.ID, "prompt").click()281282alert = _wait_for_alert(driver)283value = alert.text284alert.accept()285286assert "Enter something" == value287288289def test_alert_should_not_allow_additional_commands_if_dismissed(driver, pages):290pages.load("alerts.html")291driver.find_element(By.ID, "alert").click()292293alert = _wait_for_alert(driver)294alert.accept()295296with pytest.raises(NoAlertPresentException):297alert.text298299300@pytest.mark.xfail_firefox(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1279211")301@pytest.mark.xfail_remote(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1279211")302@pytest.mark.xfail_chrome303@pytest.mark.xfail_edge304@pytest.mark.xfail_safari305def test_unexpected_alert_present_exception_contains_alert_text(driver, pages):306pages.load("alerts.html")307driver.find_element(by=By.ID, value="alert").click()308alert = _wait_for_alert(driver)309value = alert.text310with pytest.raises(UnexpectedAlertPresentException) as e:311pages.load("simpleTest.html")312assert value == e.value.alert_text313assert f"Alert Text: {value}" in str(e)314315316def _wait_for_alert(driver):317return WebDriverWait(driver, 3).until(EC.alert_is_present())318319320