コード例 #1
0
        internal List <string> SelectWhichPageToTest(List <string> links)
        {
            List <string> AlllinksOnSiteMap = new List <string>();

            foreach (var sitemapfromlinks in links)
            {
                var actualSiteMap = sitemapfromlinks;

                try
                {
                    XElement sitemap = XElement.Load(sitemapfromlinks);
                    // ... XNames.
                    XName url = XName.Get("url", "http://www.sitemaps.org/schemas/sitemap/0.9");
                    XName loc = XName.Get("loc", "http://www.sitemaps.org/schemas/sitemap/0.9");
                    // ... Loop over url elements.
                    // ... Then access each loc element.
                    foreach (var urlElement in sitemap.Elements(url))
                    {
                        var locElement = urlElement.Element(loc);
                        AlllinksOnSiteMap.Add(locElement.Value);
                    }
                }
                catch (Exception exceptonFromLoadingXml)
                {
                    Reporter.LogTestStepForBugLogger(Status.Error, $"This sitemap: {actualSiteMap} is invalid! Error: {exceptonFromLoadingXml} ");
                }
            }
            return(AlllinksOnSiteMap);
        }
コード例 #2
0
        internal List <string> GetTheSiteFromParameter(string SitePropFromConsole)
        {
            List <string> linksOnSiteMap = new List <string>();

            string workingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.FullName, @"Sites\SiteMaps.xml");

            try
            {
                XElement sitemapsXml = XElement.Load(path);

                XName url = XName.Get("URL");
                if (SitePropFromConsole == "All")
                {
                    foreach (var urlElement in sitemapsXml.Elements())
                    {
                        linksOnSiteMap.Add(urlElement.Element(url).Value);
                    }
                    return(linksOnSiteMap);
                }
                else
                {
                    XName projectName = XName.Get(SitePropFromConsole);

                    foreach (var urlElement in sitemapsXml.Elements(projectName))
                    {
                        linksOnSiteMap.Add(urlElement.Element(url).Value);
                    }
                    return(linksOnSiteMap);
                }
            }
            catch (Exception exceptionFromLoadingXMLfile)
            {
                Reporter.LogTestStepForBugLogger(Status.Error, $"The Sitemaps.xml file is not valid! {exceptionFromLoadingXMLfile}");
                return(null);
            }
        }
コード例 #3
0
        public void RunTheTest(List <string> links)
        {
            List <string> linksOnSiteMap = SelectWhichPageToTest(links);

            foreach (var url in linksOnSiteMap)
            {
                try
                {
                    TryToOpenTheUrl(url);

                    HttpWebRequest httpReq2 = (HttpWebRequest)WebRequest.Create(url);
                    httpReq2.AllowAutoRedirect = false;

                    HttpWebResponse httpRes = (HttpWebResponse)httpReq2.GetResponse();

                    if (httpRes.StatusCode == HttpStatusCode.OK)
                    {
                        CountOpenedSites("SiteIsOk");
                    }
                    else
                    {
                        Reporter.LogTestStepForBugLogger(Status.Warning, $"This url have been opened: {url}  ,status code: {(int)httpRes.StatusCode}-{httpRes.StatusCode}");
                        CountOpenedSites("siteContainsWarning");
                    }
                    httpRes.Close();
                }
                catch (WebException ex)
                {
                    Reporter.LogTestStepForBugLogger(Status.Error, $"This url have been opened: {url}  ,status: {ex.Message}");
                    CountOpenedSites("siteContainsError");
                }
                finally
                {
                    CountOpenedSites("allSiteCase");
                }
            }
        }