It is difficult to check and test pages of website manually one by one if pages count is huge. There are some limitations with automation testing of websites. We cannot check whether UI is meet as per expectation or is there any content issue with website with automation testing. To solve this problem we came up with hybrid solution - Website Pages Checker. In this application we need to specify the websites URL's that we require to check and then run the application. The application will visit each page specified in the list, automatically scroll till end of the page and it will ask whether page is present as per requirement. This application saved our huge amount of time avoiding manually entering URL's in browser and specifying whether they are working or not in excel sheet. Please refer below steps to create this console application.
Step 1: Create Console Application in Visual Studio
Step 2: Install below Nugget Packages for the project.
- log4net
- Newtonsoft.Json
- Selenium.WebDriver
Step 3: Check chrome browser version and download chrome driver from below location
Step 5: Add changes in AssemblyInfo.cs file
Code
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Step 6: Add below change in App.config file
Code
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- Log4net Logging Setup Start--> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections> <log4net> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="file1" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs\log_%date{yyyy.MM.dd_HH.mm.ss}.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <appendToFile value="true" /> <rollingStyle value="Once" /> <maxSizeRollBackups value="5" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> </appender> <appender name="file2" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs\PagesDetails_%date{yyyy.MM.dd_HH.mm.ss}.log" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <appendToFile value="true" /> <rollingStyle value="Once" /> <maxSizeRollBackups value="5" /> <staticLogFileName value="false" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="console" /> </root> <logger name="File1"> <level value="All"/> <appender-ref ref="file1"/> </logger> <logger name="File2"> <level value="All"/> <appender-ref ref="file2"/> </logger> </log4net> <!-- Log4net Logging Setup End--> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <appSettings> <add key="UrlsFilePath" value="Urls.json"/> </appSettings> </configuration>
Step 7: Create Log Folder in application and set build action as shown below.
namespace WebsitePagesChecker { public static class Logger { public static readonly log4net.ILog Log = log4net.LogManager.GetLogger("File1"); public static readonly log4net.ILog SiteDetailsFile = log4net.LogManager.GetLogger("File2"); } }
Step 9: Create JsonUrls.cs class file and add below code
Code
using System; using System.Collections.Generic; namespace WebsitePagesChecker { internal class JsonUrls { public string url { get; set; } public static List<JsonUrls> GetSiteUrls() { List<JsonUrls> URLS = new List<JsonUrls>(); try { string UrlsFilePath = System.Configuration.ConfigurationManager.AppSettings["UrlsFilePath"].ToString(); //Getting URLS from Json File string json = System.IO.File.ReadAllText(UrlsFilePath); URLS = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonUrls>>(json); } catch (Exception ex) { Logger.Log.Error(String.Format("{0} Exception {1}", DateTime.Now, ex)); } return URLS; } } }
Step 10: Create Urls.json and add below code
Code
[ { "url": "https://www.google.com" }, { "url": "https://www.amazon.com" }, { "url": "https://www.flipkart.com" }, { "url": "https://www.youtube.com" }, { "url": "https://bookmyshow.com" } ]
Step 11: Change build action of Urls.json as shown below
Step 12: Create Drivers.cs Class file and add below code
Code
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace WebsitePagesChecker { internal class Drivers { protected static IWebDriver SetupChromeDriver() { ChromeOptions options = new ChromeOptions(); options.AddArgument("--log-level=3");// Hides log errors/warnings IWebDriver driver = new ChromeDriver(); driver.Manage().Window.Maximize(); return driver; } protected static void CloseExitDrivers(IWebDriver driver) { driver.Close(); driver.Quit(); } } }
Step 13: Create TestCases.cs class file and add below code
Code
using OpenQA.Selenium; using System; using System.Collections.Generic; namespace WebsitePagesChecker { internal class TestCases:Drivers { public static void CheckWebsitePages() { try { IWebDriver driver = SetupChromeDriver(); List<JsonUrls> URLS = JsonUrls.GetSiteUrls(); Logger.SiteDetailsFile.Info(String.Format("Total URLS:{0}", URLS.Count)); foreach (var URL in URLS) { string script = "var i = 10; var int = setInterval(function() { window.scrollTo(0, i); i += 10; if (i >= 3000) clearInterval(int);}, 20);";//to increase speed increase values of i driver.Navigate().GoToUrl(URL.url); IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript(script); Console.Write("\nPage is Ok? Yes (y) or No (n):"); char userChoice = Console.ReadKey().KeyChar; Logger.SiteDetailsFile.Info(String.Format("{0} Page:{1} Result:{2}", DateTime.Now, URL.url, userChoice.ToString().ToUpper())); } CloseExitDrivers(driver); } catch (Exception ex) { Logger.Log.Error(String.Format("Exception: {0}",ex.ToString())); } } } }
Step 14: Add below code in Program.cs file
Code
using System; namespace WebsitePagesChecker { internal class Program { static void Main(string[] args) { StartApplication(); } private static void StartApplication() { Logger.Log.Info(String.Format("Application Start {0}", DateTime.Now)); TestCases.CheckWebsitePages(); Logger.Log.Info(String.Format("Application End {0}", DateTime.Now)); Console.ReadKey(); } } }
Step 15: Your applications is ready you can run the applications and test the Urls.
Finally output of the file will be something like this
Thank you for reading the post 😀
No comments:
Post a Comment