GC is undefined error message when automating SpeadJS with Selenium Java

Posted by: sanjana.patel on 2 June 2021, 12:52 am EST

    • Post Options:
    • Link

    Posted 2 June 2021, 12:52 am EST

    I am trying automate spreadJs using selenium with Java. Below is my code snippet.

    JavascriptExecutor js = (JavascriptExecutor)driver;
    		
    		WebElement hostDiv = driver.findElement(By.id("vp_vp"));
    		
    		// get spreadInstance and save it on window object for easy access
    		js.executeScript("window.testSpread = new GC.Spread.Sheets.findControl(arguments[0]);", hostDiv);
    		// get Cell value of cell 1,1
    		js.executeScript("return window.testSpread.getActiveSheet().getValue(1,1);");
    		String value = (String) js.executeScript("return window.testSpread.getActiveSheet().getValue(1,1);");
    		// test value
    		 Assert.assertEquals("1", value);
    

    This is from where I am taking canvas id:

    <canvas id="vp_vp" gcuielement="gcWorksheetCanvas" width="1836" height="1" style="width: 1530px; height: 1px; cursor: default;" xpath="1">You need a browser which full supports HTML5 Canvas to run SpreadJS</canvas>
    

    I am getting ‘GC not defined’ error. Could you please check & let me know whats wrong?

    Please note, in my application, spreadJs is not used inside sandbox or iFrame.

  • Posted 2 June 2021, 4:16 am EST

    Hi Sanjana,

    This error usually comes when the GC object is not globally available, Please make sure the script has access to the GC object.

    For doing this you need to attach the GC object to the window. Please refer to the following code snippet and let us know if you face any issues.

    //in the project 
    import * as GC from "@grapecity/spread-sheets"
    window.GC = GC
    
    

    Regards

    Avinash

  • Posted 5 August 2021, 6:47 am EST

    Hi Avinash,

    I added the above mentioned code in my project’s main.js file as :

    import * as GC from '@grapecity/spread-sheets';
    window['GC'] = GC;
    

    I am now getting another issue.

    Below is my code:

    Approach 1 :

    js.executeScript(“window.testSpread = GC.Spread.Sheets.findControl(document.getElementById(‘vp_vp’))”);

    js.executeScript(“return window.testSpread.getActiveSheet().getValue(1,1);”);

    String value = (String) js.executeScript(“return window.testSpread.getActiveSheet().getValue(1,1);”);

    // test value

    Assert.assertEquals(“1”, value);

    Error:

    org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'getActiveSheet' of undefined
    

    Approach 2:

    WebElement hostDiv = driver.findElement(By.id(“vp_vp”));

    js.executeScript(“window.testSpread = new GC.Sheets.Spread(arguments[0]);”, hostDiv);

    //Set Cell Value

    js.executeScript(“window.testSpread.getActiveSheet().setValue(0,0,‘1’);”);

    Error:

    org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'Spread' of undefined
    

    Can you please suggest what additional changes are required.

  • Posted 6 August 2021, 1:31 am EST

    Hi Sanjana,

    Regarding the Approach 1:

    It looks like the find control method doesn’t return the correct instance of the workbook. Please make sure you have passed the correct host element inside the find control. You may also try the following code snippet. Further, if the issue persists. Please share a small sample that replicates the issue so that we could investigate it further.

    
    window.testSpread = GC.Spread.Sheets.findControl(document.querySelector("div[gcUIElement='gcSpread']"))
    
    

    Approach 2:

    From the code snippet, it looks you have used wor syntax for creating workbook instance. Please refer to the following code snippet and let us know if you face any issues.

    
    window.testSpread = new GC.Spread.Sheets.Workbook(hostElement);"
    
    

    Regards

    Avinash

Need extra support?

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

Learn More

Forum Channels