Processing XML data for checking the Sitemap

Parent Previous Next

Processing XML data for checking the Sitemap


The following libraries are used to work with XML


using System.Collections;

using System.Xml;


Let's describe the ReadSitemapXML auxiliary method for reading data from the site map sitemap.xml

This method can be described in the Steps class, but in order for it to be available in any class, it is better to describe it in the Helper.

The blank of the ReadSitemapXML method is closed when reading a link from a file sitemap.xml and as a result, return the list of read links.


File: Helper.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Windows.Forms;

using System.Threading;

using System.Threading.Tasks;

using System.IO;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Reflection;

using Newtonsoft.Json;

using HatFramework;

using System.Collections;

using System.Xml; 


namespace Hat

{

    public static class Helper

    {

        /* Method: reading the XML file of the site map */

        public static async Task<List<string>> ReadSitemapXML(Tester tester, string filename, bool useragent)

        {

            // Declaring a variable whose type is a list (a list for storing links)

            List<string> list = new List<string>();


            // Declaring a variable that is an error flag (the flag is disabled by default)

            bool errors = false;


            try

            {

                // Setting up the system connection parameters                    ServicePointManager.Expect100Continue = true;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


                // We are reading the xml file

                XmlDocument xDoc;

                if (useragent == false)

                {

                    xDoc = new XmlDocument();

                    xDoc.Load(filename);

                }

                else

                {

                    WebClient client = new WebClient();

                    client.Headers["User-Agent"] = await tester.BrowserGetUserAgentAsync();

                    client.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

                    string data = client.DownloadString(filename);


                    xDoc = new XmlDocument();

                    xDoc.LoadXml(data);

                }


                // Reading the elements from the resulting xml file

                XmlElement xRoot = xDoc.DocumentElement;

                foreach (XmlNode xnode in xRoot)

                {

                    for (int j = 0; j <= xnode.ChildNodes.Count; j++)

                    {

                        if (xnode.ChildNodes[j].Name == "loc")

                        {

                            // We save the received link to the list

                            list.Add(xnode.ChildNodes[j].InnerText);

                            break;

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                errors = true; // enabling the error flag

            }


            // Checking the error flag (if the flag is set to true, then a failure has occurred and the test will be aborted

            await tester.AssertFalseAsync(errors);


            return list; // returning the list of links

        }

    }

}


Now let's describe an autotest in which we will check the site map


File: Sitemap_Test.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Windows.Forms;

using System.Threading;

using System.Threading.Tasks;

using System.IO;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Reflection;

using Newtonsoft.Json;

using HatFramework;


namespace Hat

{

    public class Sitemap_Test

    {

        // The main variable for the autotest operation

        Tester tester;

        

        // The main input function (the autotest starts with this function)

        public async void Main(Form browserWindow)

        {

            // Initialization of the main variable and additional functions

            tester = new Tester(browserWindow);

            await setUp();

            await test();

            await tearDown();

        }


        // The function of getting started with the autotest

        public async Task setUp()

        {

            await tester.BrowserFullScreenAsync(); // Sets the browser resolution to full screen

        }


        // The function of performing autotest actions

        public async Task test()

        {

            await tester.TestBeginAsync(); // The beginning of the execution of actions


            // Loading the page at the specified address

            await tester.GoToUrlAsync("https://somovstudio.github.io/sitemap.xml", 25);


            // Reading links from a file sitemap.xml

            List<string> list = await Helper.ReadSitemapXML(tester, "https://somovstudio.github.io/sitemap.xml", false);


            // Processing the list of links

            int status = 0;

            foreach (string url in list)

            {

                // Getting the page status (200 - correct, 404 - no page, 502 - the server is not responding, etc.)

                status = await tester.RestGetStatusCodeAsync(url);

                // Output the result to the console

                tester.ConsoleMsg("Status: " + status.ToString() + " | Page: " + url);

                // Checking the correctness of the result

                await tester.AssertEqualsAsync(200, status);

            }


            await tester.TestEndAsync(); // Completion of actions

        }


        // Функция завершения работы с автотестом

        public async Task tearDown()

        {

            await tester.BrowserCloseAsync(); // Closes the browser

        }

    }

}








Created with the Personal Edition of HelpNDoc: Benefits of a Help Authoring Tool