Advertisement
Guest User

Program.cs

a guest
May 16th, 2014
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.31 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using OpenQA.Selenium;
  4. using OpenQA.Selenium.Chrome;
  5. using OpenQA.Selenium.Support.UI;
  6. using System.Net;
  7. using System.Net.Mail;
  8. using System.IO;
  9.  
  10. namespace SiteLoginTest
  11. {
  12.     class Program
  13.     {
  14.         static string baseURL = ConfigurationManager.AppSettings["BaseUrl"];
  15.  
  16.         static bool isSiteLoaded = false;
  17.         static bool isUserLoggedIn = false;
  18.  
  19.         public static void emailSender(string emailType, ChromeDriver driver)
  20.         {
  21.             string bU = baseURL;
  22.             bool isSuccess = false;
  23.  
  24.             MailMessage mailMessage = new MailMessage();
  25.             mailMessage.To.Add(ConfigurationManager.AppSettings["RecipientAddress"]);
  26.             mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromAddress"]);
  27.  
  28.             if (emailType == "siteWasNotLoaded")
  29.             {
  30.                 mailMessage.Subject = "Site Automated Tester > Site Failed to Load";
  31.                 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>";
  32.             }
  33.  
  34.             else if (emailType == "userCouldNotLogin")
  35.             {
  36.                 mailMessage.Subject = "Site Automated Tester > User Login Failed";
  37.                 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>";
  38.             }
  39.  
  40.             else if (emailType == "success")
  41.             {
  42.                 mailMessage.Subject = "Site Automated Tester > Successful Site Access Notification";
  43.                 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>";
  44.                 isSuccess = true;
  45.             }
  46.  
  47.             mailMessage.IsBodyHtml = true;
  48.             SmtpClient mailClient = new SmtpClient(ConfigurationManager.AppSettings["SmtpClient"]);
  49.             mailClient.UseDefaultCredentials = false;
  50.  
  51.             if (!isSuccess)
  52.             {
  53.                 //add attachment
  54.                 using (MemoryStream ms = new MemoryStream(driver.GetScreenshot().AsByteArray))
  55.                 {
  56.                     var attachment = new Attachment(ms, "screen-shot.jpg");
  57.                     mailMessage.Attachments.Add(attachment);
  58.                     mailClient.Send(mailMessage);
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 mailClient.Send(mailMessage);
  64.             }
  65.         }
  66.  
  67.         public static void IsSiteAvailable()
  68.         {
  69.             using (var driver = new ChromeDriver())
  70.             {
  71.                 driver.Manage().Window.Maximize();
  72.                 string x = string.Empty;
  73.                 x = baseURL;
  74.  
  75.                 driver.Navigate().GoToUrl(x);
  76.  
  77.                 try
  78.                 {
  79.                     driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"]));
  80.                     isSiteLoaded = true;
  81.                 }
  82.                 catch (NoSuchElementException nse)
  83.                 {
  84.                     isSiteLoaded = false;
  85.                 }
  86.                 catch (Exception e)
  87.                 {
  88.                     throw e;
  89.                 }
  90.  
  91.                 if (!isSiteLoaded)
  92.                 {
  93.                     emailSender("siteWasNotLoaded", driver);
  94.                     return;
  95.                 }
  96.  
  97.                 bool isLoginSuccessful = LoginToTheSite(driver);
  98.  
  99.                 if (isLoginSuccessful == true)
  100.                 {
  101.                     emailSender("success", driver);
  102.                 }
  103.             }
  104.         }
  105.  
  106.         public static bool LoginToTheSite(ChromeDriver driver)
  107.         {
  108.             driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"])).Clear();
  109.             driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserNameFieldId"])).SendKeys(ConfigurationManager.AppSettings["UserId"]);
  110.             driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserPasswordFieldId"])).Clear();
  111.             driver.FindElement(By.Id(ConfigurationManager.AppSettings["UserPasswordFieldId"])).SendKeys(ConfigurationManager.AppSettings["UserPass"]);
  112.             driver.FindElement(By.Id(ConfigurationManager.AppSettings["LoginButtonId"])).Click();
  113.  
  114.             try
  115.             {
  116.                 driver.FindElement(By.Id(ConfigurationManager.AppSettings["ElementIdToIdentifySuccessfulLogin"]));
  117.                 isUserLoggedIn = true;
  118.             }
  119.             catch (NoSuchElementException nse)
  120.             {
  121.                 isUserLoggedIn = false;
  122.             }
  123.             catch (Exception e)
  124.             {
  125.                 throw e;
  126.             }
  127.  
  128.             if (!isUserLoggedIn)
  129.             {
  130.                 emailSender("userCouldNotLogin", driver);
  131.                 return false;
  132.             }
  133.             else
  134.             {
  135.                 return true;
  136.             }
  137.         }
  138.         static void Main(string[] args)
  139.         {
  140.             IsSiteAvailable();
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement