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

Extacting an HTML Element, Setting it as a Variable and Check it Against a Database

6 Answers 117 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dan
Top achievements
Rank 1
Dan asked on 07 Aug 2012, 08:57 PM
Hi,

I am testing a web application and I need a step to extract imagehashes for normal/animated/flash/popover images and set the hash as a variable.  I will also need an additional step to validate that hash variable and check it against a database for its properties and parameters(such as image attributes).  I have checked the forum/documentation but have not found anything for extracting html elements.  Can you please guide me and point me to the right direction and explain how this can be done via the UI or in a coded step in C#?

sample html where I need to extract the hash from:

<img class="new-image-content" src="<A href="http://......adImageHash=9Zb+DUgHaeJ*W2PTRUbj7aC4SfE">http://......adImageHash=9Zb+DUgHaeJ*W2PTRUbj7aC4SfE</A>=" style="cursor: pointer; width: 100%;">

Thanks,
Daniel

6 Answers, 1 is accepted

Sort by
0
Dan
Top achievements
Rank 1
answered on 07 Aug 2012, 09:00 PM
I'm sorry, can you please move this to the Test Studio, General Discussion.  This question is not for Test Studio Express.
0
Stoich
Telerik team
answered on 08 Aug 2012, 08:51 PM
Hi Dan,
the html code you've provided doesn't seem entirely correct to me.

Nevertheless the image hash is the part of the element definition that's bold and italic:

<img class="new-image-content" src="<A href="http://......adImageHash=9Zb+DUgHaeJ*W2PTRUbj7aC4SfE">http://......adImageHash=9Zb+DUgHaeJ*W2PTRUbj7aC4SfE</A>=" style="cursor: pointer; width: 100%;">

Is this correct?

First off you will need to get the element. Once you find it - you need to get the image's source attribute. Then you need to take out only the image hash. Then you need to set it as an extracted variable and you're ready to access it. The code for this will look something like this:
ActiveBrowser.NavigateTo("https://www.google.bg/imghp?hl=bg&tab=wi");
 
HtmlImage img = Find.ByExpression<HtmlImage>("id=hplogo");
 
String hash = img.Src.Substring(img.Src.IndexOf("logos/")+6);
 
Log.WriteLine("hash:"+hash);
this.SetExtractedValue("ImageHash", hash); //Sets the extracted variable. Now you can access it in code or through the standard databinding

This code sample works against the Google logo image. But you would employ a similar approach. You will need to edit the code a bit. It will look something like this:
String hash = img.Src.Substring(img.Src.IndexOf("ImageHash")+9);

This will only get the imageHash value out of the src attribute. Also check out the following article:
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/extracted-variables-in-code.aspx

I hope you find this information useful.

Kind regards,
Stoich
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Dan
Top achievements
Rank 1
answered on 21 Sep 2012, 08:22 PM
Hi Stoich,

I'm running into this issue again,

I am using an even simpler substring function to get my hash, this code is as follows:

ActiveBrowser.NavigateTo("some url");
            
            HtmlImage img = Find.ByExpression<HtmlImage>("class=new-image-content");
            string imagehash2 = img.Src.Substring(79);
            Sponsored_imagehash_global = imagehash2;
            
            NativeWindow window = new NativeWindow();
            window.AssignHandle(ActiveBrowser.Window.Handle);
            Log.WriteLine("Current Imagehash: " + imagehash2);

However, this fails more than 50% of the times, If I run just this coded step by itself, I get the correct hash.  How do I make this work every single time?

Also, my substring can be a variable length, so anything in the form of....img.Src.Substring(img.Src.IndexOf("=")+int) where int is a number will not work.
0
Stoich
Telerik team
answered on 26 Sep 2012, 02:24 PM
Hi Dan,
how does this code fail? Probably on this line:
string imagehash2 = img.Src.Substring(79);
?

You have to workaround the fact that the hash is of variable length. You will need to determine a "stopping" mark for the hash. If there's always going to be some specific word after the hash like say: "abc123" you could do this:
int lng = img.Src.IndexOf("abc123") -img.Src.IndexOf("=");
img.Src.Substring(img.Src.IndexOf("=")+lng)

You really have a lot of options - it's all up to you and how you want to solve this problem. I'm sure you'll find a lot of useful info on how to work with String on the Internet.

I hope this helps. If you require additional assistance with this - please give us some additional information so that we can assist efficiently.

Kind regards,
Stoich
the Telerik team
Are you enjoying Test Studio? We’d appreciate your vote in the ATI automation awards.
Vote now
0
Dan
Top achievements
Rank 1
answered on 26 Sep 2012, 09:47 PM
Hi Stoich,

I've narrowed down the problem to this:

The class that Telerik is trying to find is triggered by some javascript event that needs to happen.  During the find expression, the Javascript is not yet invoked so the find expression returns null(I have an if-statement to check for this) and throws the "System.NullReferenceException: Object reference not set to an instance of an object" error in my log. 

Is there a way i can make Telerik to wait on the Javascript via code so it can find the img class?  I can put a delay before the coded step, but can I manually code a delay inside a coded step?  Right now it is going to the find expression before the ActiveBrowser.Navigate to has finished loading all the javascript the page.

Is there something like System.Threading.Thread.Sleep() I can put right after the "ActiveBrowser.NavigateTo..." and the find expression so that Telerik will waits on javascript?
0
Stoich
Telerik team
answered on 02 Oct 2012, 07:41 AM
Hello Dan,
you can just wait for the element to exist in code before trying to implement it. Here's an article that will tell you how to wait for an element in code:
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/wait-for-element-to-exist-in-code.aspx

This approach will make sure the element is in its correct place (and at that point the javascript related to it has probably run its course as far as Test Studio is concerned). And you should be safe to try and access the element.

Let me know how it goes.

Greetings,
Stoich
the Telerik team
Are you enjoying Test Studio? We’d appreciate your vote in the ATI automation awards.
Vote now
Tags
General Discussions
Asked by
Dan
Top achievements
Rank 1
Answers by
Dan
Top achievements
Rank 1
Stoich
Telerik team
Dan
Top achievements
Rank 1
Share this question
or