Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Support.UI;
- using System.Net;
- using System.Net.Mail;
- using System.IO;
- namespace SiteLoginTest
- {
- class Program
- {
- static string baseURL = ConfigurationManager.AppSettings["BaseUrl"];
- static bool isSiteLoaded = false;
- static bool isUserLoggedIn = false;
- public static void emailSender(string emailType, ChromeDriver driver)
- {
- string bU = baseURL;
- bool isSuccess = false;
- MailMessage mailMessage = new MailMessage();
- mailMessage.To.Add(ConfigurationManager.AppSettings["RecipientAddress"]);
- mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromAddress"]);
- if (emailType == "siteWasNotLoaded")
- {
- mailMessage.Subject = "Site Automated Tester > Site Failed to Load";
- mailMessage.Body = "<html><body><h1><font color='red'>Site Load Failure Notice</font></h1></br>The Site has failed to load. Failure screen has been included in the email as attachment.<p>Tested URL: " + bU + ".</p></body></html>";
- }
- else if (emailType == "userCouldNotLogin")
- {
- mailMessage.Subject = "Site Automated Tester > User Login Failed";
- mailMessage.Body = "<html><body><h1><font color='red'>Login Failure Notice</font></h1></br>The Site was successfully loaded but a user could not login to the system. Failure screen has been included in the email as attachment.<p>Tested URL: " + bU + ".</p></body></html>";
- }
- else if (emailType == "success")
- {
- mailMessage.Subject = "Site Automated Tester > Successful Site Access Notification";
- mailMessage.Body = "<html><body><h1>Site is up and running.</h1>The site could successfully be accessed and a login was successful with a user.<p>Tested URL: " + bU + ".</p></body></html>";
- isSuccess = true;
- }
- mailMessage.IsBodyHtml = true;
- SmtpClient mailClient = new SmtpClient(ConfigurationManager.AppSettings["SmtpClient"]);
- mailClient.UseDefaultCredentials = false;
- if (!isSuccess)
- {
- //add attachment
- using (MemoryStream ms = new MemoryStream(driver.GetScreenshot().AsByteArray))
- {
- var attachment = new Attachment(ms, "screen-shot.jpg");
- mailMessage.Attachments.Add(attachment);
- mailClient.Send(mailMessage);
- }
- }
- else
- {
- mailClient.Send(mailMessage);
- }
- }
- public static void IsSiteAvailable()
- {
- using (var driver = new ChromeDriver())
- {
- driver.Manage().Window.Maximize();
- string x = string.Empty;
- x = baseURL;
- driver.Navigate().GoToUrl(x);
- try
- {
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"]));
- isSiteLoaded = true;
- }
- catch (NoSuchElementException nse)
- {
- isSiteLoaded = false;
- }
- catch (Exception e)
- {
- throw e;
- }
- if (!isSiteLoaded)
- {
- emailSender("siteWasNotLoaded", driver);
- return;
- }
- bool isLoginSuccessful = LoginToTheSite(driver);
- if (isLoginSuccessful == true)
- {
- emailSender("success", driver);
- }
- }
- }
- public static bool LoginToTheSite(ChromeDriver driver)
- {
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"])).Clear();
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"])).SendKeys(ConfigurationManager.AppSettings["UserId"]);
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserPasswordFieldId"])).Clear();
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserPasswordFieldId"])).SendKeys(ConfigurationManager.AppSettings["UserPass"]);
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["LoginButtonId"])).Click();
- try
- {
- driver.FindElement(By.Id(ConfigurationManager.AppSettings["ElementIdToIdentifySuccessfulLogin"]));
- isUserLoggedIn = true;
- }
- catch (NoSuchElementException nse)
- {
- isUserLoggedIn = false;
- }
- catch (Exception e)
- {
- throw e;
- }
- if (!isUserLoggedIn)
- {
- emailSender("userCouldNotLogin", driver);
- return false;
- }
- else
- {
- return true;
- }
- }
- static void Main(string[] args)
- {
- IsSiteAvailable();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement