SpreadJS 16.1 with Selenium tests

Posted by: petri.rasanen on 8 May 2023, 11:35 pm EST

  • Posted 8 May 2023, 11:35 pm EST

    Hi,

    With SpreadJS 16.0 I was able to trigger events in Selenium tests like this:

    var sheet = spread.getActiveSheet();

    sheet.Du().trigger(“CellDoubleClick”, {

    sheet: sheet,

    sheetName: sheet.name(),

    sheetArea: 3,

    row: 3,

    col: 5

    });

    Other events we used to trigger were “ActiveSheetChanged” and “CellClick” (to click on header column for sorting).

    Now with SpreadJS 16.1 version Du() is not supported anymore. How could we trigger events in Selenium? We must perform these kind of tests in our program, because these events have code that must be tested.

    Br,

    Petri

  • Posted 9 May 2023, 11:15 pm EST

    Hello Petri,

    Please note that you are using a method the sheet.Du() which is not exposed in the public API. This method is an internal method which is minified and uglified when SpreadJS files are build. It is not recommended to use this method (or other methods which are not exposed in public API) as its name may change in the future versions.

    Instead of performing double click operation with sheet.Du().trigger() method, you need to perform double click operation with the help of selenium. This is the recommended way to perform the double click operation.

    When the selenium web driver opens a web page, you can get the SpreadJS instance using GC.Spread.Sheets.findControl() method. You can find the co-ordinates of any cell on sheet using sheet.getCellRect() method and perform double click operation with selenium based on cell co-ordinates.

    Please refer to the attached selenium sample with c# which demonstrates how you can find co-ordinates of cell by executing javascript in the web page’s context.

    Please let us know if you face any issues. You may refer to another forum case that discusses similar issue: https://www.grapecity.com/forums/spreadjs/programmatically-trigger-gc-spread-sheets-events-selectionchanged

    Doc references:

    GC.Spread.Sheets.findControl():https://www.grapecity.com/spreadjs/api/v16/modules/GC.Spread.Sheets#findcontrol

    sheet.getCellRect():https://www.grapecity.com/spreadjs/api/classes/GC.Spread.Sheets.Worksheet#getcellrect

    Regards,

    Ankit

    Sample: SaleniumTest.zip

  • Posted 13 June 2023, 12:32 am EST

    Hi,

    How can I click a certain sheet in workbook with selenium if I know sheet name or sheet index?

    I know how to execute setActiveSheet with Spread but that does not trigger event ActiveSheetChanged.

  • Posted 13 June 2023, 8:26 pm EST

    Hello Petri,

    The Events are triggered through the UI actions like when the user click on the tab strip to select a sheet. Events are not triggered when using the APIs like setActiveSheet method. This behavior is by design and expected.

    You could use the following code snippet to get the coordinates of the sheetTabRect. Once you get the coordinates, you could use the Selenium APIs to perform the click on the coordinates and it will trigger the events like ActiveSheetChanged.

      // Get the Sheet's Rectange (x, y coordinates and widht and height of the tab rectangle)
      getSheetTabRect(spread: any, sheetName: string) {
        // Sheet Doesn't exist
        if (spread.getSheetIndex(sheetName) === undefined || null) {
          return null;
        }
        var hostID = spread.getHost().id;
        var tabStripHost = document.getElementById(hostID + "_tabStrip");
        if (hostID === "") {
          tabStripHost = document.getElementById("null_tabStrip");
        }
        var offset = getOffset(tabStripHost), width = tabStripHost?.clientWidth, height = tabStripHost?.clientHeight;
        var startX = 0, acTabWidth, flag = false, activeSheetIndex = spread.getSheetIndex(sheetName);
        for (var x = 0; x < width!; x++) {
          var hitInfo = spread.hitTest(x + offset!.left, height! / 2 + offset!.top);
          var tabStripHitInfo = hitInfo && hitInfo.tabStripHitInfo;
          if (tabStripHitInfo && tabStripHitInfo.sheetTab) {
            if (tabStripHitInfo.sheetTab.sheetIndex === activeSheetIndex) {
              if (startX === 0) {
                startX = x;
              }
              flag = true;
            } else if (flag) {
              acTabWidth = x - startX;
              break;
            }
          } else if (flag) {
            acTabWidth = x - startX;
            break;
          }
        }
        return { x: startX + offset!.left + 5, y: offset!.top, width: acTabWidth, height: height! - 5 };
      }
    
    
      getOffset(elem: any) {
        var docElem, win, box = { top: 0, left: 0 }, ownerDocument = elem && elem.ownerDocument;
        if (!ownerDocument) {
          return;
        }
        docElem = ownerDocument.documentElement;
        if (elem.getBoundingClientRect) {
          try {
            box = elem.getBoundingClientRect();
          }
          catch (e) {
          }
        }
        win = ownerDocument.defaultView;
        return {
          top: box.top + (win.pageYOffset || docElem.scrollTop) - (docElem.clientTop || 0),
          left: box.left + (win.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || 0)
        };
      }

    Kindly let us know if you still face any problems.

    Regards,

    Ankit

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels