Forum Discussion

nedbacan's avatar
nedbacan
Frequent Contributor
2 years ago
Solved

Trying to call the FindChild method or property of an object but it does not exist

Hello, can someone help debug the following script, I cannot figure it out why it's failing being I am new to TC.

 

I included the screenshot of the properties from the Spy captured,I am sure the page ID is correct according to the spy screen.

 

function DragCard()
{
// Find the first column element
   var column1 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindChild("idStr", "chk-drop-list-0", 10);

// Find the card element in the first column with the desired text content
   var cardToMove = column1.FindChild("contentText", "Test Patient455", 10);

// Find the second column element

   var column2 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindChild("idStr", "chk-drop-list-2", 10);

// Drag the card to the second column
   cardToMove.Drag(column2);
}

 

 

  • I was able to achieve the drag functionality using the following function that was shared by someone else in the community. 

     

    function dragCalculatedDistance() {
         startObject = Aliases.pageEmailAnalyticsGettingStarted.workspace_FirstEmail_Val
         destinationObject = Aliases.pageEmailAnalyticsGettingStarted.FindElement("//li[contains(.,'My Private Folder')]")
         var startPoint, targetPoint, dragX, dragY;
    // Drag from the center of object A to the center of object B
    // destinationObject.Click()

         startPoint = startObject.WindowToScreen(startObject.scrollWidth /2, startObject.scrollHeight /2);
         targetPoint = destinationObject.WindowToScreen(destinationObject.scrollWidth/2, destinationObject.scrollHeight /2);

         dragX = targetPoint.X - startPoint.X+50; 
         dragY = targetPoint.Y - startPoint.Y-80;

    //Note the +50 and -80 is something I used to make sure the coordinates can adjust and move to the object correctly.

         startObject.Drag(-1, -1, dragX, dragY);

    }

    Let me know if this helps.

  • The coding provided by tristaanogre will work fine when moving the object horizontally, on any screen size. It's best to use the function and pass in the parameters for column 1 (source) and column 2 (destination).

     

    I only suggested Keyword Testing, as I wasn't sure whether the correct method should be Drag or Move. You'll have more control with scripting, so I suggest to stick with that 🙂

     

    Another approach, depending on whether your web application is JavaScript, is to use removeChild() and appendChild(). For example,

    Which moves the child object "img_w3slogo.gif" from parent 1 "div1" to parent 2 "div2".

     

     

     

12 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you see Drag Method or Drag Action, the parameters it accepts are coordinates. You are passing in an Object, which will not work.

     

    Can I make a suggestion - use Keyword Testing to record your actions, and then convert the keyword test to script. TC should have recorded the click and move of your item - make a note of the methods it has recorded.

     

    In the last picture, three columns are shown. If you get the bounding rectangle of the third column, minus the bounding rectangle of the first column, this will give you the number of pixels, to move the mouse across.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The coding provided by tristaanogre will work fine when moving the object horizontally, on any screen size. It's best to use the function and pass in the parameters for column 1 (source) and column 2 (destination).

     

    I only suggested Keyword Testing, as I wasn't sure whether the correct method should be Drag or Move. You'll have more control with scripting, so I suggest to stick with that 🙂

     

    Another approach, depending on whether your web application is JavaScript, is to use removeChild() and appendChild(). For example,

    Which moves the child object "img_w3slogo.gif" from parent 1 "div1" to parent 2 "div2".

     

     

     

  • tvklovesu's avatar
    tvklovesu
    Frequent Contributor

    Please provide more details of where it is failing and the error message it is throwing. Also if possible provide the source code sample of those elements.

    • nedbacan's avatar
      nedbacan
      Frequent Contributor

      Oops please excuse for forgetting to add the error, I thought I did from the start.  

       

      The error is happening on line 8 (see screenshot).  I am reviewing what other properties to use, the contentText seems to be the only unique property I can identify specific cards. Please if you can review the Patient Card screenshot that I previously attached and advise what other method I can use to identify the card to drag.   

       

      One other question beside the error, is it possible to place wildcards in text being searched, for an example  FindChild("contentText", "Patient003 Patient003??", 10);

       

      Thank you very much for your help.

       

       

      • tvklovesu's avatar
        tvklovesu
        Frequent Contributor

        Many times, I do have the same issue with FindChild. Instead, I use either FindChildByXpath or FindElement. Also, in the case of contentText use innerText.

         

        unction DragCard()
        {
        // Find the first column element
           var column1 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindElement("[idStr='chk-drop-list-0']");

        // Find the card element in the first column with the desired text content
           var cardToMove = column1.FindChildByXpath("//div[contains(text(),'Test Patient')]");

        // Find the second column element

           var column2 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindElement("[idStr='chk-drop-list-2']");

        // Drag the card to the second column
           cardToMove.Drag(column2);
        }

         

        For the other question to use Wildcard. you can do either using xpath or css selectors in both FindElement and FindChildByXpath methods as I mentioned in the card to move line above

  • nedbacan's avatar
    nedbacan
    Frequent Contributor

    Thank you for your help.

     

    I tried using your example, but it cannot find the attribute, it returned with an error.

    I checked it with the spy tool, and it seems correct, I did a few screenshots to see if it may help or if there are another method of finding the property chdk-drop-list-0?

     

    ERROR

    CODE

     

    PROPERTIES

    Inspection of Element

     

    • tvklovesu's avatar
      tvklovesu
      Frequent Contributor

      In your error I see a : next to idStr like FindElement("[idStr:='chk-drop-list-0']"). In my example that I previously mentioned that : is not there. Make sure if you have mistyped it. Also from your source code I see that the attribute is not idStr its id='chk-drop-list-0'. So you can use just id as below

       

       var column1 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindElement("div[id='chk-drop-list-0']");

       

      One more thing as it shown in the object spy window you can use like

       var column1 = Sys.Browser("chrome").Page("https://rdx-dev.rdxhub.com/workspace/patient-board").FindElement("#chk-drop-list-0");

       

      Let me know if this helps.

      • nedbacan's avatar
        nedbacan
        Frequent Contributor

        Hi, I was able to surpass the original error (object not found) but when I get to the line 15 (last line),

        I get a runtime error.  Also see attached video for the step through of the code.

         

        Am I using the right approach to drag a card to another column without using coordinates?   See attachment for GUI screenshot.

         

         

         

         

         

         

         

         

         

         

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    It is possible to use wildcards in your search criteria.

     

    Also, the FindElement method returns an object that matches the specified search criteria, and if no matching object was found, the method returns an empty value, null in JavaScript. You're passing an Object, into Drag method, which only accepts coordinates - this will cause an error.

  • tvklovesu's avatar
    tvklovesu
    Frequent Contributor

    I was able to achieve the drag functionality using the following function that was shared by someone else in the community. 

     

    function dragCalculatedDistance() {
         startObject = Aliases.pageEmailAnalyticsGettingStarted.workspace_FirstEmail_Val
         destinationObject = Aliases.pageEmailAnalyticsGettingStarted.FindElement("//li[contains(.,'My Private Folder')]")
         var startPoint, targetPoint, dragX, dragY;
    // Drag from the center of object A to the center of object B
    // destinationObject.Click()

         startPoint = startObject.WindowToScreen(startObject.scrollWidth /2, startObject.scrollHeight /2);
         targetPoint = destinationObject.WindowToScreen(destinationObject.scrollWidth/2, destinationObject.scrollHeight /2);

         dragX = targetPoint.X - startPoint.X+50; 
         dragY = targetPoint.Y - startPoint.Y-80;

    //Note the +50 and -80 is something I used to make sure the coordinates can adjust and move to the object correctly.

         startObject.Drag(-1, -1, dragX, dragY);

    }

    Let me know if this helps.

  • nedbacan's avatar
    nedbacan
    Frequent Contributor

    I appreciate everyone's help in supporting me with my question.  My initial goal was to drag the card from one column to another without having to use coordinates which what KB1  and tvklovesu provided in his sample code, but according to rraghvani this seems impossible based on the Drag method, so I will use Keyword Testing.  BUT ....I do not feel confidence using coordinates will run successful all the time, I feel the script will fail if used on another computer other than where the keyword was created on.

     

    https://community.smartbear.com/t5/TestComplete-Questions/Drag-a-card-through-different-columns-by-name/m-p/242827 

    • tvklovesu's avatar
      tvklovesu
      Frequent Contributor

      nedbacan, if you hardcode the coordinates then it will fail when you run on any machine. Even if you run on the same machine where you recorded might fail sometimes. But if you get the coordinates at runtime then it will use the exact location of the object no matter what machine and browser you use it will still be able to find the object and drag it to the destination. In my script that I provided, it will get the values and then pass them in the drag method. If you are still not sure give a try and let me know, how did it go.