using AventStack.ExtentReports.Utils; using DemoBlazeAutomation.POM.Helper; using DemoBlazeAutomation.POM.Models; using FluentAssertions; using SeleniumExtras.WaitHelpers; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; namespace DemoBlazeAutomation.POM { /// <summary> /// Ejemplo de implementación de Pagina en POM /// </summary> public class HomePage : BasePage { public HomePage(IWebDriver driver) : base(driver) { } private int contadorDeProductos = 0; private IList<ProductModel> listaDeProductosObtenida = new List<ProductModel>(); private IList <IWebElement> ListaDeProductosUIObtenida = new List<IWebElement>(); IWebElement NextButton => GetElementWhenIsClickeable(SelectorType.Id, "next2"); IWebElement PreviousButton => GetElementWhenIsClickeable(SelectorType.Id, "prev2"); IWebElement CatPhones => GetElementWhenIsClickeable(SelectorType.LinkText, "Phones"); IWebElement CatLaptops => GetElementWhenIsClickeable(SelectorType.LinkText, "Laptops"); IWebElement CatMonitors => GetElementWhenIsClickeable(SelectorType.LinkText, "Monitors"); IWebElement SamsungGalaxyS7 => GetElementWhenIsClickeable(SelectorType.XPath, "//a[normalize-space()='Samsung galaxy s7']"); IWebElement LoginField => GetElementWhenIsClickeable(SelectorType.XPath, "//input[@id='loginusername']"); IWebElement PasswordField => GetElementWhenIsClickeable(SelectorType.XPath, "//input[@id='loginpassword']"); IWebElement BtnLogin => GetElementWhenIsClickeable(SelectorType.XPath, "//button[normalize-space()='Log in']"); IWebElement UserAuthenticate => GetElementWhenIsClickeable(SelectorType.LinkText, "Welcome admin"); IWebElement EmailField => GetElementWhenIsClickeable(SelectorType.Id, "recipient-email"); IWebElement ContactNameField => GetElementWhenIsClickeable(SelectorType.Id, "recipient-name"); IWebElement MessageTextField => GetElementWhenIsClickeable(SelectorType.Id, "message-text"); IWebElement SendMessageBtn => GetElementIsVisible(SelectorType.CssSelector, "button.btn.btn-primary"); IWebElement ModalForm => GetElementWhenIsClickeable(SelectorType.XPath, "//div[@id='exampleModal']//*"); bool SonyVaio7 => GetElementIfCanBeSelected(SelectorType.XPath, "//a[normalize-space()='Sony vaio i7']"); bool DisplayShopElements => GetElementIfCanBeSelected(SelectorType.Id, "tbodyid"); IWebElement Footer => GetElementIsVisible(SelectorType.Id, "footc"); /// <summary> /// Armo una lista con todos los elementos Web de los productos mostrados en el shop de la pagina /// </summary> public IList<IWebElement> DisplayedProducts() => BringMeAllElements("//*[@class='card h-100']", selectorType: SelectorType.XPath, 15); //private IList<IWebElement> DisplayedProducts => BringMeAllElements("//*[@class='col-lg-4 col-md-6 mb-4']", selectorType: SelectorType.XPath, 15); //private IList<IWebElement> DisplayedProducts => Driver.FindElements(By.XPath("//*[@class='card h-100']")).Where(element => element.Displayed).ToList(); /// <summary> /// Armo una lista de ProductWebElement parseando previamente la lista de IWebElements a productos que pueda manejar facilemente en C# /// </summary> /// <returns></returns> public IList<ProductWebElement> DisplayedProductWebElements() => ProductHelper.Parse(DisplayedProducts()); /// <summary> /// Retorno una lista de Product Model, es decir de cada producto transformado a elemento c# /// </summary> /// <returns></returns> public IList<ProductModel> DisplayedProductModels() => ProductHelper.Parse(DisplayedProductWebElements()); /// <summary> /// Transformación: Transformación [producto ui ] -> Transformacion [producto elemento web ] -> producto elemento c# /// </summary> public int cantidadProductsUiEncontrados() => DisplayedProductWebElements().Count(); private IWebElement GetElementByLinkText(string linkText) { IWebElement element = GetElementWhenExists(By.LinkText(linkText)); return element; } public void ClickProduct(string product) { ClickElement(GetElementByLinkText(product)); } public void GoToSection(string sectionName) { ClickElement(GetElementWhenIsClickeable(SelectorType.PartialLinkText, sectionName)); } public bool GetModal(string xpath) { return GetElementIsVisible(SelectorType.XPath, xpath).Displayed; } public void ValidateURLorModal(string value) { if (value.StartsWith("http")) { // Validar la redirección de URL string currentUrl = GetCurrentUrl(); currentUrl.Should().Be(value); } else { // Validar la apertura del modal bool modalIsAvailable = GetModal(value); modalIsAvailable.Should().BeTrue(); } } public void Login(Table table) { var user = table.CreateInstance<Account>(); Write(LoginField, user.userName); Write(PasswordField, user.password); } public void ClickBtnLogin() { ClickElement(BtnLogin); } public string UserLoginSuccesfull() { var user = UserAuthenticate.Text; return user; } public string MessageAlert() { var text = GetTextAlert(); return text; } public void CompleteAndSendForm() { Write(EmailField, "demo@gmail.com"); Write(ContactNameField, "Tin Gerez"); Write(MessageTextField, "Texto de prueba"); ClickElement(SendMessageBtn); CloseAlert(); } public void CloseAlert() { AcceptAlert(); SwitchToDefaultContent(); } public void NavigateToCatologo() { var listProductModel = new List<ProductModel>(); var listProductUI = new List<IWebElement>(); var listaDeProductosPrimeraPagina = DisplayedProductModels(); var listaDeProductosPrimeraPaginaUI = DisplayedProducts(); listProductModel.AddRange(listaDeProductosPrimeraPagina); listProductUI.AddRange(listProductUI); var time = TimeSpan.FromSeconds(3); //No es lo recomendado, pero es lo que me funcionó. Thread.Sleep(time); NextButton.Click(); Thread.Sleep(time); var listaDeProductosSegundaPagina = DisplayedProductModels(); var listaDeProductosSegundaPaginaUI = DisplayedProducts(); listProductModel.AddRange(listaDeProductosSegundaPagina); listProductUI.AddRange(listProductUI); contadorDeProductos = listProductModel.Count; listaDeProductosObtenida = listProductModel; ListaDeProductosUIObtenida = listProductUI; } public int TotalProduct() => contadorDeProductos; public IList<ProductModel> ListObtained() => listaDeProductosObtenida; public IList<IWebElement> ListUiObtained() => ListaDeProductosUIObtenida; public IWebElement FindProductByName(string productName, IList<IWebElement> listWebElements) { var h4Element = Wait.Until(ExpectedConditions.ElementToBeClickable(By.TagName("h4"))); string h4text = h4Element.Text; foreach (var product in listWebElements) { if (h4text.Equals(productName, StringComparison.OrdinalIgnoreCase)) { return product; } } return null; } public void AddProducts(string producto) { Console.WriteLine(producto); Thread.Sleep(2000); ClickProduct(producto); } } class Product { public string? NombreDelProducto { get; set; } } class Account { public string? userName { get; set; } public string? password { get; set; } } }