Tuesday, November 16, 2021

Find Broken Images in UI (Selenium & Rest Sharp )


Find Broken Images in UI



                




Broken image is a link/image that does not show up as a picture, clicking upon which takes the end-user to a defunct picture. The user encounters a 404 Error when clicked on the broken image
.

The code written to solve the issue using selenium and Rest sharp to interact with the broken links .


Code :

  • The details of the images present on the page are fetched by locating the WebElements with img tag
  • Using img.GetAttribute("src") we are fetching the URI and executing the URI using the Restsharp library to get the statuscode which is success or not found 


using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using OpenQA.Selenium.Remote;
using System.Threading;
using System.Net.Http;
using System.Threading.Tasks;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using RestSharp;

namespace UI.TESTCASES.Helper

{

    public class BrokenImageFinder

    {

        // selenium Webdriver Initialization 

        IWebDriver driver;

        [Test]

        public async Task LT_Broken_Images_Test()

        {

            int broken_images = 0;

            String test_url = "https://the-internet.herokuapp.com/broken_images";

            // WebdriverManager Implementation that will download the respective chrome version  

            new DriverManager().SetUpDriver(new ChromeConfig());

            driver = new ChromeDriver();

            driver.Manage().Window.Maximize();

            driver.Navigate().GoToUrl(test_url);

            var image_list = driver.FindElements(By.TagName("img"));

            /* Loop through all the images */

            foreach (var img in image_list)

            {

                var img1 = img.GetAttribute("src");

                Thread.Sleep(3000);

                try

                {

                    RestClient client = new RestClient(img1);

                    RestRequest request = new RestRequest(Method.GET);

                    var response = client.Execute(request);

                    if (response.StatusCode == HttpStatusCode.OK)

                    {

                        Console.WriteLine("Image at the link " + img.GetAttribute("src") + " is OK, status is "

                                + response.StatusCode);

                    }

                    else

                    {

                        Console.WriteLine("Image at the link " + img.GetAttribute("src") + " is Broken, status is "

                            + response.StatusCode);

                        broken_images++;

                    }

                }

                catch (Exception ex)

                {

                    if ((ex is ArgumentNullException) ||

                       (ex is NotSupportedException))

                    {

                        System.Console.WriteLine("Exception occured\n");

                    }

                }

            }

            /* Perform wait to check the output */

            System.Threading.Thread.Sleep(2000);

            Console.WriteLine("\nThe page " + test_url + " has " + broken_images + " broken images");

        }

    }

}


Output : 





No comments:

Post a Comment