|
11 | 11 | using Microsoft.AspNetCore.Testing; |
12 | 12 | using OpenQA.Selenium; |
13 | 13 | using OpenQA.Selenium.Support.Extensions; |
| 14 | +using OpenQA.Selenium.Support.UI; |
14 | 15 | using Xunit; |
15 | 16 | using Xunit.Abstractions; |
16 | 17 |
|
@@ -366,10 +367,69 @@ public void CanRefreshItemsProviderResultsInPlace() |
366 | 367 | name => Assert.Equal("Person 3", name)); |
367 | 368 | } |
368 | 369 |
|
| 370 | + [Fact] |
| 371 | + public void CanExpandDataSetAndRetainScrollPosition() |
| 372 | + { |
| 373 | + Browser.MountTestComponent<VirtualizationDataChanges>(); |
| 374 | + var dataSetLengthSelector = new SelectElement(Browser.Exists(By.Id("large-dataset-length"))); |
| 375 | + var dataSetLengthLastRendered = () => int.Parse(Browser.FindElement(By.Id("large-dataset-length-lastrendered")).Text, CultureInfo.InvariantCulture); |
| 376 | + var container = Browser.Exists(By.Id("removing-many")); |
| 377 | + |
| 378 | + // Scroll to the end of a medium list |
| 379 | + dataSetLengthSelector.SelectByText("1000"); |
| 380 | + Browser.Equal(1000, dataSetLengthLastRendered); |
| 381 | + Browser.True(() => |
| 382 | + { |
| 383 | + ScrollToEnd(Browser, container); |
| 384 | + return GetPeopleNames(container).Contains("Person 1000"); |
| 385 | + }); |
| 386 | + |
| 387 | + // Expand the data set |
| 388 | + dataSetLengthSelector.SelectByText("100000"); |
| 389 | + Browser.Equal(100000, dataSetLengthLastRendered); |
| 390 | + |
| 391 | + // See that the old data is still visible, because the scroll position is preserved as a pixel count, |
| 392 | + // not a scroll percentage |
| 393 | + Browser.True(() => GetPeopleNames(container).Contains("Person 1000")); |
| 394 | + } |
| 395 | + |
| 396 | + [Fact] |
| 397 | + public void CanHandleDataSetShrinkingWithExistingOffsetAlreadyBeyondNewListEnd() |
| 398 | + { |
| 399 | + // Represents https://github.com/dotnet/aspnetcore/issues/37245 |
| 400 | + Browser.MountTestComponent<VirtualizationDataChanges>(); |
| 401 | + var dataSetLengthSelector = new SelectElement(Browser.Exists(By.Id("large-dataset-length"))); |
| 402 | + var dataSetLengthLastRendered = () => int.Parse(Browser.FindElement(By.Id("large-dataset-length-lastrendered")).Text, CultureInfo.InvariantCulture); |
| 403 | + var container = Browser.Exists(By.Id("removing-many")); |
| 404 | + |
| 405 | + // Scroll to the end of a very long list |
| 406 | + dataSetLengthSelector.SelectByText("100000"); |
| 407 | + Browser.Equal(100000, dataSetLengthLastRendered); |
| 408 | + Browser.True(() => |
| 409 | + { |
| 410 | + ScrollToEnd(Browser, container); |
| 411 | + return GetPeopleNames(container).Contains("Person 100000"); |
| 412 | + }); |
| 413 | + |
| 414 | + // Now make the dataset much shorter |
| 415 | + // We should automatically have the scroll position reduced to the new maximum |
| 416 | + // Because the new data set is *so much* shorter than the previous one, if bug #37245 were still here, |
| 417 | + // this would take over 30 minutes so the test would fail |
| 418 | + dataSetLengthSelector.SelectByText("25"); |
| 419 | + Browser.Equal(25, dataSetLengthLastRendered); |
| 420 | + Browser.True(() => GetPeopleNames(container).Contains("Person 25")); |
| 421 | + } |
| 422 | + |
369 | 423 | private string[] GetPeopleNames(IWebElement container) |
370 | 424 | { |
371 | 425 | var peopleElements = container.FindElements(By.CssSelector(".person span")); |
372 | 426 | return peopleElements.Select(element => element.Text).ToArray(); |
373 | 427 | } |
| 428 | + |
| 429 | + private static void ScrollToEnd(IWebDriver browser, IWebElement elem) |
| 430 | + { |
| 431 | + var js = (IJavaScriptExecutor)browser; |
| 432 | + js.ExecuteScript("arguments[0].scrollTop = arguments[0].scrollHeight", elem); |
| 433 | + } |
374 | 434 | } |
375 | 435 | } |
0 commit comments