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

[Solved] Attach javascript file via DOM

3 Answers 41 Views
Telerik Testing Framework
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jason Bye
Top achievements
Rank 1
Jason Bye asked on 25 Jan 2010, 06:55 AM
I'm trying to find a way to attach a javascript file to a loaded webpage using code in my test project. Essentially I have some javascript helper methods that I need to be present on the webpage so I can run certain tests. Ideally I do not want to have to adjust my web application to include a reference to the helper .js file on the page, instead I would rather my test attach it when it needs it. Is this conceivably possible?

Using Actions.InvokeScript() it is possible to send a string of javascript to the browser. As such I tried the following

            string js = @"{ var fileref = document.createElement('script'); 
                            fileref.setAttribute('type', 'text/javascript'); 
                            fileref.setAttribute('src','file:///C:/Temp/helper.js'); 
                            document.getElementsByTagName('head')[0].appendChild(fileref); 
                           }"; 
 
            ActiveBrowser.Actions.InvokeScript(js); 

The InvokeScript() method reports an error when executing the 4th command i.e. the appendChild() line. If you run the javascript directly in the browser (e.g. from behind a button), it works fine, and you immediately have access to all the methods exposed by helper.js, so the problem is not the javascript.

Does anyone know a way of injecting javascript into a page via the webaii framework?

3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 25 Jan 2010, 04:12 PM
Hello Jason Bye,

There is a way of injecting JavaScript (or any HTML). It requires using the HTTPProxy feature as documented here:

http://www.artoftest.com/support/webaii/topicsindex.aspx?topic=httpproxy

The idea is to use the HTTP proxy to intercept the page coming in from the web server, but before it is fed to the browser. Then modify the HTML content to add the JavaScript you want, and forward the modified resulting HTML to the browser. That should do it for you.

Sincerely yours,
Cody
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jason Bye
Top achievements
Rank 1
answered on 26 Jan 2010, 02:36 AM
Hi Cody,

Thanks for your reply. I had a play with the HttpProxy. Its looks promising, but I have one fundamental problem I can't get past. Having captured a response, I can't find a means by which to convert the Response.Content property (which is a byte[]) into something useful i.e. a string. Even when I know the response is definitely of type text/html and UTF-8 encoded, I can't seem to cast it to a string.

Here is my test code. Its a pretty basic VsUnit test. I'm just trying to capture the html from google.com and stick it in a string

[TestMethod] 
        public void TestHttpProxy() 
        { 
            Manager.LaunchNewBrowser(BrowserType.InternetExplorer); 
            ResponseListenerInfo li = new ResponseListenerInfo(ResponseHandler); 
            Manager.Http.AddBeforeResponseListener(li); 
            ActiveBrowser.NavigateTo("http://www.google.com/"); 
            Manager.Http.RemoveBeforeResponseListener(li); 
        } 
 
        private void ResponseHandler(object sender, HttpResponseEventArgs e) 
        { 
             
            // is it the page, ignore images or css etc 
            if (e.Response.Headers["Content-Type"].Contains("html")) 
            {
                // try various decoding methods --- none of them seem to return anything useful
                string s1 = Encoding.Default.GetString(e.Response.Content); 
                string s2 = Encoding.ASCII.GetString(e.Response.Content); 
                string s3 = Encoding.UTF8.GetString(e.Response.Content); 
            } 
 
        } 

None of the strings s1,s2,s3 ever contain any useful content. They always contain something unreadable.

I know for sure that the html from www.google.com is UTF-8 encoded, because the headers tell me so. Why then doesn't Encoding.UTF8.GetString(e.Response.Content); return me a readable string? I can only assume that the byte[] returned by Response.Content is in a different format. How can i decode this format?

I also tried Response.ToString(), but that doesn't return the content, it only tells you how long the content is.

Hope you can help

Jason

0
Jason Bye
Top achievements
Rank 1
answered on 26 Jan 2010, 03:34 AM
Ignore my last reply. It turns out the google homepage was gzip compressed, and therefore needed to be decompressed before it was converted to a string.
Tags
Telerik Testing Framework
Asked by
Jason Bye
Top achievements
Rank 1
Answers by
Cody
Telerik team
Jason Bye
Top achievements
Rank 1
Share this question
or