-1

We have Visual Studio Team Services (VSTS) site. We need to take the screenshots of the specific regions on the page using c# code. Tried out to fetch the html content of the page. However unable to do so even though we are giving proper credentials.

Require help to take screenshot of the VSTS site using c# code (of specific region)

I used below code to try to fetch the html content of the page. (So that once I get the html content I can convert it to image).

WebRequest request = WebRequest.Create("google.com"); WebResponse response = request.GetResponse(); 
Stream data = response.GetResponseStream();
string html = String.Empty; 

using (StreamReader sr = new StreamReader(data)) 
{ html = sr.ReadToEnd(); }

If I give the URL as Google or any other URL then it working fine however if I give the VSTS site URL even with credentials it is not working

nina
  • 95
  • 3
  • 12

1 Answers1

0

Not sure why you need to capture the screenshot for VSTS from C# code. But the issue you meet could be caused by authentication in your code. When you authenticate to VSTS from 3rd party tools or code, you need to enable alternate credential from "VSTS Web Portal/Security/Alternate authentication credentials" and use the alternate credential for the authentication. So update your code to below and then try again:

            string altusername = "xxx";
            string altpassword = "xxx";
            WebRequest request = WebRequest.Create("https://xxx.visualstudio.com/xxx/xxx");
            string auth = altusername + ":" + altpassword;
            auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
            request.Headers["Authorization"] = "Basic" + auth;

            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            string html = String.Empty;

            using (StreamReader sr = new StreamReader(data))
            { html = sr.ReadToEnd(); }

Another thing is that you may still meet some other issue even you can authenticate to VSTS successfully since the response from VSTS is more complex than Google. I would recommend you to use some web automation testing tools like Selenium to achieve the feature you want.

Following code also works at my side:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        string altusername = "xxx";
        string altpassword = "xxx";
        string url = "https://xxx.visualstudio.com/_git/Python";
        string auth = altusername + ":" + altpassword;
        auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
        string header = "Authorization: Basic " + auth;
        webBrowser1.Navigate(url, null,null,header);
        Timer tim = new Timer();
        tim.Tick += new EventHandler(timer_Tick); 
        tim.Interval = (1000) * (30);             // Adjust the time base on the time you need to load the webpage
        tim.Enabled = true;                       
        tim.Start();                              
    }
    void timer_Tick(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(webBrowser1.Width, webBrowser1.Height);
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        webBrowser1.DrawToBitmap(bmp, rect);
        bmp.Save("D:\\Code\\0a.bmp");
    }
}
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • Thank you for your help. Tried the above code, however getting below error : "Microsoft Internet Explorer's Enhanced Security Configuration is currently enabled on your environment. This enhanced level of security prevents our web integration experiences from displaying or performing correctly. To continue with your operation please disable this configuration or contact your administrator.". Tried to follow the steps on "https://4sysops.com/archives/how-to-disable-internet-explorer-enhanced-security-configuration-ie-esc-in-windows-server-2012/". However not able to achieve it – nina Dec 12 '16 at 09:45
  • @nina Cannot open the link you mentioned. Can you try to disable IE Enhanced Security Configuration by following this link:https://www.limestonenetworks.com/support/knowledge-center/17/70/how_do_i_disable_internet_explorer_enhanced_security.html? – Eddie Chen - MSFT Dec 13 '16 at 00:40
  • Disabled the enhanced security configuration using the above mentioned url. However still getting the same error. I copied the html content fetched from the code and paste it in a html file. And then opened the html file. So getting the error "Internet Explorer restricted this webpage from running scripts or ActiveX controls". Followed the url "http://stackoverflow.com/questions/7038724/how-to-automaticaly-allow-blocked-content-in-ie". However still same issue persists – nina Dec 13 '16 at 07:16
  • @nina Can you open VSTS web site from the browser directly? And can you open it from other browsers like Chrome? – Eddie Chen - MSFT Dec 13 '16 at 07:52
  • Yes am able to open it from browser – nina Dec 13 '16 at 11:57
  • Any help on this ? – nina Dec 14 '16 at 16:35
  • @nina Then can you open the saved html file from other browser like chrome? – Eddie Chen - MSFT Dec 15 '16 at 07:39
  • When I open the saved html file then I get the message "Microsoft Internet Explorer's Enhanced Security Configuration is currently enabled on your environment. This enhanced level of security prevents our web integration experiences from displaying or performing correctly. To continue with your operation please disable this configuration or contact your administrator." – nina Dec 15 '16 at 15:05
  • Do we have any third party DLLs that can help out – nina Dec 17 '16 at 02:06
  • Can someone pls help out with this – nina Dec 20 '16 at 14:01
  • @nina Sorry for the late response. There isn't any third party DLLs to achieve the feature you want. Did you tried the method with Selenium test? – Eddie Chen - MSFT Dec 21 '16 at 00:33
  • Thanks for the response Eddie.... Yea the last option is Selenium... Before moving to that want to try some other approach – nina Dec 21 '16 at 18:39
  • @nina I added another solution which works at my side. You can check if it can works for you. – Eddie Chen - MSFT Dec 22 '16 at 10:06
  • Thanks Eddie. But getting blank image in my case – nina Dec 24 '16 at 15:35
  • @nina Does the VSTS page display in webbrowser? – Eddie Chen - MSFT Dec 26 '16 at 00:59
  • Yes the page is displayed in web browser – nina Dec 26 '16 at 06:38
  • @nina It seems that it took the screenshot before the page fully loaded. – Eddie Chen - MSFT Dec 26 '16 at 07:29
  • :I verified once again and I don't completely get a blank image. Get image for some of the content and some of the image as blank. Yes you are right the issue is it page loading. Because when I run the application, first top content is loaded(and in image get this content only) and then other content is loaded. Tried using three.sleep also but no help. Can you please help out with same – nina Dec 27 '16 at 14:01
  • Thanks for the help. It worked out. However stuck in another issue with it. I have a console application where i am doing some processing and hen need the screen shot. So creating the form object explicitly and calling the buttonclick method. However the timer_Tick method is not called – nina Jan 17 '17 at 14:07
  • The screen shot was getting captured successful. However now suddenly after 7 months ran into an issue. When trying to Capture screenshot it is showing pop up to enter credentials. The pop up was not shown earlier. Any help – nina Aug 10 '17 at 17:26