This is a migrated thread and some comments may be shown as answers.
Custom step interigate DOM?
8 Answers 16 Views
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
Iron
Iron
David asked on 09 Apr 2021, 02:27 PM

Is it possible to make a custom coded testing step that iterates the current webpage DOM, finds all of the date fields and types in a date?

 

I am a C# coder but not that familiar with the object model of test studio so I could use a little starter code.

 

Thanks!

8 Answers, 1 is accepted

Sort by
0
Plamen Mitrev
Telerik team
answered on 12 Apr 2021, 12:41 PM

Hello David,

There are methods that I believe can help you achieve the exact scenario that you want to automate. You can use the Find.AllByTagName<>() or Find.AllByExpression<>() to get a collection of all elements that match the criteria.

Once you have this collection of elements, you can iterate it and execute custom logic against each of the elements. In your case, you need to type some text. This can be done with the following code example, which simulates the real user behavior and ensure that the focus is on the target element before typing.

Actions.SetText(yourElement, "");        // clear the current text        yourElement.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementCenterAtWindowCenter);
ActiveBrowser.Window.SetFocus();
yourElement.Focus();
yourElement.MouseClick();
Manager.Desktop.KeyBoard.TypeText("text", 50, 100, true); //enter the text with desktop keyboard actions

Please explore the above suggestions and code sample and test them on your end. I remain available to advise you further or answer any follow up questions that you might have.

Regards,
Plamen Mitrev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Test Studio course! Check it out at https://learn.telerik.com/.
0
David
Top achievements
Rank 1
Iron
Iron
answered on 12 Apr 2021, 01:29 PM

Is it possible to find all controls on a page that have a  specific class name like this control?

 

<input name="ctl00$ContentPlaceHolder1$txt_arrival" type="text" id="ContentPlaceHolder1_txt_arrival" style="width:75px;" class="hasDatepicker">

0
David
Top achievements
Rank 1
Iron
Iron
answered on 12 Apr 2021, 01:30 PM
I think I found it: // Find the first element with attribute class=myclassElement e = Find.ByAttributes("class=myclass");
0
David
Top achievements
Rank 1
Iron
Iron
answered on 12 Apr 2021, 02:34 PM

So this code finds the button as I see it highlighted but when it clicks it the web app blows up

 

            Element btn = Find.ByAttributes("value=Continue");
            
            if (btn != null)
            {
                System.Threading.Thread.Sleep(1000); 
                Actions.Click(btn);
            }

 

when I look at how test studio does it by looking at the test step generated code it does this and works

 

            // Click 'ContentPlaceHolder1BtnContinueSubmit'
            Pages.SynchronizedPredeployment7.ContentPlaceHolder1BtnContinueSubmit.Click(false);

 

but I need to find the button and click. Any ideas?

0
David
Top achievements
Rank 1
Iron
Iron
answered on 13 Apr 2021, 04:49 PM

ok we are having an issue with the find.Allbyattributes looking for controls. It works fine on a page that has the following controls:

 

<input name="ctl00$ContentPlaceHolder1$txt_arrival" type="text" id="ContentPlaceHolder1_txt_arrival" style="width:75px;" class="hasDatepicker">

<input type="submit" name="ctl00$ContentPlaceHolder1$btn_continue" value="Continue" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btn_continue&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ContentPlaceHolder1_btn_continue" style="cursor: pointer;">

 

*** This is the code for the first page that works:

// Find all elements with class=hasDatepicker and src has a type of text
            IList<Element> allItems = Find.AllByAttributes("class=hasDatepicker", "type=text");
            
            foreach (Element item in allItems)
            {
                Actions.SetText(item,"13/13/1313");
                System.Threading.Thread.Sleep(1000); 
            }       
            
           
            HtmlInputControl btn = Find.ByAttributes<HtmlInputControl>("value=Continue");
            System.Threading.Thread.Sleep(3000);    
            
            if (btn != null)
            {
                btn.Focus();
                btn.MouseClick();
            }

 

*** The 2nd page that the find does not work on has these controls:

<input name="ctl00$ContentPlaceHolder1$txtPOPStart" type="text" maxlength="10" id="ContentPlaceHolder1_txtPOPStart" tabindex="3" class="hasDatepicker">

<input type="submit" name="ctl00$ContentPlaceHolder1$btnSearchContracts" value="Search for Contracts" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btnSearchContracts&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ContentPlaceHolder1_btnSearchContracts" tabindex="13" style="cursor: pointer;">

 

*** This is the test code for the 2nd page that does not find either of the controls

IList<Element> allItems = Find.AllByAttributes("class=hasDatepicker", "type=text");
            
            foreach (Element item in allItems)
            {
                Actions.SetText(item,"13/13/1313");
                System.Threading.Thread.Sleep(1000); 
            }       
            
            MessageBox.Show(window, "# Dates found: " + allItems.Count());
           
            HtmlInputControl btn = Find.ByAttributes<HtmlInputControl>("value=Search for Contracts", "type=submit");

 

** We even have searched for the exact control names but it finds nothing. HELP!!!!!

0
Plamen Mitrev
Telerik team
answered on 14 Apr 2021, 04:35 PM

Hello David,

Thank you for sharing a sample from your application and the code that you have tested on your end. I used that to host a local page that has two inputs and two buttons, same as yours, and prepared a sample web test with a coded step that enters text in both inputs and clicks the buttons. Please find more details below and the sample test attached to this reply.

Since you already know the type of elements you are getting from the Find.AllByAttributes() method -> HtmlInputText, I would advise you to specify their type directly there. This makes it easier to work with those elements using the Telerik Testing Framework. 

I adjusted your code to set the text exactly at the target element of each iteration. That way the text is set exactly for the specified element and you don't need to take care of which element is on focus at the moment of execution. I also made some small changes to your code that clicks the buttons to make sure that they are visible on the screen, before they are clicked.

You can record actions or verifications with the Test Studio standalone product and convert the steps into coded steps. That way you can explore what series of methods is used in a single step. Please keep in mind that Test Studio stores the target elements in the Elements Explorer and references them in the coded step from the Pages object. If you are using only coded steps, I advise you to use a suitable Find.By...() method.

Please explore the sample page and attached web test with coded step on your end and use similar logic to automate your test scenario. If you encounter any troubles, please share the DOM tree of the tested application and your current code, as well as the results from the test execution.

Thank you for your cooperation in this discussion.

Regards,
Plamen Mitrev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Test Studio course! Check it out at https://learn.telerik.com/.
0
David
Top achievements
Rank 1
Iron
Iron
answered on 14 Apr 2021, 08:19 PM
It turns out that adding this: Manager.ActiveBrowser.RefreshDomTree(); at the beginning of the cod3ed step fixed everything! Awesome!
0
Plamen Mitrev
Telerik team
answered on 15 Apr 2021, 09:29 AM

Hello David,

I am happy to hear that refreshing the DOM tree improved the execution of your test scenario. I assume that some actions before the coded step change the content of the page or redirect it to a different page.

You can consider the suggestions from my previous reply, which are general guidelines on how to use the Find.By... methods to make your tests more stable. Let me know if you experience any issues or need further assistance.

Thank you for providing feedback about your adopted solution.

Regards,
Plamen Mitrev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Test Studio course! Check it out at https://learn.telerik.com/.
Asked by
David
Top achievements
Rank 1
Iron
Iron
Answers by
Plamen Mitrev
Telerik team
David
Top achievements
Rank 1
Iron
Iron
Share this question
or