简体   繁体   中英

Web browser Control in Stress Test in C#

I am a tester and i have created a simple application to load a web page using Web Browser control in C#(.Net Version = 3.5). Here is my code

public class WebBrowserClass
{
  private  System.Windows.Forms.WebBrowser myBrowser = new System.Windows.Forms.WebBrowser();
  public static int counter = 0;
    public WebBrowserClass(int w, int h)
    {
        myBrowser.ScriptErrorsSuppressed = false;
        myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentCompleted);
        myBrowser.Width = w;
        myBrowser.Height = h;
    }
    public void navigate(string myURL)
    {
        myBrowser.Navigate(myURL);
    }


    private void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        using (Bitmap bitmap = new Bitmap(myBrowser.Width, myBrowser.Height))
        {

            System.Threading.Thread.Sleep(1000);


            myBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, myBrowser.Width, myBrowser.Height));
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = stream.ToArray();

                if (!Directory.Exists("c:\\AnonymousStressTest"))
                    Directory.CreateDirectory("c:\\AnonymousStressTest");
                bitmap.Save("c:\\AnonymousStressTest\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + " " + DateTime.Now.ToLongTimeString().Replace(":", " ") + ".png");
                counter++;
            }
        }
    }


  }

WebBrowserClass works fine when i create its 10-20 object but when i heavily use the class up to 500 objects problem starts appear. At start stress test works fine but in the middle or near to end this message starts appearing "Navigation to the webpage was cancelled".

When url of a webpage is given it get load and re-directed to another webpage.

Please note that the firewall is off on server as well as on client machine. This Stress Test application was ran on client machine.

How can i avoid "Navigation to the webpage was cancelled". ?

Unless you really need a real browser control (which also loads all images, css files and runs the JavaScript), you should really use HttpClient rather than a Web browser control, as its more scalable and you can run it in console application or as a website or in the cloud and uses way less memory and resources.

Otherwise you can try to increase the max number of connections of Internet explorer my editing the following registry key(s):

HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MAXCONNECTIONSPER1_0SERVER 
HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MAXCONNECTIONSPER1_0SERVER

like in the screenshots below:

在此输入图像描述在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM