8000 [plugin-web-app] Add workaround for error on `scrollToElement` action by valfirst · Pull Request #4475 · vividus-framework/vividus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[plugin-web-app] Add workaround for error on scrollToElement action #4475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.manager.IWebDriverManager;
import org.vividus.softassert.ISoftAssert;
Expand Down Expand Up @@ -129,7 +130,18 @@ public void moveToElement(WebElement element)
{
if (element != null)
{
new Actions(getWebDriver()).scrollToElement(element).perform();
try
{
new Actions(getWebDriver()).scrollToElement(element).perform();
}
catch (MoveTargetOutOfBoundsException e)
{
// This is workaround for some Chrome/Chromium driver/browser bug:
// https://github.com/w3c/webdriver/issues/1635#issuecomment-1196434722
// https://stackoverflow.com/q/69975806/2067574
javascriptActions.scrollElementIntoViewportCenter(element);
}

new Actions(getWebDriver()).moveToElement(element).perform();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
Expand All @@ -40,6 +41,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.Captor;
import org.mockito.InOrder;
Expand All @@ -52,6 +54,7 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Interactive;
import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;
import org.openqa.selenium.interactions.Sequence;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.manager.IWebDriverManager;
Expand Down Expand Up @@ -288,21 +291,60 @@ void shouldMoveToElement()
verify((Interactive) webDriver, times(2)).perform(sequencesCaptor.capture());
List<Collection<Sequence>> sequences = sequencesCaptor.getAllValues();
assertEquals(2, sequences.size());
assertEquals(Map.of(
ID, "default wheel",
TYPE, "wheel",
ACTIONS, List.of(
Map.of(
DURATION, 250L,
X, 0,
Y, 0,
"deltaX", 0,
"deltaY", 0,
TYPE, "scroll",
ORIGIN, webElement
)
)), sequences.get(0).iterator().next().toJson());
assertEquals(createScrollToElementAction(), sequences.get(0).iterator().next().toJson());
assertEquals(mouseSequence(List.of(pointerMoveAction())), sequences.get(1).iterator().next().toJson());
verifyNoInteractions(javascriptActions);
}

@Test
void shouldUseWorkaroundForFailureAtScrollToElementWhileMovingToElement()
{
when(webDriverProvider.get()).thenReturn(webDriver);
doThrow(new MoveTargetOutOfBoundsException("move target out of bounds")).doNothing().when(
(Interactive) webDriver).perform(argThat(new ArgumentMatcher<>()
{
private int invocationNumber;

@Override
public boolean matches(Collection<Sequence> sequences)
{
if (invocationNumber == 0)
{
invocationNumber++;
return sequences.size() == 1 && createScrollToElementAction().equals(
sequences.iterator().next().toJson());
}
return true;
}
}
)
);
mouseActions.moveToElement(webElement);
var ordered = inOrder(webDriver, javascriptActions);
ordered.verify((Interactive) webDriver).perform(
argThat(sequences -> sequences.size() == 1 && createScrollToElementAction().equals(
sequences.iterator().next().toJson())));
ordered.verify(javascriptActions).scrollElementIntoViewportCenter(webElement);
ordered.verify((Interactive) webDriver).perform(argThat(sequences -> sequences.size() == 1 && mouseSequence(
List.of(pointerMoveAction())).equals(sequences.iterator().next().toJson())));
}

private Map<String, Object> createScrollToElementAction()
{
return Map.of(
ID, "default wheel",
TYPE, "wheel",
ACTIONS, List.of(
Map.of(
DURATION, 250L,
X, 0,
Y, 0,
"deltaX", 0,
"deltaY", 0,
TYPE, "scroll",
ORIGIN, webElement
)
));
}

private static Map<String, Object> mouseSequence(List<Map<String, ?>> mouseActions)
Expand Down
0