Forum Discussion

Rajesh2's avatar
Rajesh2
Contributor
8 years ago
Solved

How to fetch the data from a Grid/dataset/table?

Hi,

 

Greetings,

 

NOTE: Please solve my queries on the basis of Python language as i am working in Python

 

I am working in a project where in we are using TestComplete with Python language. I have some queries, Can anyone please clarify my queries as soon as possible..

 

Query1:

How to fetch the data from a Grid/dataset/table? I have attached the Screenshot of the Grid. Can anyone please let me know how to fetch the firstname or lastname. When i object spy on the grid it is not spying the data present in it, but it is spying on the Grid layout.

I have tried with adding it into a table in TC and then fetching from it. But it is tidious as we need to keep on updating the table manually everytime it gets updated. Please suggest any other solutions.

NOTE: Screenshot attached(GridImage.png)

 

Query2:

I need to fetch the data from excel file and send that data as argument in the application. I am able to fetch using DDT driver. driver.Value[0 or 1 or 2...] but i want to fetch the data on the basis of the column Name. Like if the Column name is FirstName then it should fetch the data from that column only. Can anyone please suggest how to go about this?

 

Query3:

How to get a snapshot on the failure of the script?

 

Query4:

Can we get the report in Excel format.

  • Hi baxatob Thank you so much for the solution.

     

    But we are facing some issue. It is returning the value of only first row. 

     

    As Pavan name was in first row it returned the Age of that Name.

     

    But when i gave Log.Message("Raj") which is in third row, the code isn't working.

     

    while not source.EOF():
            if source.Value["NAME"] == name:
                return source.Value["AGE"]
            source.Next()

     

    Can we know why it is not working for the same. If you know the solution, Can you please help us with the same..!!

  • Hi,

     

    You should call the get_value() routine within Log.Message():

     

    Log.Message(get_value("Raj"))

    It works for me.

  • baxatob's avatar
    baxatob
    8 years ago

    In your case get_value() is a class method. You should use a keyword self as a first argument, while defining your function:

     

    class Excel:
        
        def get_value(self, RowValue, ColumnName, name):
            ...

     

     

    P.S. You do not need these delays : Delay(1000)

  • At first you need to import your class:

     

    from Script1 import Excel
    
    class Common_Functions(Excel):
        ...

25 Replies

  • baxatob's avatar
    baxatob
    Community Hero

    1. Please go carefully through this topic: https://support.smartbear.com/testcomplete/docs/app-objects/specific-tasks/grids/index.html 

     

    2. You can create a DBTable variable from your excel file. Then you can use it something like this:

     

    def get_value(column_name):
    
        source = Project.Variables.your_dbtable_variable
        source.Reset()
        while not source.IsEOF():
            Log.Message(source.Value[column_name])
            source.Next()

    The code above will save all values under the column_name into the Log.

     

    3. Enable Post image on error option in your Project -> Properties -> Playback -> Log

     

    4. You can't do it out-of-the-box, however you can create your own script or extension for this purpose.

    • Rajesh2's avatar
      Rajesh2
      Contributor

      Hi baxatob, Thank you for the response.

       

      1. The link which you have sent is throwing 404 error. Can you please check the same?

       

      2. Thank you, Will check the same.

       

      3. I want the screen shot to be stored in the desktop folder. How to store it externally with log files.

       

      4. Ok, Will try with the same. By creating our own method to fetch into excel report.

      • baxatob's avatar
        baxatob
        Community Hero

        Hi,

         

        I have updated the link >>

         

        You can use the following code for taking a screenshot:

         

        def take_screenshot(path):
            Sys.Desktop.Picture().SaveToFile(path)


         path should be a string with full path to your destination, e.g. "C:\\Users\\User\\Desktop\\shot.jpg"
        Sys.Desktop will provide a screenshot from the whole desktop. You can use any mapped object instead of Sys.Desktop

         

         

         

         

         

    • pavanmac's avatar
      pavanmac
      Contributor

      Hi could you please elaborate on fetching the data from excel using their column names.

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Look into using the DDT.ExcelDriver.  Once the driver is set up, accessing the data is a matter of objExcelDriver.Value('ColumnName').

  • baxatob's avatar
    baxatob
    Community Hero
    Hi Rajesh2

    First I created a Project variable of DBTable type, that uses Excel file as a data source. (We can do it through the wizzard in Project/Variables menu).

    Then I call my variable from the script.


    • pavanmac's avatar
      pavanmac
      Contributor

      I dont want to store in the variable but i want to fetch directly from the excel . 

       

      For Ex: My excel is saved on desktop. 

       

      Please suggest how to go about this.

      • baxatob's avatar
        baxatob
        Community Hero

        As tristaanogre said you can also use DDT.ExcelDriver. It uses absolutely the same logic:

         

        def get_value(name):
        
            source = DDT.ExcelDriver("C:\\Users\\Pavan\\Desktop\\Data2.xls", "Sheet1")
            
            while not source.EOF():
                if source.Value["NAME"] == name:
                    return source.Value["AGE"]
                source.Next()