using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports.MarkupUtils;
namespace FacebookAutomation
{
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class FacebookPostTest
{
private IWebDriver driver;
private WebDriverWait wait;
private ExtentTest test;
private static ExtentReports extent;
[OneTimeSetUp]
public void BeforeClass()
{
// Create an instance of ExtentReports
extent = new ExtentReports();
// Create an HTML report and attach to ExtentReports
var htmlReporter = new ExtentHtmlReporter("TestReport.html");
extent.AttachReporter(htmlReporter);
}
[SetUp]
public void Setup()
{
// Set up ChromeDriver and initialize driver and wait
driver = new ChromeDriver();
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
// Maximize the browser window
driver.Manage().Window.Maximize();
// Create a new ExtentTest for the test case
test = extent.CreateTest(TestContext.CurrentContext.Test.Name);
// Navigate to Facebook login page
driver.Navigate().GoToUrl("https://www.facebook.com/");
}
[Test]
public void PostOnFacebook()
{
// Login to Facebook
var loginPage = new FacebookLoginPage(driver, wait, test);
loginPage.Login("your_username", "your_password");
// Write a post on the timeline
string postText = "Hello, this is a test post!";
var homePage = new FacebookHomePage(driver, wait, test);
homePage.WritePost(postText);
// Assert that the post is successfully posted
bool isPostDisplayed = homePage.IsPostDisplayed(postText);
Assert.IsTrue(isPostDisplayed, "The post was not successfully posted.");
}
[TearDown]
public void Teardown()
{
// Quit the browser
driver.Quit();
// Mark the test as pass or fail in the Extent report
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stackTrace = TestContext.CurrentContext.Result.StackTrace;
var errorMessage = TestContext.CurrentContext.Result.Message;
Status logStatus;
switch (status)
{
case NUnit.Framework.Interfaces.TestStatus.Failed:
logStatus = Status.Fail;
test.Fail(MarkupHelper.CreateLabel("Test Failed", ExtentColor.Red));
if (!string.IsNullOrEmpty(stackTrace))
test.Error(stackTrace + errorMessage);
break;
case NUnit.Framework.Interfaces.TestStatus.Passed:
logStatus = Status.Pass;
test.Pass(MarkupHelper.CreateLabel("Test Passed", ExtentColor.Green));
break;
default:
logStatus = Status.Warning;
test.Skip(MarkupHelper.CreateLabel("Test Skipped", ExtentColor.Orange));
break;
}
// Log the final status in the Extent report
test.Log(logStatus, "Test ended with " + logStatus);
}
[OneTimeTearDown]
public void AfterClass()
{
// Flush the Extent report to generate the report
extent.Flush();
}
}
public class SeleniumActions
{
protected readonly IWebDriver driver;
protected readonly WebDriverWait wait;
protected ExtentTest test;
public SeleniumActions(IWebDriver driver, WebDriverWait wait, ExtentTest test)
{
this.driver = driver;
this.wait = wait;
this.test = test;
}
protected IWebElement FindElement(By locator)
{
try
{
return wait.Until(ExpectedConditions.ElementIsVisible(locator));
}
catch (Exception ex)
{
test.Fail(ex.Message);
throw;
}
}
protected void Click(By locator)
{
try
{
FindElement(locator).Click();
}
catch (Exception ex)
{
test.Fail(ex.Message);
throw;
}
}
protected void SendKeys(By locator, string text)
{
try
{
FindElement(locator).SendKeys(text);
}
catch (Exception ex)
{
test.Fail(ex.Message);
throw;
}
}
protected void Submit(By locator)
{
try
{
FindElement(locator).Submit();
}
catch (Exception ex)
{
test.Fail(ex.Message);
throw;
}
}
protected bool IsDisplayed(By locator)
{
try
{
return FindElement(locator).Displayed;
}
catch (NoSuchElementException)
{
return false;
}
catch (Exception ex)
{
test.Fail(ex.Message);
throw;
}
}
}
public class FacebookLoginPage : SeleniumActions
{
private readonly By emailField = By.Id("email");
private readonly By passwordField = By.Id("pass");
private readonly By loginButton = By.Name("login");
public FacebookLoginPage(IWebDriver driver, WebDriverWait wait, ExtentTest test) : base(driver, wait, test)
{
}
public void Login(string username, string password)
{
// Enter username and password
SendKeys(emailField, username);
SendKeys(passwordField, password);
// Click on the login button
Click(loginButton);
}
}
public class FacebookHomePage : SeleniumActions
{
private readonly By createPostField = By.XPath("//textarea[contains(@aria-label, 'Create a post')]");
public FacebookHomePage(IWebDriver driver, WebDriverWait wait, ExtentTest test) : base(driver, wait, test)
{
}
public void WritePost(string text)
{
// Click on the create post field
Click(createPostField);
// Write the post text
SendKeys(createPostField, text);
// Submit the post
Submit(createPostField);
}
public bool IsPostDisplayed(string text)
{
// Wait for the post to appear on the timeline
By postLocator = By.XPath($"//div[contains(text(), '{text}')]");
// Check if the post is displayed
return IsDisplayed(postLocator);
}
}
}
No comments:
Post a Comment