I'm currently dealing with a pivot table/grid that allows users to expand and collapse rows. My challenge is figuring out how to scroll to an expanded row after an expansion occurs. Typically, I use the following code for scrolling functions:
Actions actions = new Actions(Browser);
actions.MoveToElement(plusMinusIcon).Perform();
and this:
var coordinates = plusMinusIcon.Location;
((IJavaScriptExecutor)Browser).ExecuteScript("window.scrollTo(0," + coordinates.Y + ")");
These scrolling methods work fine before any expansion, when the page isn't too large and the scrollbar isn't too long. However, if I expand a row, causing more rows to appear and the page to become much larger, the scroll method fails to reach the correct location and instead scrolls elsewhere (even though it worked fine for the initial expansion). During debugging, I observed that the scroll does move the page slightly, but not to the intended element. What could be causing this issue? Do I need to calculate new coordinates for scrolling?
Below is the line of code from my test:
Page.ClickExpansionIcon(VRptPage.VRptProjWOutActualsPvtGridBodyArea, "All Hours", 2);
Here is the method used to scroll into view and click the element:
/// <summary>
/// Expands the first instance in the grid of either All Days or All Hours header cell
/// <param name="pvtGridBodyAreaElement">The element that contains the body elements of any pivot grid. i.e. "Page.VRptErrorMeasuresPvtGridBodyArea"</param>
/// <param name="rowNumber">The exact row where the desired cell exists</param>
/// <param name="timeFormatToExpand">Either 'All Days' or 'All Hours', 'All Dates', or 'All 1/4 Hours'</param>
/// </summary>
public void ClickExpansionIcon(IWebElement pvtGridBodyAreaElement, string timeFormatToExpand, int rowNumber)
{
var cssSelectorString = string.Format("th[title='{0}']", timeFormatToExpand);
// Find the element that can be expanded
var expansionCapableElem = pvtGridBodyAreaElement.FindElements(By.CssSelector(cssSelectorString))[rowNumber - 1];
// Find the + or - icons within the parent cell.
var plusMinusIcon = expansionCapableElem.FindElement(By.CssSelector("span[data-expand]"));
var coordinates = plusMinusIcon.Location;
((IJavaScriptExecutor)Browser).ExecuteScript("window.scrollTo(0," + coordinates.Y + ")");
Thread.Sleep(2000);
plusMinusIcon.Click();
Thread.Sleep(2000);
}