Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Software Testing Using C#

This document contains code for automating tests of a web application using Selenium and C#. It defines classes for the base test page, a login page object model, and a test execution class. The base class contains common methods like initializing the Chrome driver, taking screenshots, logging actions. The login page class defines page elements and a login method. The test execution class inherits from the base and contains test initialization, cleanup and sample tests using the page object model.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Software Testing Using C#

This document contains code for automating tests of a web application using Selenium and C#. It defines classes for the base test page, a login page object model, and a test execution class. The base class contains common methods like initializing the Chrome driver, taking screenshots, logging actions. The login page class defines page elements and a login method. The test execution class inherits from the base and contains test initialization, cleanup and sample tests using the page object model.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DAy 2 - Lab Assignment

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POM
{
public partial class LoginPage : BasePage
{
#region Properties
public string username { get; set; }
public string password { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string mobileNumber { get; set; }
#endregion

public void Login()


{
if (username != null)
{
EnterText(txtUsername, username);

}
if(password != null)
{
EnterText(txtPassword, password);
}
Click(btnLogin);
}
}
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using MongoDB.Bson.Serialization.Serializers;
using System.Linq.Expressions;

namespace Frame_POMA
{

[TestClass]
public class Execution : BasePage
{
private TestContext instance;

public TestContext TestContext


{
set { instance = value; }
get { return instance; }
}

[AssemblyInitialize]
public static void AssemblyInitialize(TestContext testContext)
{
LogReport("Test Report");
Console.WriteLine("AssemblyInitialize");
}

[AssemblyCleanup]
public static void AssemblyCleanup()
{
ExtentFlush();
Console.WriteLine("AssemblyCleanup");
}

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
Console.WriteLine("ClassInitialize");
}

[ClassCleanup]
public static void ClassCleanup()
{
Console.WriteLine("ClassCleanup");
}

[TestInitialize]
public void TestInitialize()
{
exParentTest = extentReports.CreateTest(TestContext.TestName);
NodeCreation(TestContext.TestName);
InitializeChrome();
OpenUrl("http://adactinhotelapp.com/");
}

[TestCleanup]
public void TestCleanup()
{
QuitChrome();
}
}
}

Base CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports.Reporter.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;

namespace Frame_POMA
{
public class BasePage
{
public static IWebDriver driver;
public static ExtentReports extentReports;
public static ExtentTest exParentTest;
public static ExtentTest exChildTest;
public static string dirpath = "C:\\ExtentReports\\" +
DateTime.Now.ToString("yyyyMMdd_HHmmss") + "\\";

//classlevelLog4net
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public static void LogReport(string testcase)


{
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(dirpath);
log.Info("Application is working ");
#region ABC
htmlReporter.Config.DocumentTitle = "Automation Testing Report";
htmlReporter.Config.Theme = Theme.Standard;

extentReports = new ExtentReports();


extentReports.AttachReporter(htmlReporter);

extentReports.AddSystemInfo("AUT", "Hotel Adactin");


extentReports.AddSystemInfo("Environment", "QA");
extentReports.AddSystemInfo("Machine", Environment.MachineName);
extentReports.AddSystemInfo("OS", Environment.OSVersion.VersionString);
#endregion
}
public void Log(string exp, string a)
{
switch (exp)
{
case "info":
log.Info(a);
break;
case "error":
log.Error(a);
break;
case "debug":
log.Debug(a);
break;
case "warn":
log.Warn(a);
break;
}
}
public void NodeCreation(string methodname)
{
exChildTest = exParentTest.CreateNode(methodname);
}
public static void TakeScreenshot(Status status, string stepDetail)
{
string path = dirpath + "TestExcelLog_" + DateTime.Now.ToString("yyyyMMddHHmmss");
Screenshot image = ((ITakesScreenshot)driver).GetScreenshot();
image.SaveAsFile(path + ".png", ScreenshotImageFormat.Png);
BasePage.exChildTest.Log(status, stepDetail,
MediaEntityBuilder.CreateScreenCaptureFromPath(path + ".png").Build());
log.Info("Application is working ");
}
public static void ExtentFlush()
{
extentReports.Flush();
}
public void InitializeChrome()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
log.Info("Application is working ");
}
public void EnterText(By by, string value)
{
try
{
driver.FindElement(by).SendKeys(value);
TakeScreenshot(Status.Pass, "Text Written on " + by + " this locator and value : " + value);
}
catch (Exception ex)
{
TakeScreenshot(Status.Fail, "Enter Text Failed : " + ex.ToString());
}

}
public void Clear(By by)
{
driver.FindElement(by).Clear();
}
public void Click(By by)
{
try
{
driver.FindElement(by).Click();
TakeScreenshot(Status.Pass, "Click on " + by);
Log("info", "Application is working ");

}
catch (Exception ex)
{
TakeScreenshot(Status.Fail, "Click Failed : " + ex.ToString());
}
}
public void OpenUrl(string url)
{
try
{
driver.Url = url;
TakeScreenshot(Status.Pass, "URL Open");
}
catch (Exception ex)
{
TakeScreenshot(Status.Fail, "This Site can't be reached");
}
}
public string GetElementText(By by)
{
string text;
try
{
text = driver.FindElement(by).Text;
}
catch
{
try
{
text = driver.FindElement(by).GetAttribute("value");
}
catch
{
text = driver.FindElement(by).GetAttribute("innerHTML");
}
}
return text;
}
public string GetElementState(By by)
{
string elementState = driver.FindElement(by).GetAttribute("Disabled");

if (elementState == null)
{
elementState = "enabled";
}
else if (elementState == "false")
{
elementState = "enabled";
}
else if (elementState == "true")
{
elementState = "disabled";
}
return elementState;
}
public static string ExecuteJavaScriptCode(string javascriptCode)
{
string value = null;
try
{
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
value = (string)js.ExecuteScript(javascriptCode);
}
catch (Exception)
{

}
return value;
}
public static void ThreadSleepWait(int seconds)
{
//Mili Seconds =

Thread.Sleep(seconds * 1000);
}
public void QuitChrome()
{
driver.Quit();
}

}
}

You might also like