Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using NUnit.Framework;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Interactions;
- using System;
- using System.Linq;
- namespace Foody
- {
- [TestFixture]
- public class FoodyTests
- {
- protected IWebDriver driver;
- private static readonly string BaseUrl = "http://softuni-qa-loadbalancer-2137572849.eu-north-1.elb.amazonaws.com:85/";
- private static string? lastCreatedFoodTitle;
- private static string? lastCreatedFoodDescription;
- private Actions actions;
- [OneTimeSetUp]
- public void OneTimeSetup()
- {
- driver = new ChromeDriver();
- actions = new Actions(driver);
- driver.Navigate().GoToUrl(BaseUrl);
- driver.Manage().Window.Maximize();
- driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
- var loginBtn = driver.FindElement(By.LinkText("LOG IN"));
- var chromeOptions = new ChromeOptions();
- chromeOptions.AddArguments("--disable-search-engine-choice-screen");
- chromeOptions.AddArgument("--incognito");
- chromeOptions.AddArgument("--disable-save-password-popup");
- chromeOptions.AddArgument("--no-first-run");
- chromeOptions.AddArgument("--no-default-browser-check");
- chromeOptions.AddArgument("--disable-popup-blocking");
- chromeOptions.AddArgument("--disable-extensions");
- chromeOptions.AddArgument("--disable-infobars");
- chromeOptions.AddArgument("--disable-notifications");
- chromeOptions.AddArgument("--start-maximized");
- chromeOptions.AddArgument("--disable-default-apps");
- chromeOptions.AddArgument("--incognito");
- // Scroll to the login button
- actions.MoveToElement(loginBtn).Perform();
- // Click the login button
- loginBtn.Click();
- // Fill in login details
- driver.Navigate().GoToUrl("http://softuni-qa-loadbalancer-2137572849.eu-north-1.elb.amazonaws.com:85/");
- driver.FindElement(By.LinkText("LOG IN")).Click();
- driver.FindElement(By.Id("username")).SendKeys("DimitarIvanov");
- driver.FindElement(By.Id("password")).SendKeys("123456");
- driver.FindElement(By.CssSelector("button.btn.warning[type='submit']")).Click();
- }
- [Test, Order(1)]
- public void AddFoodWithInvalidDataTest()
- {
- var invalidTitleInput = "";
- driver.Navigate().GoToUrl($"{BaseUrl}/Food/Add");
- driver.FindElement(By.XPath("//input[@id='name']")).SendKeys(invalidTitleInput);
- var addButton = driver.FindElement(By.XPath("//button[@type='submit']"));
- actions.ScrollToElement(addButton).Perform();
- addButton.Click();
- var currentUrl = driver.Url;
- Assert.That(currentUrl, Is.EqualTo($"{BaseUrl}/Food/Add"), "user should remain on the same page with same URL");
- var errorMessage = driver.FindElement(By.XPath("//li[contains(., 'Unable to add this food revue!')]"));
- Assert.That(errorMessage.Text.Trim(), Is.EqualTo("Unable to add this food revue!"), "The error message is not displayed as expected.");
- }
- [Test, Order(2)]
- public void CreateRandomRevueTest()
- {
- lastCreatedFoodTitle = GenerateRandomString(4);
- lastCreatedFoodDescription = GenerateRandomString(10);
- driver.Navigate().GoToUrl($"{BaseUrl}/Food/Add");
- driver.FindElement(By.Id("name")).SendKeys(lastCreatedFoodTitle);
- driver.FindElement(By.Id("Description")).SendKeys(lastCreatedFoodDescription);
- var btn = driver.FindElement(By.CssSelector(".btn"));
- actions.MoveToElement(btn).Perform();
- btn.Click();
- string currentUrl = driver.Url;
- Assert.That(currentUrl, Is.EqualTo($"{BaseUrl}/"), "Browser do not navigate to the page");
- var lastFoodTitleElement = driver.FindElement(By.XPath("//"));
- string actualFoodTitle = lastFoodTitleElement.Text.Trim();
- Assert.That(actualFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The last created food title does not match the expected value.");
- }
- [Test, Order(3)]
- public void AddFoodWithRandomTitleTest()
- {
- // Generate random movie title and description
- lastCreatedFoodTitle = "Food: " + GenerateRandomString(5);
- lastCreatedFoodDescription = "Food Description: " + GenerateRandomString(10);
- // Navigate to the Add Movie page
- driver.Navigate().GoToUrl($"{BaseUrl}Catalog/Add");
- // Enter the movie title and description
- driver.FindElement(By.CssSelector("input#Food")).SendKeys(lastCreatedFoodTitle);
- driver.FindElement(By.CssSelector("textarea#Food")).SendKeys(lastCreatedFoodDescription);
- // Find and click the 'Add' button
- var add = driver.FindElement(By.CssSelector("button.btn.warning[type='submit']"));
- actions.MoveToElement(add).Perform();
- add.Click();
- // Navigate to the last page of the movie listings
- NavigateToLastPage();
- Assert.That(lastCreatedFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The last created food title does not match the expected value");
- }
- [Test, Order(3)]
- public void EditLastAddedFoodTest()
- {
- if (string.IsNullOrEmpty(lastCreatedFoodTitle))
- {
- Console.WriteLine("No created foods available to edit.");
- return; // Прекратява теста, ако няма създадени храни
- }
- driver.Navigate().GoToUrl(BaseUrl);
- var allFoods = driver.FindElements(By.XPath("//a[@class='btn btn-primary btn-xl rounded-pill mt-5'] [text()='Edit']"));
- var lastFoodEditButton = allFoods.Last();
- Actions actions = new Actions(driver);
- actions.MoveToElement(lastFoodEditButton).Click().Perform();
- var nameInput = driver.FindElement(By.XPath("//input[@id='name']"));
- nameInput.Clear(); // Изчиства старото заглавие
- nameInput.SendKeys($"{lastCreatedFoodTitle} + Edited");
- driver.FindElement(By.XPath("//button[@type='submit']")).Click();
- // Логика за асертиране, че заглавието не се е променило, както и съобщение в конзолата
- Assert.That(lastCreatedFoodTitle, Is.Not.EqualTo($"{lastCreatedFoodTitle} + Edited"), "The title of the food was not changed.");
- Assert.That(lastCreatedFoodTitle, Is.EqualTo(lastCreatedFoodTitle), "The title of the food was not changed.");
- Console.WriteLine("Title change is not possible due to incomplete functionality");
- }
- [Test, Order(4)]
- public void SearchLastAddedFoodTest()
- {
- driver.Navigate().GoToUrl(BaseUrl);
- var searchfield = driver.FindElement(By.XPath("//input[@type='search']"));
- searchfield.Click();
- searchfield.SendKeys(lastCreatedFoodTitle);
- var searchbutton = driver.FindElement(By.XPath("//button[@type='submit']"));
- var searchedfood = driver.FindElements(By.XPath("//h2[@class='display-4']"));
- var lastFoodTitle = searchedfood.Last().Text.Trim();
- Assert.That(lastFoodTitle, Is.EqualTo(lastCreatedFoodTitle));
- }
- [Test, Order(5)]
- //
- public void DeleteLastAddedFoodTest()
- {
- driver.Navigate().GoToUrl(BaseUrl);
- var allFoods = driver.FindElements(By.XPath("//a[@class='btn btn-primary btn-xl rounded-pill mt-5'] [text(🙁'Delete']"));
- var allfoodcount = allFoods.Count();
- var lastFoodDeleteButton = allFoods.Last();
- Assert.That(allfoodcount, Is.EqualTo(allFoods.Count() - 1));
- }
- [Test, Order(6)]
- public void Test_Search_For_Deleted_Food()
- {
- driver.Navigate().GoToUrl(BaseUrl);
- var searchField = driver.FindElement(By.XPath("//input[@type='search']"));
- searchField.Click();
- searchField.SendKeys(lastCreatedFoodTitle);
- driver.FindElement(By.XPath("//button[@type='submit']")).Click();
- var searchResult = driver.FindElement(By.XPath("//h2[@class='display-4']"));
- Assert.That(searchResult.Text, Is.EqualTo("There are no foods :("));
- var addButton = driver.FindElement(By.CssSelector("a.nav-link[href='/Food/Add']"));
- Assert.True(addButton.Displayed);
- }
- [OneTimeTearDown]
- public void OneTimeTearDown()
- {
- // Quit the driver after all tests are executed
- driver.Quit();
- }
- private string GenerateRandomString(int length)
- {
- // Generate a random string of specified length
- const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var random = new Random();
- return new string(Enumerable.Repeat(chars, length)
- .Select(s => s[random.Next(s.Length)]).ToArray());
- }
- private void NavigateToLastPage()
- {
- // Find the pagination elements to navigate to the last page
- var paginationItems = driver.FindElements(By.CssSelector("ul.pagination li.page-item"));
- var lastPageItem = paginationItems.Last();
- actions.MoveToElement(lastPageItem).Perform();
- // Click the link of the actual last page
- var lastPageLink = lastPageItem.FindElement(By.CssSelector("a.page-link"));
- lastPageLink.Click();
- }
- private void VerifyLastMovieTitle(string expectedTitle)
- {
- // Re-locate the movie elements on the last page
- var movies = driver.FindElements(By.CssSelector(".col-lg-4"));
- var lastMovieElement = movies.Last();
- var lastMovieElementTitle = lastMovieElement.FindElement(By.CssSelector("h2"));
- // Verify that the last movie title matches the expected value
- string actualMovieTitle = lastMovieElementTitle.Text.Trim();
- Assert.That(actualMovieTitle, Is.EqualTo(expectedTitle), "The last movie title does not match the expected value.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment