Path: blob/trunk/java/test/org/openqa/selenium/JavascriptEnabledDriverTest.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.junit.jupiter.api.Assumptions.assumeTrue;21import static org.openqa.selenium.WaitingConditions.elementTextToEqual;22import static org.openqa.selenium.WaitingConditions.elementValueToEqual;23import static org.openqa.selenium.WaitingConditions.windowToBeSwitchedToWithName;24import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;25import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2627import org.junit.jupiter.api.Test;28import org.openqa.selenium.interactions.Locatable;29import org.openqa.selenium.support.ui.ExpectedConditions;30import org.openqa.selenium.testing.JupiterTestBase;31import org.openqa.selenium.testing.NoDriverAfterTest;32import org.openqa.selenium.testing.NotYetImplemented;3334class JavascriptEnabledDriverTest extends JupiterTestBase {3536@Test37void testDocumentShouldReflectLatestTitle() {38driver.get(pages.javascriptPage);3940assertThat(driver.getTitle()).isEqualTo("Testing Javascript");41driver.findElement(By.linkText("Change the page title!")).click();42waitForTitleChange("Changed");43assertThat(driver.getTitle()).isEqualTo("Changed");44}4546@Test47@NotYetImplemented(SAFARI)48public void testDocumentShouldReflectLatestDom() {49driver.get(pages.javascriptPage);50String currentText = driver.findElement(By.xpath("//div[@id='dynamo']")).getText();51assertThat(currentText).isEqualTo("What's for dinner?");5253WebElement webElement = driver.findElement(By.linkText("Update a div"));54webElement.click();5556WebElement dynamo = driver.findElement(By.xpath("//div[@id='dynamo']"));5758wait.until(elementTextToEqual(dynamo, "Fish and chips!"));59assertThat(dynamo.getText()).isEqualTo("Fish and chips!");60}6162@Test63void testShouldWaitForLoadsToCompleteAfterJavascriptCausesANewPageToLoad() {64driver.get(pages.formPage);6566driver.findElement(By.id("changeme")).click();6768waitForTitleChange("Page3");69assertThat(driver.getTitle()).isEqualTo("Page3");70}7172@Test73void testShouldBeAbleToFindElementAfterJavascriptCausesANewPageToLoad() {74driver.get(pages.formPage);7576driver.findElement(By.id("changeme")).click();7778waitForTitleChange("Page3");79assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("3");80}8182@Test83@NotYetImplemented(value = SAFARI, reason = "getText does not normalize spaces")84public void testShouldFireOnChangeEventWhenSettingAnElementsValue() {85driver.get(pages.javascriptPage);86driver.findElement(By.id("change")).sendKeys("foo");87String result = driver.findElement(By.id("result")).getText();8889assertThat(result).isEqualTo("change");90}9192@Test93@NotYetImplemented(SAFARI)94public void testShouldBeAbleToSubmitFormsByCausingTheOnClickEventToFire() {95driver.get(pages.javascriptPage);96WebElement element = driver.findElement(By.id("jsSubmitButton"));97element.click();9899waitForTitleChange("We Arrive Here");100101assertThat(driver.getTitle()).isEqualTo("We Arrive Here");102}103104private void waitForTitleChange(String newTitle) {105wait.until(titleIs(newTitle));106}107108@Test109@NotYetImplemented(SAFARI)110public void testShouldBeAbleToClickOnSubmitButtons() {111driver.get(pages.javascriptPage);112WebElement element = driver.findElement(By.id("submittingButton"));113element.click();114115waitForTitleChange("We Arrive Here");116117assertThat(driver.getTitle()).isEqualTo("We Arrive Here");118}119120@Test121void testIssue80ClickShouldGenerateClickEvent() {122driver.get(pages.javascriptPage);123WebElement element = driver.findElement(By.id("clickField"));124assertThat(element.getAttribute("value")).isEqualTo("Hello");125126element.click();127128String elementValue = wait.until(elementValueToEqual(element, "Clicked"));129130assertThat(elementValue).isEqualTo("Clicked");131}132133@Test134@NotYetImplemented(SAFARI)135public void testShouldBeAbleToSwitchToFocusedElement() {136driver.get(pages.javascriptPage);137138driver.findElement(By.id("switchFocus")).click();139140WebElement element = driver.switchTo().activeElement();141assertThat(element.getAttribute("id")).isEqualTo("theworks");142}143144@Test145void testIfNoElementHasFocusTheActiveElementIsTheBody() {146driver.get(pages.simpleTestPage);147148WebElement element = driver.switchTo().activeElement();149150assertThat(element.getAttribute("name")).isEqualTo("body");151}152153@Test154@NotYetImplemented(value = SAFARI)155public void testChangeEventIsFiredAppropriatelyWhenFocusIsLost() {156driver.get(pages.javascriptPage);157158WebElement input = driver.findElement(By.id("changeable"));159input.sendKeys("test");160moveFocus();161assertThat(driver.findElement(By.id("result")).getText().trim())162.isIn("focus change blur", "focus blur change");163164input.sendKeys(Keys.BACK_SPACE, "t");165moveFocus();166167// I weep.168assertThat(driver.findElement(By.id("result")).getText().trim())169.isIn(170"focus change blur focus blur", "focus blur change focus blur",171"focus blur change focus blur change", "focus change blur focus change blur");172}173174/** If the click handler throws an exception, the firefox driver freezes. This is suboptimal. */175@Test176void testShouldBeAbleToClickIfEvenSomethingHorribleHappens() {177driver.get(pages.javascriptPage);178179driver.findElement(By.id("error")).click();180181// If we get this far then the test has passed, but let's do something basic to prove the point182String text = driver.findElement(By.id("error")).getText();183assertThat(text).isNotNull();184}185186@Test187void testShouldBeAbleToGetTheLocationOfAnElement() {188assumeTrue(driver instanceof JavascriptExecutor);189190driver.get(pages.javascriptPage);191192((JavascriptExecutor) driver).executeScript("window.focus();");193WebElement element = driver.findElement(By.id("keyUp"));194195assumeTrue(element instanceof Locatable);196197Point point = ((Locatable) element).getCoordinates().inViewPort();198199assertThat(point.getX()).as("X coordinate").isGreaterThan(1);200// Element's Y coordinates can be 0, as the element is scrolled right to the top of the window.201assertThat(point.getY()).as("Y coordinate").isGreaterThanOrEqualTo(0);202}203204/*205* There's a weird issue with this test, which means that I've added the needs fresh driver206* annotation. To see it in action, try running the single test suite with only these tests207* running: "ImplicitWaitTest", "TemporaryFilesystemTest", "JavascriptEnabledDriverTest".208* SimonStewart 2010-10-04209*/210@NoDriverAfterTest211@Test212@NotYetImplemented(SAFARI)213public void testShouldBeAbleToClickALinkThatClosesAWindow() {214driver.get(pages.javascriptPage);215216String handle = driver.getWindowHandle();217driver.findElement(By.id("new_window")).click();218219// Depending on the Android emulator platform this can take a while.220wait.until(windowToBeSwitchedToWithName("close_me"));221222wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("close")));223driver.findElement(By.id("close")).click();224225driver.switchTo().window(handle);226227// If we haven't seen an exception or hung the test has passed228}229230private void moveFocus() {231driver.findElement(By.id("clickField")).click();232}233}234235236