Path: blob/trunk/java/test/org/openqa/selenium/FormHandlingTest.java
3991 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.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;22import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;23import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;24import static org.openqa.selenium.support.ui.ExpectedConditions.urlContains;25import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;26import static org.openqa.selenium.testing.drivers.Browser.IE;27import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2829import java.io.File;30import java.io.IOException;31import org.junit.jupiter.api.Test;32import org.openqa.selenium.environment.webserver.Page;33import org.openqa.selenium.testing.Ignore;34import org.openqa.selenium.testing.JupiterTestBase;35import org.openqa.selenium.testing.NotYetImplemented;3637class FormHandlingTest extends JupiterTestBase {3839@Test40void testShouldClickOnSubmitInputElements() {41driver.get(pages.formPage);42driver.findElement(By.id("submitButton")).click();43wait.until(titleIs("We Arrive Here"));44assertThat(driver.getTitle()).isEqualTo("We Arrive Here");45}4647@Test48void testClickingOnUnclickableElementsDoesNothing() {49driver.get(pages.formPage);50driver.findElement(By.xpath("//body")).click();51}5253@Test54void testShouldBeAbleToClickImageButtons() {55driver.get(pages.formPage);56driver.findElement(By.id("imageButton")).click();57wait.until(titleIs("We Arrive Here"));58assertThat(driver.getTitle()).isEqualTo("We Arrive Here");59}6061@Test62void testShouldBeAbleToSubmitForms() {63driver.get(pages.formPage);64driver.findElement(By.name("login")).submit();65wait.until(titleIs("We Arrive Here"));66}6768@Test69void testShouldSubmitAFormWhenAnyInputElementWithinThatFormIsSubmitted() {70driver.get(pages.formPage);71driver.findElement(By.id("checky")).submit();72wait.until(titleIs("We Arrive Here"));73}7475@Test76void testShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted() {77driver.get(pages.formPage);78driver.findElement(By.xpath("//form/p")).submit();79wait.until(titleIs("We Arrive Here"));80}8182@Test83void testShouldNotBeAbleToSubmitAFormThatDoesNotExist() {84driver.get(pages.formPage);85WebElement element = driver.findElement(By.name("SearchableText"));86assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(element::submit);87}8889@Test90void testShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue() {91driver.get(pages.javascriptPage);92WebElement textarea = driver.findElement(By.id("keyUpArea"));93String cheesy = "brie and cheddar";94textarea.sendKeys(cheesy);95assertThat(textarea.getAttribute("value")).isEqualTo(cheesy);96}9798@Test99void testSendKeysKeepsCapitalization() {100driver.get(pages.javascriptPage);101WebElement textarea = driver.findElement(By.id("keyUpArea"));102String cheesey = "BrIe And CheDdar";103textarea.sendKeys(cheesey);104assertThat(textarea.getAttribute("value")).isEqualTo(cheesey);105}106107@Test108@NotYetImplemented(FIREFOX)109@NotYetImplemented(SAFARI)110public void testShouldSubmitAFormUsingTheNewlineLiteral() {111driver.get(pages.formPage);112WebElement nestedForm = driver.findElement(By.id("nested_form"));113WebElement input = nestedForm.findElement(By.name("x"));114input.sendKeys("\n");115wait.until(titleIs("We Arrive Here"));116assertThat(driver.getCurrentUrl()).endsWith("?x=name");117}118119@Test120void testShouldSubmitAFormUsingTheEnterKey() {121driver.get(pages.formPage);122WebElement nestedForm = driver.findElement(By.id("nested_form"));123WebElement input = nestedForm.findElement(By.name("x"));124input.sendKeys(Keys.ENTER);125wait.until(titleIs("We Arrive Here"));126assertThat(driver.getCurrentUrl()).endsWith("?x=name");127}128129@Test130void testShouldEnterDataIntoFormFields() {131driver.get(pages.xhtmlTestPage);132WebElement element =133driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));134String originalValue = element.getAttribute("value");135assertThat(originalValue).isEqualTo("change");136137element.clear();138element.sendKeys("some text");139140element = driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));141String newFormValue = element.getAttribute("value");142assertThat(newFormValue).isEqualTo("some text");143}144145@Test146void testShouldBeAbleToAlterTheContentsOfAFileUploadInputElement() throws IOException {147driver.get(pages.formPage);148WebElement uploadElement = driver.findElement(By.id("upload"));149assertThat(uploadElement.getAttribute("value")).isEmpty();150151File file = File.createTempFile("test", "txt");152file.deleteOnExit();153154uploadElement.sendKeys(file.getAbsolutePath());155156String uploadPath = uploadElement.getAttribute("value");157assertThat(uploadPath).endsWith(file.getName());158}159160@Test161void testShouldBeAbleToSendKeysToAFileUploadInputElementInAnXhtmlDocument() throws IOException {162driver.get(pages.xhtmlFormPage);163WebElement uploadElement = driver.findElement(By.id("file"));164assertThat(uploadElement.getAttribute("value")).isEmpty();165166File file = File.createTempFile("test", "txt");167file.deleteOnExit();168169uploadElement.sendKeys(file.getAbsolutePath());170171String uploadPath = uploadElement.getAttribute("value");172assertThat(uploadPath).endsWith(file.getName());173}174175@Test176@Ignore(value = SAFARI, reason = "Hanging")177public void testShouldBeAbleToUploadTheSameFileTwice() throws IOException {178File file = File.createTempFile("test", "txt");179file.deleteOnExit();180181driver.get(pages.formPage);182WebElement uploadElement = driver.findElement(By.id("upload"));183assertThat(uploadElement.getAttribute("value")).isEmpty();184185uploadElement.sendKeys(file.getAbsolutePath());186uploadElement.submit();187188// Apparently on chrome we need to wait for the element to be gone if we're loading the same189// page again190wait.until(d -> d.findElements(By.id("upload")).isEmpty());191192driver.get(pages.formPage);193uploadElement = driver.findElement(By.id("upload"));194assertThat(uploadElement.getAttribute("value")).isEmpty();195196uploadElement.sendKeys(file.getAbsolutePath());197uploadElement.submit();198199// If we get this far, then we're all good.200}201202@Test203void testSendingKeyboardEventsShouldAppendTextInInputs() {204driver.get(pages.formPage);205WebElement element = driver.findElement(By.id("working"));206element.sendKeys("some");207String value = element.getAttribute("value");208assertThat(value).isEqualTo("some");209210element.sendKeys(" text");211value = element.getAttribute("value");212assertThat(value).isEqualTo("some text");213}214215@Test216@NotYetImplemented(SAFARI)217public void testSendingKeyboardEventsShouldAppendTextInInputsWithExistingValue() {218driver.get(pages.formPage);219WebElement element = wait.until(presenceOfElementLocated(By.id("inputWithText")));220element.sendKeys(". Some text");221String value = element.getAttribute("value");222223assertThat(value).isEqualTo("Example text. Some text");224}225226@Test227@NotYetImplemented(SAFARI)228public void testSendingKeyboardEventsShouldAppendTextInTextAreas() {229driver.get(pages.formPage);230WebElement element = driver.findElement(By.id("withText"));231232element.sendKeys(". Some text");233String value = element.getAttribute("value");234235assertThat(value).isEqualTo("Example text. Some text");236}237238@Test239void testEmptyTextBoxesShouldReturnAnEmptyStringNotNull() {240driver.get(pages.formPage);241WebElement emptyTextBox = driver.findElement(By.id("working"));242assertThat(emptyTextBox.getAttribute("value")).isEmpty();243}244245@Test246@Ignore(value = SAFARI, reason = "Does not support alerts yet")247public void handleFormWithJavascriptAction() {248String url = appServer.whereIs("form_handling_js_submit.html");249driver.get(url);250WebElement element = driver.findElement(By.id("theForm"));251element.submit();252Alert alert = wait.until(alertIsPresent());253String text = alert.getText();254alert.accept();255256assertThat(text).isEqualTo("Tasty cheese");257wait.until(titleIs("Submitted Successfully!"));258wait.until(urlContains("submitted_page.html"));259}260261@Test262void testCanClickOnASubmitButton() {263checkSubmitButton("internal_explicit_submit");264}265266@Test267void testCanClickOnASubmitButtonNestedSpan() {268checkSubmitButton("internal_span_submit");269}270271@Test272void testCanClickOnAnImplicitSubmitButton() {273checkSubmitButton("internal_implicit_submit");274}275276@Test277@Ignore(IE)278public void testCanClickOnAnExternalSubmitButton() {279checkSubmitButton("external_explicit_submit");280}281282@Test283@Ignore(IE)284public void testCanClickOnAnExternalImplicitSubmitButton() {285checkSubmitButton("external_implicit_submit");286}287288@Test289void canSubmitFormWithSubmitButtonIdEqualToSubmit() {290driver.get(pages.formPage);291driver.findElement(By.id("submit")).click();292wait.until(titleIs("We Arrive Here"));293assertThat(driver.getTitle()).isEqualTo("We Arrive Here");294}295296@Test297void canSubmitFormWithSubmitButtonNameEqualToSubmit() {298String blank = appServer.create(new Page().withTitle("Submitted Successfully!"));299driver.get(300appServer.create(301new Page()302.withBody(303String.format("<form action='%s'>", blank),304" <input type='submit' name='submit' value='Submit'>",305"</form>")));306307driver.findElement(By.name("submit")).submit();308wait.until(titleIs("Submitted Successfully!"));309}310311private void checkSubmitButton(String buttonId) {312driver.get(appServer.whereIs("click_tests/html5_submit_buttons.html"));313String name = "Gromit";314315driver.findElement(By.id("name")).sendKeys(name);316driver.findElement(By.id(buttonId)).click();317318wait.until(titleIs("Submitted Successfully!"));319320assertThat(driver.getCurrentUrl()).contains("name=" + name);321}322}323324325