Forum Discussion

MarcusBengtsson's avatar
MarcusBengtsson
Frequent Contributor
2 years ago

How to do when ID always changes

Hello! I have this checkbox that seems to be changing id every time I re-run the test (on web)

 

it looks like this 

//div[@id='gridIKgbvJEL']//tr/td/i[contains(@class, 'c-grid-checkbox')]")

last time it was like this

//div[@id='gridbezutZNT']//tr/td/i[contains(@class, 'c-grid-checkbox')]

 

so I was trying out if it was possible to do some sort of wild card for it as it seems to only be changing the "grid...." portion and it would look like this

//div[@id='grid*']//tr/td/i[contains(@class, 'c-grid-checkbox')]

 

but I assume it couldnt be as easy as this no? Do you guys have any suggestions. 

11 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You should ask that question to the developers of the application! The control could be dynamically generated. 

     

    I also have something similar, on the application that I test, where the "id" is always changing. I have to use different properties to identify the object instead. I'm not using XPath, which makes things easier!

     

    XPath example for the above control,

     

    Question. Is it necessary for you to use XPath? Are you testing on Windows, macOS, Linux, Android and iOS?

     

    • MarcusBengtsson's avatar
      MarcusBengtsson
      Frequent Contributor

      I am testing on Windows, and its on the web. I dont think its "necessary" to use xpath really haha. 

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If that's the case, then don't use XPath!

     

    Have the following enabled/disabled (global settings)

    Use Tree model (project settings),

    Only map objects that you are interacting with, 

    Click yes,

    Skip mapped names (TC will automatically identify name and properties to be used), until you get to the last mapping i.e. 6/6

    Provide a suitable name, and add any additional properties (it has to be unique)

    Mapped objects and aliases (the name you provided) looks like this,

     

    If textnodeHeading can not be highlighted, then the properties used in each of the objects in the tree structure has changed.

     

    Identify the object,

    and correct it, either using wildcards or full URL

     

    This is how I build my mapped objects for the web application that I test. It might be long-winded, but it works great for a web application that was never indented or built for automation. Majority of all the objects don't have the "id" attribute in the web application that I test.

     

     

  • eykxas's avatar
    eykxas
    Regular Contributor

    I disagree with this opinion ^^. Xpath is great thing and the most reliable (and faster) method to get any object on a web page. But, since TC only support Xpath 1.0 syntax, it requires some tricks to built it in the right way.

    For exemple in this page (the TC forum page where we are) the button "Reply" has a part generated in its id. its look like this : 
    //input[@id="submitContext_75b1b600eccb34_0"]

    To get the button in the right way, We must identify the others components in the page and use some of them as "checkpoint" to be sure that we get the button always in the same way.

    so the xpath become : //div[contains(@class, 'lia-inline-message-reply')]//div[contains(@class, 'lia-form-submit')]//input[@value='Reply']

    in this way we get the reply panel, then the button group, and finally the reply button. As long as the architecture of the TC website (and/or css) doesn't change, this path is always the same.

    namemapping is great too, but it can be pretty tricky to maintain when we have dozens of objects. Xpath is much more simpler.

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Xpath is great thing and the most reliable (and faster) method to get any object on a web page.

      Actually, native TestComplete's object identification is much more functional and handy.

      The only problem is that it will not work if the test is executed under Appium control (this is the case when TestComplete tests are executed on iOS, Android). So, if there is a chance that your tests will be executed not only on Windows, you have no other choice but use XPath.

      If your tests are for Windows execution only, than native object identification is much better choice.

       

      P.S. I would hope that SmartBear can make native NameMapping to be compatible with non-Windows environments, but I am afraid that this might be not worth required efforts... 😞

       

    • MarcusBengtsson's avatar
      MarcusBengtsson
      Frequent Contributor

      I tried to create an "If ... Then" so that IF this window exists it will do theses steps and then an ELSE to do this steps if the first condition was not meant. 

       

      Is this the correct way to write it? Equals exists? so if Browser.TestTemplateDetails exists it will continue with "If ... Then" if it doesnt exist it will continue with "ELSE".?

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    For example,

    This is checking the button property of Enabled, to see if it's equal to false.

     

    Whereas, this is checking if the object exists,

     

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    As per rraghvani, check if this work:

    //div[starts-with(@id, 'grid')]//tr/td/i[contains(@class, 'c-grid-checkbox')]