Path: blob/trunk/java/test/org/openqa/selenium/FormHandlingTest.java
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.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.testing.drivers.Browser.FIREFOX;25import static org.openqa.selenium.testing.drivers.Browser.IE;26import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2728import java.io.File;29import java.io.IOException;30import org.junit.jupiter.api.Test;31import org.openqa.selenium.environment.webserver.Page;32import org.openqa.selenium.testing.Ignore;33import org.openqa.selenium.testing.JupiterTestBase;34import org.openqa.selenium.testing.NotYetImplemented;3536class FormHandlingTest extends JupiterTestBase {3738@Test39void testShouldClickOnSubmitInputElements() {40driver.get(pages.formPage);41driver.findElement(By.id("submitButton")).click();42wait.until(titleIs("We Arrive Here"));43assertThat(driver.getTitle()).isEqualTo("We Arrive Here");44}4546@Test47void testClickingOnUnclickableElementsDoesNothing() {48driver.get(pages.formPage);49driver.findElement(By.xpath("//body")).click();50}5152@Test53void testShouldBeAbleToClickImageButtons() {54driver.get(pages.formPage);55driver.findElement(By.id("imageButton")).click();56wait.until(titleIs("We Arrive Here"));57assertThat(driver.getTitle()).isEqualTo("We Arrive Here");58}5960@Test61void testShouldBeAbleToSubmitForms() {62driver.get(pages.formPage);63driver.findElement(By.name("login")).submit();64wait.until(titleIs("We Arrive Here"));65}6667@Test68void testShouldSubmitAFormWhenAnyInputElementWithinThatFormIsSubmitted() {69driver.get(pages.formPage);70driver.findElement(By.id("checky")).submit();71wait.until(titleIs("We Arrive Here"));72}7374@Test75void testShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted() {76driver.get(pages.formPage);77driver.findElement(By.xpath("//form/p")).submit();78wait.until(titleIs("We Arrive Here"));79}8081@Test82void testShouldNotBeAbleToSubmitAFormThatDoesNotExist() {83driver.get(pages.formPage);84WebElement element = driver.findElement(By.name("SearchableText"));85assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(element::submit);86}8788@Test89void testShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue() {90driver.get(pages.javascriptPage);91WebElement textarea = driver.findElement(By.id("keyUpArea"));92String cheesy = "brie and cheddar";93textarea.sendKeys(cheesy);94assertThat(textarea.getAttribute("value")).isEqualTo(cheesy);95}9697@Test98void testSendKeysKeepsCapitalization() {99driver.get(pages.javascriptPage);100WebElement textarea = driver.findElement(By.id("keyUpArea"));101String cheesey = "BrIe And CheDdar";102textarea.sendKeys(cheesey);103assertThat(textarea.getAttribute("value")).isEqualTo(cheesey);104}105106@Test107@NotYetImplemented(FIREFOX)108@NotYetImplemented(SAFARI)109public void testShouldSubmitAFormUsingTheNewlineLiteral() {110driver.get(pages.formPage);111WebElement nestedForm = driver.findElement(By.id("nested_form"));112WebElement input = nestedForm.findElement(By.name("x"));113input.sendKeys("\n");114wait.until(titleIs("We Arrive Here"));115assertThat(driver.getCurrentUrl()).endsWith("?x=name");116}117118@Test119void testShouldSubmitAFormUsingTheEnterKey() {120driver.get(pages.formPage);121WebElement nestedForm = driver.findElement(By.id("nested_form"));122WebElement input = nestedForm.findElement(By.name("x"));123input.sendKeys(Keys.ENTER);124wait.until(titleIs("We Arrive Here"));125assertThat(driver.getCurrentUrl()).endsWith("?x=name");126}127128@Test129void testShouldEnterDataIntoFormFields() {130driver.get(pages.xhtmlTestPage);131WebElement element =132driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));133String originalValue = element.getAttribute("value");134assertThat(originalValue).isEqualTo("change");135136element.clear();137element.sendKeys("some text");138139element = driver.findElement(By.xpath("//form[@name='someForm']/input[@id='username']"));140String newFormValue = element.getAttribute("value");141assertThat(newFormValue).isEqualTo("some text");142}143144@Test145void testShouldBeAbleToAlterTheContentsOfAFileUploadInputElement() throws IOException {146driver.get(pages.formPage);147WebElement uploadElement = driver.findElement(By.id("upload"));148assertThat(uploadElement.getAttribute("value")).isEmpty();149150File file = File.createTempFile("test", "txt");151file.deleteOnExit();152153uploadElement.sendKeys(file.getAbsolutePath());154155String uploadPath = uploadElement.getAttribute("value");156assertThat(uploadPath.endsWith(file.getName())).isTrue();157}158159@Test160void testShouldBeAbleToSendKeysToAFileUploadInputElementInAnXhtmlDocument() throws IOException {161driver.get(pages.xhtmlFormPage);162WebElement uploadElement = driver.findElement(By.id("file"));163assertThat(uploadElement.getAttribute("value")).isEmpty();164165File file = File.createTempFile("test", "txt");166file.deleteOnExit();167168uploadElement.sendKeys(file.getAbsolutePath());169170String uploadPath = uploadElement.getAttribute("value");171assertThat(uploadPath.endsWith(file.getName())).isTrue();172}173174@Test175@Ignore(value = SAFARI, reason = "Hanging")176public void testShouldBeAbleToUploadTheSameFileTwice() throws IOException {177File file = File.createTempFile("test", "txt");178file.deleteOnExit();179180driver.get(pages.formPage);181WebElement uploadElement = driver.findElement(By.id("upload"));182assertThat(uploadElement.getAttribute("value")).isEmpty();183184uploadElement.sendKeys(file.getAbsolutePath());185uploadElement.submit();186187// Apparently on chrome we need to wait for the element to be gone if we're loading the same188// page again189wait.until(d -> d.findElements(By.id("upload")).isEmpty());190191driver.get(pages.formPage);192uploadElement = driver.findElement(By.id("upload"));193assertThat(uploadElement.getAttribute("value")).isEmpty();194195uploadElement.sendKeys(file.getAbsolutePath());196uploadElement.submit();197198// If we get this far, then we're all good.199}200201@Test202void testSendingKeyboardEventsShouldAppendTextInInputs() {203driver.get(pages.formPage);204WebElement element = driver.findElement(By.id("working"));205element.sendKeys("some");206String value = element.getAttribute("value");207assertThat(value).isEqualTo("some");208209element.sendKeys(" text");210value = element.getAttribute("value");211assertThat(value).isEqualTo("some text");212}213214@Test215@NotYetImplemented(SAFARI)216public void testSendingKeyboardEventsShouldAppendTextInInputsWithExistingValue() {217driver.get(pages.formPage);218WebElement element = wait.until(presenceOfElementLocated(By.id("inputWithText")));219element.sendKeys(". Some text");220String value = element.getAttribute("value");221222assertThat(value).isEqualTo("Example text. Some text");223}224225@Test226@NotYetImplemented(SAFARI)227public void testSendingKeyboardEventsShouldAppendTextInTextAreas() {228driver.get(pages.formPage);229WebElement element = driver.findElement(By.id("withText"));230231element.sendKeys(". Some text");232String value = element.getAttribute("value");233234assertThat(value).isEqualTo("Example text. Some text");235}236237@Test238void testEmptyTextBoxesShouldReturnAnEmptyStringNotNull() {239driver.get(pages.formPage);240WebElement emptyTextBox = driver.findElement(By.id("working"));241assertThat(emptyTextBox.getAttribute("value")).isEmpty();242}243244@Test245@Ignore(value = SAFARI, reason = "Does not support alerts yet")246public void handleFormWithJavascriptAction() {247String url = appServer.whereIs("form_handling_js_submit.html");248driver.get(url);249WebElement element = driver.findElement(By.id("theForm"));250element.submit();251Alert alert = wait.until(alertIsPresent());252String text = alert.getText();253alert.accept();254255assertThat(text).isEqualTo("Tasty cheese");256}257258@Test259void testCanClickOnASubmitButton() {260checkSubmitButton("internal_explicit_submit");261}262263@Test264void testCanClickOnASubmitButtonNestedSpan() {265checkSubmitButton("internal_span_submit");266}267268@Test269void testCanClickOnAnImplicitSubmitButton() {270checkSubmitButton("internal_implicit_submit");271}272273@Test274@Ignore(IE)275public void testCanClickOnAnExternalSubmitButton() {276checkSubmitButton("external_explicit_submit");277}278279@Test280@Ignore(IE)281public void testCanClickOnAnExternalImplicitSubmitButton() {282checkSubmitButton("external_implicit_submit");283}284285@Test286void canSubmitFormWithSubmitButtonIdEqualToSubmit() {287driver.get(pages.formPage);288driver.findElement(By.id("submit")).click();289wait.until(titleIs("We Arrive Here"));290assertThat(driver.getTitle()).isEqualTo("We Arrive Here");291}292293@Test294void canSubmitFormWithSubmitButtonNameEqualToSubmit() {295String blank = appServer.create(new Page().withTitle("Submitted Successfully!"));296driver.get(297appServer.create(298new Page()299.withBody(300String.format("<form action='%s'>", blank),301" <input type='submit' name='submit' value='Submit'>",302"</form>")));303304driver.findElement(By.name("submit")).submit();305wait.until(titleIs("Submitted Successfully!"));306}307308private void checkSubmitButton(String buttonId) {309driver.get(appServer.whereIs("click_tests/html5_submit_buttons.html"));310String name = "Gromit";311312driver.findElement(By.id("name")).sendKeys(name);313driver.findElement(By.id(buttonId)).click();314315wait.until(titleIs("Submitted Successfully!"));316317assertThat(driver.getCurrentUrl()).contains("name=" + name);318}319}320321322