diff --git a/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java b/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java index 12ed6b6a1..3a2b91ae6 100644 --- a/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java +++ b/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java @@ -7,9 +7,12 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Action; +import org.openqa.selenium.interactions.Actions; import com.codeborne.selenide.AssertionMode; import com.codeborne.selenide.Condition; @@ -386,4 +389,46 @@ public static void wrapAssertionError(final Runnable runnable) } } } -} + + /** + * Drag and drop an element to a given position. The position will be set by the user. It drags the element and + * moves it to a specific position of the respective slider. + * + * @param elementToMove + * The selector of the slider to drag and drop + * @param elementToCheck + * The locator of the slider value + * @param horizontalMovement + * The offset for the horizontal movement + * @param verticalMovement + * The offset for the vertical movement + * @param pauseBetweenMovements + * Time to pass after the slider do the next movement step + * @param retryMovements + * Amount of retries the slider will be moved + * @param condition + * The condition for the slider to verify the movement + */ + + public static void dragAndDropUntilCondition(SelenideElement elementToMove, SelenideElement elementToCheck, int horizontalMovement, int verticalMovement, + int pauseBetweenMovements, int retryMovements, Condition condition) + { + Actions moveSlider = new Actions(Neodymium.getDriver()); + + int counter = 0; + while (!elementToCheck.has(condition)) + { + if (counter > retryMovements) + { + SelenideAddons.wrapAssertionError(() -> { + Assert.assertTrue("CircutBreaker: Was not able to move the element and to reach the condition. Tried: " + retryMovements + + " times to move the element.", false); + }); + } + Action action = moveSlider.dragAndDropBy(elementToMove.getWrappedElement(), horizontalMovement, verticalMovement).build(); + action.perform(); + Selenide.sleep(pauseBetweenMovements); + counter++; + } + } +} \ No newline at end of file diff --git a/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java b/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java index 0268caecb..b3491721a 100644 --- a/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java +++ b/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java @@ -15,6 +15,7 @@ import org.junit.runner.RunWith; import org.openqa.selenium.StaleElementReferenceException; +import com.codeborne.selenide.Condition; import com.codeborne.selenide.Selenide; import com.codeborne.selenide.SelenideElement; import com.codeborne.selenide.ex.ElementShould; @@ -404,4 +405,76 @@ private RuntimeException getWrappedThrowable(String message) { return new RuntimeException(new StaleElementReferenceException(message)); } -} + + @Test() + public void testRightHorizontalDragAndDropUntilCondition() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $(".balSlider a[role=slider]"); + SelenideAddons.dragAndDropUntilCondition(slider, slider, 40, 0, 3000, 23, Condition.attribute("aria-valuenow", "8")); + + Assert.assertEquals($(".balSlider a[role=slider]").getAttribute("aria-valuenow"), "8"); + } + + @Test() + public void testLeftHorizontalDragAndDropUntilCondition() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $(".balSlider a[role=slider]"); + SelenideAddons.dragAndDropUntilCondition(slider, slider, -40, 0, 3000, 23, Condition.attribute("aria-valuenow", "-8")); + + Assert.assertEquals($(".balSlider a[role=slider]").getAttribute("aria-valuenow"), "-8"); + } + + @Test() + public void testUpVerticalDragAndDropUntilCondition() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $("#equalizer .k-slider-vertical:first-child a"); + SelenideAddons.dragAndDropUntilCondition(slider, slider, 0, -10, 3000, 23, Condition.attribute("aria-valuenow", "16")); + + Assert.assertEquals($("#equalizer .k-slider-vertical:first-child a").getAttribute("aria-valuenow"), "16"); + } + + @Test() + public void testDownVerticalDragAndDropUntilCondition() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $("#equalizer .k-slider-vertical:first-child a"); + SelenideAddons.dragAndDropUntilCondition(slider, slider, 0, 10, 3000, 23, Condition.attribute("aria-valuenow", "-6")); + + Assert.assertEquals($("#equalizer .k-slider-vertical:first-child a").getAttribute("aria-valuenow"), "-6"); + } + + @Test(expected = AssertionError.class) + public void testDragAndDropAssertionError() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $(".balSlider a[role=slider]"); + SelenideAddons.dragAndDropUntilCondition(slider, slider, -10, 0, 3000, -1, Condition.attribute("aria-valuenow", "-16")); + } + + @Test() + public void testLeftHorizontalDragAndDropUntilText() + { + Selenide.open("https://demos.telerik.com/kendo-ui/slider/index"); + + SelenideElement slider = $(".balSlider a[role=slider]"); + leftHorizontalDragAndDropUntilText(slider, slider, -40, "aria-valuenow", "-8"); + + Assert.assertEquals($(".balSlider a[role=slider]").getAttribute("aria-valuenow"), "-8"); + } + + private void leftHorizontalDragAndDropUntilText(SelenideElement elementToMove, SelenideElement elementToCheck, int horizontalMovement, + String sliderValueAttributeName, String moveUntil) + { + // Example: create a special method to move until a given text + SelenideAddons.dragAndDropUntilCondition(elementToMove, elementToCheck, horizontalMovement, 0, 3000, 10, + Condition.attribute(sliderValueAttributeName, moveUntil)); + } +} \ No newline at end of file