BbJLeB

Untitled

Aug 18th, 2024
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.21 KB | None | 0 0
  1. using OpenQA.Selenium;
  2. using OpenQA.Selenium.Chrome;
  3. using OpenQA.Selenium.Interactions;
  4. using NUnit.Framework;  
  5. using System;
  6. using System.Linq;
  7.  
  8. namespace Foody
  9. {
  10.     [TestFixture]
  11.     public class FoodyTests
  12.     {
  13.         protected IWebDriver driver;
  14.         private static readonly string BaseUrl = "http://softuni-qa-loadbalancer-2137572849.eu-north-1.elb.amazonaws.com:85/";
  15.         private static string? lastCreatedFoodTitle;
  16.         private static string? lastCreatedFoodDescription;
  17.         private Actions actions;
  18.  
  19.         [OneTimeSetUp]
  20.         public void OneTimeSetup()
  21.         {
  22.             var chromeOptions = new ChromeOptions();
  23.             chromeOptions.AddUserProfilePreference("profile.password_manager_enabled", false);
  24.             chromeOptions.AddArguments("--disable-search-engine-choice-screen");
  25.             chromeOptions.AddArgument("--incognito");
  26.             chromeOptions.AddArgument("--disable-save-password-popup");
  27.             chromeOptions.AddArgument("--no-first-run");
  28.             chromeOptions.AddArgument("--no-default-browser-check");
  29.             chromeOptions.AddArgument("--disable-popup-blocking");
  30.             chromeOptions.AddArgument("--disable-extensions");
  31.             chromeOptions.AddArgument("--disable-infobars");
  32.             chromeOptions.AddArgument("--disable-notifications");
  33.             chromeOptions.AddArgument("--start-maximized");
  34.             chromeOptions.AddArgument("--disable-default-apps");
  35.             chromeOptions.AddArgument("--incognito");
  36.            
  37.  
  38.             string chromeDriverPath = @"C:\Program Files\ChromeDriver\chromedriver.exe";
  39.             driver = new ChromeDriver(chromeDriverPath, chromeOptions);
  40.             driver.Manage().Window.Maximize();
  41.             driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
  42.  
  43.             // Initialize Actions instance
  44.             actions = new Actions(driver);
  45.  
  46.             // Log in to the application
  47.             driver.Navigate().GoToUrl(BaseUrl);
  48.             var loginBtn = driver.FindElement(By.Id("loginBtn"));
  49.  
  50.             // Scroll to the login button
  51.             actions.MoveToElement(loginBtn).Perform();
  52.  
  53.             // Click the login button
  54.             loginBtn.Click();
  55.  
  56.             // Fill in login details
  57.             driver.FindElement(By.Id("DimitarIvanov")).SendKeys("[email protected]");
  58.             driver.FindElement(By.Id("DimitarIvanov")).SendKeys("123456");
  59.             driver.FindElement(By.CssSelector("button.btn.warning[type='submit']")).Click();
  60.         }
  61.  
  62.         [Test, Order(1)]
  63.         public void AddFoodWithInvalidDataTest()
  64.         {
  65.             var invalidTitleInput = "";
  66.  
  67.             driver.Navigate().GoToUrl($"{BaseUrl}/Food/Add");
  68.  
  69.             driver.FindElement(By.XPath("//input[@id='name']")).SendKeys(invalidTitleInput);
  70.  
  71.             var addButton = driver.FindElement(By.XPath("//button[@type='submit']"));
  72.             actions.ScrollToElement(addButton).Perform();
  73.             addButton.Click();
  74.  
  75.             var currentUrl = driver.Url;
  76.             Assert.That(currentUrl, Is.EqualTo($"{BaseUrl}/Food/Add"), "user should remain on the same page with same URL");
  77.  
  78.             var errorMessage = driver.FindElement(By.XPath("//li[contains(., 'Unable to add this food revue!')]"));
  79.  
  80.             Assert.That(errorMessage.Text.Trim(), Is.EqualTo("Unable to add this food revue!"), "The error message is not displayed as expected.");
  81.         }
  82.  
  83.         [Test, Order(2)]
  84.  
  85.         public void CreateRandomRevueTest()
  86.         {
  87.             lastCreatedFoodTitle = GenerateRandomString(4);
  88.             lastCreatedFoodDescription = GenerateRandomString(10);
  89.  
  90.             driver.Navigate().GoToUrl($"{BaseUrl}/Food/Add");
  91.  
  92.             driver.FindElement(By.Id("name")).SendKeys(lastCreatedFoodTitle);
  93.             driver.FindElement(By.Id("Description")).SendKeys(lastCreatedFoodDescription);
  94.             var btn = driver.FindElement(By.CssSelector(".btn"));
  95.             actions.MoveToElement(btn).Perform();  
  96.             btn.Click();  
  97.  
  98.             string currentUrl = driver.Url;
  99.             Assert.That(currentUrl, Is.EqualTo($"{BaseUrl}/"), "Browser do not navigate to the page");
  100.  
  101.            var lastFoodTitleElement = driver.FindElement(By.XPath("//"));
  102.  
  103.             string actualFoodTitle = lastFoodTitleElement.Text.Trim();
  104.             Assert.That(actualFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The last created food title does not match the expected value.");
  105.         }
  106.  
  107.         [Test, Order(3)]
  108.         public void AddFoodWithRandomTitleTest()
  109.         {
  110.             // Generate random movie title and description
  111.             lastCreatedFoodTitle = "Food: " + GenerateRandomString(5);
  112.             lastCreatedFoodDescription = "Food Description: " + GenerateRandomString(10);
  113.  
  114.             // Navigate to the Add Movie page
  115.             driver.Navigate().GoToUrl($"{BaseUrl}Catalog/Add");
  116.  
  117.             // Enter the movie title and description
  118.             driver.FindElement(By.CssSelector("input#Food")).SendKeys(lastCreatedFoodTitle);
  119.             driver.FindElement(By.CssSelector("textarea#Food")).SendKeys(lastCreatedFoodDescription);
  120.  
  121.             // Find and click the 'Add' button
  122.             var add = driver.FindElement(By.CssSelector("button.btn.warning[type='submit']"));
  123.             actions.MoveToElement(add).Perform();
  124.             add.Click();
  125.            
  126.             // Navigate to the last page of the movie listings
  127.             NavigateToLastPage();
  128.  
  129.             Assert.That(lastCreatedFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The last created food title does not match the expected value");
  130.         }
  131.  
  132.         [Test, Order(3)]
  133.         public void EditLastAddedFoodTest()
  134.         {
  135.             Assert.IsNotNull(lastCreatedFoodTitle, "There are no created foods");
  136.  
  137.             driver.Navigate().GoToUrl(BaseUrl);
  138.  
  139.             var allFoods = driver.FindElements(By.XPath("//a[@class='btn btn-primary btn-xl rounded-pill mt-5'] [text()='Edit']"));
  140.             var lastFoodEditButton = allFoods.Last();
  141.  
  142.             Actions actions = new Actions(driver);
  143.             actions.MoveToElement(lastFoodEditButton).Click().Perform();
  144.  
  145.             driver.FindElement(By.XPath("//input[@id='name']")).Click();
  146.             driver.FindElement(By.XPath("//input[@id='name']")).SendKeys($"{lastCreatedFoodTitle} + Edited");
  147.             driver.FindElement(By.XPath("//button[@type='submit']")).Click();
  148.  
  149.             Assert.That(lastCreatedFoodTitle, Is.Not.EqualTo($"{lastCreatedFoodTitle} + Edited"), "The title of the food was not changed.");
  150.             Assert.That(lastCreatedFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The title of the food was not changed.");
  151.             Console.WriteLine("Title change is not possible due to incomplete functionality");
  152.         }
  153.         [Test, Order(4)]
  154.  
  155.         public void SearchLastAddedFoodTest()
  156.         {
  157.             driver.Navigate().GoToUrl(BaseUrl);
  158.             var searchfield = driver.FindElement(By.XPath("//input[@type='search']"));
  159.             searchfield.Click();
  160.             searchfield.SendKeys(lastCreatedFoodTitle);
  161.             var searchbutton = driver.FindElement(By.XPath("//button[@type='submit']"));
  162.             var searchedfood = driver.FindElements(By.XPath("//h2[@class='display-4']"));
  163.  
  164.             var lastFoodTitle = searchedfood.Last().Text.Trim();
  165.             Assert.That(lastFoodTitle, Is.EqualTo(lastCreatedFoodTitle));
  166.  
  167.  
  168.         }
  169.         [Test, Order(5)]
  170.         p//
  171.  
  172.         [Test, Order(6)]
  173.         //
  174.  
  175.        
  176.  
  177.         [OneTimeTearDown]
  178.         public void OneTimeTearDown()
  179.         {
  180.             // Quit the driver after all tests are executed
  181.             driver.Quit();
  182.         }
  183.  
  184.         private string GenerateRandomString(int length)
  185.         {
  186.             // Generate a random string of specified length
  187.             const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  188.             var random = new Random();
  189.             return new string(Enumerable.Repeat(chars, length)
  190.               .Select(s => s[random.Next(s.Length)]).ToArray());
  191.         }
  192.  
  193.         private void NavigateToLastPage()
  194.         {
  195.             // Find the pagination elements to navigate to the last page
  196.             var paginationItems = driver.FindElements(By.CssSelector("ul.pagination li.page-item"));
  197.             var lastPageItem = paginationItems.Last();
  198.             actions.MoveToElement(lastPageItem).Perform();
  199.  
  200.             // Click the link of the actual last page
  201.             var lastPageLink = lastPageItem.FindElement(By.CssSelector("a.page-link"));
  202.             lastPageLink.Click();
  203.         }
  204.  
  205.         private void VerifyLastMovieTitle(string expectedTitle)
  206.         {
  207.             // Re-locate the movie elements on the last page
  208.             var movies = driver.FindElements(By.CssSelector(".col-lg-4"));
  209.             var lastMovieElement = movies.Last();
  210.             var lastMovieElementTitle = lastMovieElement.FindElement(By.CssSelector("h2"));
  211.  
  212.             // Verify that the last movie title matches the expected value
  213.             string actualMovieTitle = lastMovieElementTitle.Text.Trim();
  214.             Assert.That(actualMovieTitle, Is.EqualTo(expectedTitle), "The last movie title does not match the expected value.");
  215.         }
  216.     }
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment