Tuesday, June 20, 2023

nunit with c#

To add extent reports to the existing code, you need to include the ExtentReports and ExtentTest libraries, initialize the ExtentReports object, and create ExtentTest instances for reporting the test status. Here's an updated version of the code with Extent Reports integration:

```csharp
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using System.Threading;

namespace FacebookTests
{
    [TestFixture]
    [Parallelizable(ParallelScope.Fixtures)]
    public class FacebookBaseTest
    {
        protected IWebDriver driver;
        protected ExtentReports extent;
        protected ExtentTest test;

        [OneTimeSetUp]
        public void OneTimeSetup()
        {
            // Initialize ExtentReports with HTML reporter
            var htmlReporter = new ExtentHtmlReporter("TestReport.html");
            extent = new ExtentReports();
            extent.AttachReporter(htmlReporter);
        }

        [OneTimeTearDown]
        public void OneTimeCleanup()
        {
            // Flush the ExtentReports instance to generate the report
            extent.Flush();
        }

        [SetUp]
        public void Setup()
        {
            // Initialize ChromeDriver
            driver = new ChromeDriver();

            // Maximize the browser window
            driver.Manage().Window.Maximize();

            // Create a new ExtentTest for the current test
            test = extent.CreateTest(TestContext.CurrentContext.Test.Name);
        }

        [TearDown]
        public void Cleanup()
        {
            // Close the browser
            driver.Quit();

            // End the ExtentTest for the current test
            test.Log(Status.Info, "Test completed");

            // Get the result of the test and log it accordingly
            var result = TestContext.CurrentContext.Result.Outcome;
            if (result == ResultState.Success)
                test.Pass("Test Passed");
            else if (result == ResultState.Failure)
            {
                test.Fail("Test Failed");
                test.AddScreenCaptureFromPath(CaptureScreenshot());
            }
            else if (result == ResultState.Inconclusive || result == ResultState.Skipped)
                test.Skip("Test Skipped");
        }

        protected string CaptureScreenshot()
        {
            // Capture and save a screenshot of the current browser window
            var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            var screenshotPath = $"Screenshots/{TestContext.CurrentContext.Test.Name}.png";
            screenshot.SaveAsFile(screenshotPath, ScreenshotImageFormat.Png);
            return screenshotPath;
        }

        protected void WaitForElementDisplayed(By locator)
        {
            // Wait for an element to be displayed on the page
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(locator));
        }
    }

    public class FacebookPostTests : FacebookBaseTest
    {
        private LoginPage loginPage;
        private HomePage homePage;

        [SetUp]
        public void TestSetup()
        {
            // Initialize page objects
            loginPage = new LoginPage(driver);
            homePage = new HomePage(driver);
        }

        [Test]
        public void PostOnFacebookTest()
        {
            // Navigate to Facebook
            driver.Navigate().GoToUrl("https://www.facebook.com");

            // Perform login
            loginPage.Login("your_username", "your_password");

            // Post a message on Facebook
            homePage.PostStatus("Hello, Facebook!");

            // Wait for the post to be created
            WaitForElementDisplayed(By.XPath($"//*[contains(text(), 'Hello, Facebook!')]"));

            // Assert that the post was successfully created
            Assert.IsTrue(homePage.IsPost

No comments:

Post a Comment