Finding and clicking dynamic buttons

3 posts, 1 answers
  1. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 29 Feb 2012 Link to this post

    I need help to find and identify screen objects whose name changes.
    Specific Scenario Example
    I have a screen with a Logout button.  The visible text on the button is 'Logout ' plus the name of the logged on user (e.g. 'Logout Admin').  When I record the action of clicking on the button in Test Studio the script supplies a line that reads:
    Click 'LogoutSpan'
    Click 'LogoutSpan0'
    or similar.
    How can I script so that the action works regardless of any number that may be appended to the 'LogoutSpan' part?
    In fact, how can I identify anything (e.g. a 'Div') where its name or ID changes dynamically?

    I thought that maybe some VB in the Code Behind window would be a solution, perhaps using a regular expression.  In the case of the button I came up with:

    which I think will find any screen object with 'Logout' in its name, but got no further.  I tried to write an excution line, something like:
    but the type-ahead prompt in the Code Behind window didn't have 'Button.Click' as an option.

    I'm not a code wizard, and don't really understand regular expressions (and yes, I've read the available on-line documentation) so I would very much appreciate some help with this.  If I have a concrete example of how to resolve the above then I can build on it for the future.

    Nigel Edwards, Transition Computing.



  2. Answer
    Anthony avatar
    19 posts

    Posted 29 Feb 2012 Link to this post

    Hello Nigel,

    First I'd like to clear up some confusion on How the Element Repository Works. If a new element is added each time you click the button when the text differs, then it likely does not have a unique attribute (like ID or name) attached to it. My guess is that Test Studio is crafting the Find Expression of each newly added element based on text content. If that's the case you can easily edit the Find Expression to locate the element based on "textcontent contains logout".

    To proceed in code you'll need to inspect the HTML surrounding that element. It seems Test Studio is referencing a <span> element and not a <button>. In that case you can use the following VB code:

    Dim s As HtmlSpan = Find.ByExpression(Of HtmlSpan)("textcontent=~logout", "name=logout")

    Substitute textcontent and name for whatever attribute you'd like, or only reference one attribute instead of two. See here for more information.

    You can also identify and act upon elements the standard way, but it's not as robust as the HTML Control Element Wrappers Suite:

    Dim e As Element = Find.ByExpression("textcontent=~logout")

    See here for more info.

    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  3. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 01 Mar 2012 Link to this post

    Hi Anthony,

    Once again you've provided a great solution, so many thanks for your efforts.
Back to Top