Skip to content

Commit

Permalink
Java tests: Implementing ability to switch to the top instead of rest…
Browse files Browse the repository at this point in the history
…arting the driver.
  • Loading branch information
barancev committed Jan 29, 2016
1 parent a8c14bd commit 75b230c
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 35 deletions.
4 changes: 2 additions & 2 deletions java/client/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void testAlertShouldNotAllowAdditionalCommandsIfDismissed() {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldAllowUsersToAcceptAnAlertInAFrame() {
driver.switchTo().frame("iframeWithAlert");
Expand All @@ -238,7 +238,7 @@ public void testShouldAllowUsersToAcceptAnAlertInAFrame() {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldAllowUsersToAcceptAnAlertInANestedFrame() {
driver.switchTo().frame("iframeWithIframe").switchTo().frame("iframeWithAlert");
Expand Down
14 changes: 7 additions & 7 deletions java/client/test/org/openqa/selenium/ClickScrollingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void testShouldScrollOverflowElementsIfClickPointIsOutOfViewButElementIsI
assertEquals("clicked", driver.findElement(By.id("clicked")).getText());
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI, MARIONETTE}, reason = "others: not tested")
public void testShouldBeAbleToClickElementInAFrameThatIsOutOfView() {
Expand All @@ -158,7 +158,7 @@ public void testShouldBeAbleToClickElementInAFrameThatIsOutOfView() {
assertTrue(element.isSelected());
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI}, reason = "not tested")
public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {
Expand All @@ -169,7 +169,7 @@ public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrame() {
assertTrue(element.isSelected());
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test(expected = MoveTargetOutOfBoundsException.class)
@Ignore(reason = "All tested browses scroll non-scrollable frames")
public void testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFrame() {
Expand All @@ -179,7 +179,7 @@ public void testShouldNotBeAbleToClickElementThatIsOutOfViewInANonScrollableFram
element.click();
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI}, reason = "not tested")
public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrameThatIsOutOfView() {
Expand All @@ -190,7 +190,7 @@ public void testShouldBeAbleToClickElementThatIsOutOfViewInAFrameThatIsOutOfView
assertTrue(element.isSelected());
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI}, reason = "not tested")
public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrame() {
Expand All @@ -202,7 +202,7 @@ public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrame() {
assertTrue(element.isSelected());
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI}, reason = "not tested")
public void testShouldBeAbleToClickElementThatIsOutOfViewInANestedFrameThatIsOutOfView() {
Expand All @@ -227,7 +227,7 @@ private long getScrollTop() {
return (Long)((JavascriptExecutor)driver).executeScript("return document.body.scrollTop;");
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(value = {SAFARI, MARIONETTE}, reason = "Not tested")
public void testShouldBeAbleToClickElementInATallFrame() {
Expand Down
14 changes: 5 additions & 9 deletions java/client/test/org/openqa/selenium/ClickTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public void setUp() throws Exception {
driver.get(pages.clicksPage);
}

@After
public void tearDown() throws Exception {
driver.switchTo().defaultContent();
}

@Test
public void testCanClickOnALinkAndFollowIt() {
driver.findElement(By.id("normal")).click();
Expand Down Expand Up @@ -81,9 +76,9 @@ public void testCanClickOnAnAnchorAndNotReloadThePage() {
assertEquals("Latch was reset", Boolean.TRUE, samePage);
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@Test
@Ignore(value = {MARIONETTE}, reason = "getPageSource issue")
@SwitchToTopAfterTest
public void testCanClickOnALinkThatUpdatesAnotherFrame() {
driver.switchTo().frame("source");

Expand All @@ -94,9 +89,9 @@ public void testCanClickOnALinkThatUpdatesAnotherFrame() {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@Test
@Ignore(value = {MARIONETTE}, reason = "getPageSource issue")
@SwitchToTopAfterTest
public void testElementsFoundByJsCanLoadUpdatesInAnotherFrame() {
driver.switchTo().frame("source");

Expand All @@ -112,6 +107,7 @@ public void testElementsFoundByJsCanLoadUpdatesInAnotherFrame() {
@JavascriptEnabled
@Test
@Ignore(value = {MARIONETTE})
@SwitchToTopAfterTest
public void testJsLocatedElementsCanUpdateFramesIfFoundSomehowElse() {
driver.switchTo().frame("source");

Expand Down Expand Up @@ -285,8 +281,8 @@ public void testShouldBeAbleToClickOnAnElementGreaterThanTwoViewports() {
}

@Test
@Ignore(value = {CHROME, MARIONETTE},
reason = "Chrome: failed")
@Ignore(value = {CHROME, MARIONETTE}, reason = "Chrome: failed")
@SwitchToTopAfterTest
public void testShouldBeAbleToClickOnAnElementInFrameGreaterThanTwoViewports() {
String url = appServer.whereIs("click_too_big_in_frame.html");
driver.get(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void testAddCookiesWithDifferentPathsThatAreRelatedToOurs() {
}

@Ignore(value = {CHROME, PHANTOMJS, SAFARI})
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testGetCookiesInAFrame() {
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testSameElementLookedUpDifferentWaysUsingFindElementsShouldHaveSameH
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testAnElementFoundInADifferentFrameViaJsShouldHaveSameId() {
driver.get(pages.missedJsReferencePage);
Expand Down
4 changes: 2 additions & 2 deletions java/client/test/org/openqa/selenium/ElementFindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public void testShouldNotBeAbleToLocateASingleElementOnABlankPage() {
driver.findElement(By.id("nonExistantButton"));
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testAnElementFoundInADifferentFrameIsStale() {
driver.get(pages.missedJsReferencePage);
Expand All @@ -758,7 +758,7 @@ public void testAnElementFoundInADifferentFrameIsStale() {

@JavascriptEnabled
@Test
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
public void testAnElementFoundInADifferentFrameViaJsCanBeUsed() {
driver.get(pages.missedJsReferencePage);

Expand Down
2 changes: 1 addition & 1 deletion java/client/test/org/openqa/selenium/PageLoadingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public void testShouldReturnURLOnNotExistedPage() {
}

@Ignore({MARIONETTE})
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldBeAbleToLoadAPageWithFramesetsAndWaitUntilAllFramesAreLoaded() {
driver.get(pages.framesetPage);
Expand Down
6 changes: 3 additions & 3 deletions java/client/test/org/openqa/selenium/PositionAndSizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void testShouldScrollPageAndGetCoordinatesOfAnElementThatIsOutOfViewPort(
assertThat(getLocationOnPage(By.id("box")), is(new Point(10, 5010)));
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldGetCoordinatesOfAnElementInAFrame() {
driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));
Expand All @@ -119,7 +119,7 @@ public void testShouldGetCoordinatesOfAnElementInAFrame() {
}

@Ignore({SAFARI, MARIONETTE})
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldGetCoordinatesInViewPortOfAnElementInAFrame() {
driver.get(appServer.whereIs("coordinates_tests/element_in_frame.html"));
Expand All @@ -129,7 +129,7 @@ public void testShouldGetCoordinatesInViewPortOfAnElementInAFrame() {
}

@Ignore({SAFARI, MARIONETTE})
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldGetCoordinatesInViewPortOfAnElementInANestedFrame() {
driver.get(appServer.whereIs("coordinates_tests/element_in_nested_frame.html"));
Expand Down
29 changes: 29 additions & 0 deletions java/client/test/org/openqa/selenium/SwitchToTopAfterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SwitchToTopAfterTest {

}
4 changes: 2 additions & 2 deletions java/client/test/org/openqa/selenium/TakesScreenshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void testShouldCaptureScreenshotAtIFramePage() throws Exception {
compareColors(expectedColors, actualColors);
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(
value = {IE, MARIONETTE},
Expand Down Expand Up @@ -357,7 +357,7 @@ && getEffectivePlatform(driver).is(LINUX)
compareColors(expectedColors, actualColors);
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
@Ignore(
value = {IE, CHROME, MARIONETTE},
Expand Down
2 changes: 1 addition & 1 deletion java/client/test/org/openqa/selenium/UploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setUp() throws Exception {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testFileUploading() throws Exception {
assumeFalse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

public class WindowSwitchingTest extends JUnit4TestBase {

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOperations() {
assumeFalse(Browser.detect() == Browser.opera &&
Expand Down
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ java_library(name = "base",
"Pages.java",
"ParallelTestRunner.java",
"StubDriver.java",
"SwitchToTopAfterTest.java",
"WaitingConditions.java",
],
embedded = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.openqa.selenium.NoDriverAfterTest;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.SwitchToTopAfterTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.Color;
Expand Down Expand Up @@ -314,7 +315,7 @@ public void testMovingMouseBackAndForthPastViewPort() {
wait.until(elementTextToEqual(resultArea, expectedEvents));
}

@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testShouldClickElementInIFrame() {
driver.get(pages.clicksPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openqa.selenium.NoDriverAfterTest;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.SwitchToTopAfterTest;
import org.openqa.selenium.WaitingConditions;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.testing.Ignore;
Expand Down Expand Up @@ -183,7 +184,7 @@ private void navigateToClicksPageAndClickLink() {
}

@Ignore(value = {PHANTOMJS, SAFARI}, reason = "Not tested")
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void canMoveMouseToAnElementInAnIframeAndClick() {
driver.get(appServer.whereIs("click_tests/click_in_iframe.html"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openqa.selenium.NoDriverAfterTest;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.SwitchToTopAfterTest;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.testing.Ignore;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void testDragAndDropToElement() {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testDragAndDropToElementInIframe() {
driver.get(pages.iframePage);
Expand All @@ -102,7 +103,7 @@ public void testDragAndDropToElementInIframe() {
}

@JavascriptEnabled
@NoDriverAfterTest // So that next test never starts with "inside a frame" base state.
@SwitchToTopAfterTest
@Test
public void testDragAndDropElementWithOffsetInIframeAtBottom() {
driver.get(appServer.whereIs("iframeAtBottom.html"));
Expand Down
14 changes: 13 additions & 1 deletion java/client/test/org/openqa/selenium/testing/JUnit4TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openqa.selenium.NeedsFreshDriver;
import org.openqa.selenium.NoDriverAfterTest;
import org.openqa.selenium.Pages;
import org.openqa.selenium.SwitchToTopAfterTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.environment.InProcessTestEnvironment;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void prepareEnvironment() throws Exception {
.outerRule(new DetectBrowserRule())
.around(new TraceMethodNameRule())
.around(new ManageDriverRule())
.around(new SwitchToTopRule())
.around(new NotYetImplementedRule())
.around(new CoveringUpSauceErrorsRule());

Expand Down Expand Up @@ -138,6 +140,17 @@ protected void finished(Description description) {
}
}

private class SwitchToTopRule extends TestWatcher {
@Override
protected void finished(Description description) {
super.finished(description);
SwitchToTopAfterTest annotation = description.getAnnotation(SwitchToTopAfterTest.class);
if (annotation != null) {
driver.switchTo().defaultContent();
}
}
}

private class CoveringUpSauceErrorsRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
Expand Down Expand Up @@ -168,7 +181,6 @@ private void dealWithSauceFailureIfNecessary(Throwable t) {
throw Throwables.propagate(t);
}
}

}

private class NotYetImplementedRule implements TestRule {
Expand Down

0 comments on commit 75b230c

Please sign in to comment.