This is a migrated thread and some comments may be shown as answers.

Code Find Logic Help Needed

5 Answers 105 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
ServiceDesk
Top achievements
Rank 2
ServiceDesk asked on 27 Aug 2014, 02:01 PM
Hello,

I have this peace of html code. See below. Now what I want to is to check if the first product (class="product-item is-full") contains "Verzekeruzelf.nl" if so click the second product the "Verder"anchor. 

I am trying all kinds of things but nothing seems to work for me. See also my C# code.

<div class="product-items" id="product-items">               
        <div class="product-item is-full">
            <div class="product-item-header">
                <span class="icon-prijs-kwaliteit">
                    <span class="inner-circle">1</span>
                </span>
                <div class="row-fluid row-fluid-centered">
                    <div class="grid2of3">
                        <div class="product-item-company">Verzekeruzelf.nl</div>
                    </div>                  
                </div>
            </div>
 
            <div class="product-item-body s-visible-block">
                <div class="row-fluid row-fluid-centered">
                    <div class="grid2of3">
                        <span class="price-quality-label">Prijs-kwaliteit</span>
                        <div class="price-quality-score">100</div>
                    </div>                   
                </div>              
            </div>
 
            <div class="product-item-footer">
                <div class="product-item-price"><sub>per maand: </sub>30,<sup>21</sup></div>
                <a href="/autoverzekering/details/overzicht/50162" class="btn-purple">Verder</a>
            </div>
        </div>
 
        <div class="product-item is-full">
            <div class="product-item-header">
                <span class="icon-prijs-kwaliteit">
                    <span class="inner-circle">2</span>
                </span>              
                <div class="row-fluid row-fluid-centered">
                    <div class="grid2of3">
                        <div class="product-item-company">Allsecur</div>
                    </div>                  
                </div>
            </div>
 
            <div class="product-item-body s-visible-block">
                <div class="row-fluid row-fluid-centered">
                    <div class="grid2of3">
                        <span class="price-quality-label">Prijs-kwaliteit</span>
                        <div class="price-quality-score">92</div>
                    </div>                  
                </div>              
            </div>
 
            <div class="product-item-footer">
                <div class="product-item-price"><sub>per maand: </sub>30,<sup>48</sup></div>
                <a href="/autoverzekering/details/overzicht/50869" class="btn-purple">Verder</a>
            </div>
        </div>
    </div>

I get the message:

LOG: -------------- allProducts ELEMENT FOUND: HtmlDiv:<DIV class=product-item-header>
LOG: -------------- productOne ELEMENT NOT FOUND  --------------

ActiveBrowser.RefreshDomTree();
  
//HtmlDiv productOne = Find.ByExpression<HtmlDiv>("class=product-item-header"); //Grab the first company
  
IList<HtmlDiv> allProducts = Find.AllByAttributes<HtmlDiv>("class=product-item-header");
 
if(allProducts != null) {
     
    Log.WriteLine("-------------- allProducts ELEMENT FOUND: " + allProducts[0].ToString());
     
    //HtmlDiv NameCompanyOne = top3[0].Find.ByNodeIndexPath<HtmlDiv>("1/0/0");
    //Log.WriteLine("-------------- VERZEKER ONE NAME: " + NameCompanyOne.InnerText);
     
    HtmlDiv productOne = allProducts[0].Find.ByAttributes<HtmlDiv>("class=product-item-company");
     
    if( productOne != null) {
        if ( productOne.InnerText.Contains("Verzekeruzelf.nl") ) { //Check if it's Verzekeruzelf.nl  
            Log.WriteLine("-------------- VERZEKER U ZELF FOUND  --------------");
         
            //First company is Verzekeruzelf.nl           
            HtmlAnchor anchorCompanyTwo = allProducts[1].Find.ByExpression<HtmlAnchor>("value=Verder");   //Grab Verder link
            anchorCompanyTwo.Click();   //Click it
        } else {   
            Log.WriteLine("-------------- NO VERZEKER U ZELF FOUND  --------------");
             
            //First company not is Verzekeruzelf.nl
            HtmlAnchor anchorCompanyTwo = allProducts[0].Find.ByExpression<HtmlAnchor>("value=Verder");   //Grab Verder link
            anchorCompanyTwo.Click();   //Click it 
        }  
    } else {
        Log.WriteLine("-------------- productOne ELEMENT NOT FOUND  --------------");
    }
} else {
    Log.WriteLine("-------------- allProducts ELEMENT NOT FOUND  --------------");
}

Hope someone can help me out?

Thanks,
Bjorn

5 Answers, 1 is accepted

Sort by
0
Miguel
Top achievements
Rank 1
answered on 28 Aug 2014, 07:21 PM
Hi Roel,
When you are doing this kind of tests in Test Studio I've found that the best way to do what you describe is to add the list you are using to your project elements and navigate it through there instead of finding it every time in the Html. Knowing the structure of your element is still helpful.

    HtmlAnchor anch = null;
    foreach(HtmlTableRow row in Pages.testPage.ContetTypeBehaviors.contentTypeTable.Rows)
    {
        if(row.Cells[0].ChildNodes[0].TextContent.Equals("testType"))
        {
            anch = row.Cells[0].ChildNodes[0].As<HtmlAnchor>();
            anch.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementTopAtWindowTop);
            anch.MouseClick(ArtOfTest.WebAii.Core.MouseClickType.LeftClick, 0, 0, ArtOfTest.Common.OffsetReference.AbsoluteCenter);
            break;
        }
    }

Here I extract the rows from the table and knowing its structure I click on the desired object.
Hope this helps.
0
ServiceDesk
Top achievements
Rank 2
answered on 29 Aug 2014, 10:06 AM
The problem is here that the products in the top 3 can change everyday. So your product ID where you want to search for is not always on the page. Thats why I cannot add it to to library and just click on the id. If that was so then I did not need to have this step in code.

So I don't see how your solution can help me.
0
Ivaylo
Telerik team
answered on 01 Sep 2014, 06:47 AM
Hello Bjorn,

I can offer you an easier way on how to achieve what you are trying to do using the following code:

IList<HtmlDiv> divList = ActiveBrowser.Find.AllByAttributes<HtmlDiv>("class=product-item is-full");
int i = 0;
foreach(HtmlDiv div in divList)
{   
    HtmlDiv findDiv = div.Find.ByContent<HtmlDiv>("Verzekeruzelf.nl");
    if(findDiv != null)
    {
        HtmlDiv linkDiv = divList[i+1];
        HtmlAnchor link = linkDiv.Find.AllByTagName<HtmlAnchor>("a").FirstOrDefault();
        if(link != null)
        {
             
            link.ScrollToVisible(ScrollToVisibleType.ElementTopAtWindowTop);
            link.MouseClick();
            break;
        }
    }
    i++;
}

You can remove the Scroll to Visible part from the code, I have put this in order to make sure I am clicking on the correct link.


Here is a short video on how this is working on my end.

Hope you'll find it useful.

Regards,
Ivaylo
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
ServiceDesk
Top achievements
Rank 2
answered on 05 Sep 2014, 08:14 AM
Sorry for me delayed answer.  I changed this because it's a zero based index I suppose.

HtmlDiv linkDiv = divList[i];

I does not work because I looks like the search does not only look within the selected div but also outside it. Because under the <div class="product-items" id="product-items"> there is a <select> box with also the name "Verzekeruzelf.nl" in it and I get 3 times this message in the log:

-------------- VERZEKERUZELF FOUND GO TO NEXT ELEMENT --------------


IList<HtmlDiv> divList = ActiveBrowser.Find.AllByAttributes<HtmlDiv>("class=product-item is-full");
int i = 0;
foreach(HtmlDiv div in divList) {  
    HtmlDiv findDiv = div.Find.ByContent<HtmlDiv>("Verzekeruzelf.nl");
    if(findDiv != null) {       
        HtmlDiv linkDiv = divList[i];
        HtmlAnchor link = linkDiv.Find.AllByTagName<HtmlAnchor>("a").FirstOrDefault();
        if(link != null) {           
            link.ScrollToVisible(ScrollToVisibleType.ElementTopAtWindowTop);
            link.Click();
            break;
        } else {
            Log.WriteLine("-------------- NO LINK FOUND WHY?? --------------");
        }       
    } else {
        Log.WriteLine("-------------- VERZEKERUZELF FOUND GO TO NEXT ELEMENT --------------");
    }
    i++;
}


0
Ivaylo
Telerik team
answered on 10 Sep 2014, 07:16 AM
Hello Roel,

Please note that the coded sample is provided for the html provided in your initial post and it is working just fine if you refer to the video provided.

If you need the right coded solution we will need direct access to the application. Otherwise please use the coded sample as guidance and implement the solution on your own.

Regards,
Ivaylo
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
ServiceDesk
Top achievements
Rank 2
Answers by
Miguel
Top achievements
Rank 1
ServiceDesk
Top achievements
Rank 2
Ivaylo
Telerik team
Share this question
or