Path: blob/trunk/py/test/selenium/webdriver/common/form_handling_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 pytest1819from selenium.common.exceptions import WebDriverException20from selenium.webdriver.common.by import By21from selenium.webdriver.support import expected_conditions as EC22from selenium.webdriver.support.ui import WebDriverWait232425def test_should_click_on_submit_input_elements(driver, pages):26pages.load("formPage.html")27driver.find_element(By.ID, "submitButton").click()28WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))293031def test_clicking_on_unclickable_elements_does_nothing(driver, pages):32pages.load("formPage.html")33driver.find_element(By.XPATH, "//body").click()343536def test_should_be_able_to_click_image_buttons(driver, pages):37pages.load("formPage.html")38driver.find_element(By.ID, "imageButton").click()39WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))404142def test_should_submit_input_in_form(driver, pages):43pages.load("formPage.html")44driver.find_element(By.NAME, "login").submit()45WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))464748def test_should_submit_any_input_element_within_form(driver, pages):49pages.load("formPage.html")50driver.find_element(By.ID, "checky").submit()51WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))525354def test_should_submit_any_element_within_form(driver, pages):55pages.load("formPage.html")56driver.find_element(By.XPATH, "//form/p").submit()57WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))585960def test_should_submit_element_with_id_submit(driver, pages):61pages.load("formPage.html")62driver.find_element(By.ID, "submit").submit()63WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))646566def test_should_submit_element_with_name_submit(driver, pages):67pages.load("formPage.html")68driver.find_element(By.NAME, "submit").submit()69WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))707172def test_should_not_submit_button_outside_form(driver, pages):73pages.load("formPage.html")74with pytest.raises(WebDriverException):75driver.find_element(By.NAME, "SearchableText").submit()767778def test_should_be_able_to_enter_text_into_atext_area_by_setting_its_value(driver, pages):79pages.load("javascriptPage.html")80textarea = driver.find_element(By.ID, "keyUpArea")81cheesey = "Brie and cheddar"82textarea.send_keys(cheesey)83assert textarea.get_attribute("value") == cheesey848586def test_should_enter_data_into_form_fields(driver, pages):87pages.load("xhtmlTest.html")88element = driver.find_element(By.XPATH, "//form[@name='someForm']/input[@id='username']")89originalValue = element.get_attribute("value")90assert originalValue == "change"9192element.clear()93element.send_keys("some text")9495element = driver.find_element(By.XPATH, "//form[@name='someForm']/input[@id='username']")96newFormValue = element.get_attribute("value")97assert newFormValue == "some text"9899100def test_should_be_able_to_select_acheck_box(driver, pages):101pages.load("formPage.html")102checkbox = driver.find_element(By.ID, "checky")103assert checkbox.is_selected() is False104checkbox.click()105assert checkbox.is_selected() is True106checkbox.click()107assert checkbox.is_selected() is False108109110def test_should_toggle_the_checked_state_of_acheckbox(driver, pages):111pages.load("formPage.html")112checkbox = driver.find_element(By.ID, "checky")113assert checkbox.is_selected() is False114checkbox.click()115assert checkbox.is_selected() is True116checkbox.click()117assert checkbox.is_selected() is False118119120def test_toggling_acheckbox_should_return_its_current_state(driver, pages):121pages.load("formPage.html")122checkbox = driver.find_element(By.ID, "checky")123assert checkbox.is_selected() is False124checkbox.click()125assert checkbox.is_selected() is True126checkbox.click()127assert checkbox.is_selected() is False128129130def test_should_be_able_to_select_aradio_button(driver, pages):131pages.load("formPage.html")132radioButton = driver.find_element(By.ID, "peas")133assert radioButton.is_selected() is False134radioButton.click()135assert radioButton.is_selected() is True136137138def test_should_be_able_to_select_aradio_button_by_clicking_on_it(driver, pages):139pages.load("formPage.html")140radioButton = driver.find_element(By.ID, "peas")141assert radioButton.is_selected() is False142radioButton.click()143assert radioButton.is_selected() is True144145146def test_should_return_state_of_radio_buttons_before_interaction(driver, pages):147pages.load("formPage.html")148radioButton = driver.find_element(By.ID, "cheese_and_peas")149assert radioButton.is_selected() is True150151radioButton = driver.find_element(By.ID, "cheese")152assert radioButton.is_selected() is False153154155def test_toggling_an_option_should_toggle_options_in_amulti_select(driver, pages):156pages.load("formPage.html")157158select = driver.find_element(By.NAME, "multi")159option = select.find_elements(By.TAG_NAME, "option")[0]160161selected = option.is_selected()162option.click()163assert not selected == option.is_selected()164165option.click()166assert selected == option.is_selected()167168169def test_should_throw_an_exception_when_selecting_an_unselectable_element(driver, pages):170pages.load("formPage.html")171element = driver.find_element(By.XPATH, "//title")172with pytest.raises(WebDriverException):173element.click()174175176def test_sending_keyboard_events_should_append_text_in_inputs(driver, pages):177pages.load("formPage.html")178element = driver.find_element(By.ID, "working")179element.send_keys("Some")180value = element.get_attribute("value")181assert value == "Some"182183element.send_keys(" text")184value = element.get_attribute("value")185assert value == "Some text"186187188def test_should_be_able_to_clear_text_from_input_elements(driver, pages):189pages.load("formPage.html")190element = driver.find_element(By.ID, "working")191element.send_keys("Some text")192value = element.get_attribute("value")193assert len(value) > 0194195element.clear()196value = element.get_attribute("value")197assert len(value) == 0198199200def test_empty_text_boxes_should_return_an_empty_string_not_null(driver, pages):201pages.load("formPage.html")202emptyTextBox = driver.find_element(By.ID, "working")203assert emptyTextBox.get_attribute("value") == ""204205emptyTextArea = driver.find_element(By.ID, "emptyTextArea")206assert emptyTextArea.get_attribute("value") == ""207208209def test_should_be_able_to_clear_text_from_text_areas(driver, pages):210pages.load("formPage.html")211element = driver.find_element(By.ID, "withText")212element.send_keys("Some text")213value = element.get_attribute("value")214assert len(value) > 0215216element.clear()217value = element.get_attribute("value")218assert len(value) == 0219220221def test_radio_should_not_be_selected_after_selecting_sibling(driver, pages):222pages.load("formPage.html")223cheese = driver.find_element(By.ID, "cheese")224peas = driver.find_element(By.ID, "peas")225226cheese.click()227assert cheese.is_selected() is True228assert peas.is_selected() is False229230peas.click()231assert cheese.is_selected() is False232assert peas.is_selected() is True233234235