Skip to content

Commit

Permalink
Merge pull request #176 from OleksiiKhorunzhak/basePage
Browse files Browse the repository at this point in the history
Add PasePage, DataLoader and Serialization examples from lesson 32
  • Loading branch information
vldemyan authored Aug 27, 2024
2 parents c7d4a80 + 2f04c6f commit 386083b
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 0 deletions.
25 changes: 25 additions & 0 deletions BasePageAndDataLoader/BasePageAndDataLoader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.45.1" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions BasePageAndDataLoader/Data/DataLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolarTechnology.Data
{
public class ManufacturerData
{
public string Manufacturer { get; set; }
public int Id { get; set; }
public string aaaaa { get; set; }
}

public static class DataLoader
{
public static string GetManufacturer()
{
var save = new ManufacturerData();
save.Manufacturer = "save text";
save.Id = 20;


TestDataLoader.SaveJson<ManufacturerData>(@"C:\tmp\m2.json", save);

var testData = TestDataLoader.LoadJson(@"C:\tmp\m.json");

// Use test data in the test
return testData.Manufacturer;

}
}
}
63 changes: 63 additions & 0 deletions BasePageAndDataLoader/Data/TestDataLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.IO;
using Newtonsoft.Json;

namespace SolarTechnology.Data
{

public static class TestDataLoader
{
/// <summary>
/// Loads test data from a JSON file.
/// </summary>
/// <typeparam name="T">The type of object to deserialize the data into.</typeparam>
/// <param name="filePath">The relative or absolute path to the JSON file.</param>
/// <returns>The deserialized object of type T.</returns>
public static ManufacturerData LoadJson(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"The file '{filePath}' was not found.");
}

string jsonData = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<ManufacturerData>(jsonData);
}

public static void SaveJson<T>(string filePath, T objectToSave)
{
File.WriteAllText(filePath, JsonConvert.SerializeObject(objectToSave));
}

/// <summary>
/// Loads test data from a CSV file.
/// </summary>
/// <param name="filePath">The relative or absolute path to the CSV file.</param>
/// <returns>A list of string arrays representing the rows in the CSV file.</returns>
public static List<string[]> LoadCsv(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"The file '{filePath}' was not found.");
}

var lines = File.ReadAllLines(filePath);
return lines.Select(line => line.Split(',')).ToList();
}

/// <summary>
/// Loads test data from a plain text file.
/// </summary>
/// <param name="filePath">The relative or absolute path to the text file.</param>
/// <returns>A string representing the content of the text file.</returns>
public static string LoadText(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"The file '{filePath}' was not found.");
}

return File.ReadAllText(filePath);
}
}

}
30 changes: 30 additions & 0 deletions BasePageAndDataLoader/PageObjects/BasePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Playwright;

namespace SolarTechnology.PageObjects
{
internal abstract class BasePage
{
internal IPage Page;


public BasePage(IPage page)
{
Page = page;
}

public abstract string GetUrl();

public async Task GoTo()
{
await Page.GotoAsync(GetUrl());
}

public async Task WaitForLoader()
{
ILocator locator = Page.Locator("#p_prldr");
await locator.WaitForAsync(new() { State = WaitForSelectorState.Visible });
await locator.WaitForAsync(new() { State = WaitForSelectorState.Hidden });
}

}
}
26 changes: 26 additions & 0 deletions BasePageAndDataLoader/PageObjects/ShopPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Playwright;

namespace SolarTechnology.PageObjects
{
internal class ShopPage : BasePage
{
//private IPage _page;
private string shopPageUrl = "https://solartechnology.com.ua/shop";

public override string GetUrl()
{
return shopPageUrl;
}

public ShopPage(IPage page) : base (page)
{
//_page = page;
}

//public async Task GoToShopPage()
//{
// await Page.GotoAsync(shopPageUrl);
//}

}
}
74 changes: 74 additions & 0 deletions BasePageAndDataLoader/PageObjects/SolarPanelsPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.Playwright;

namespace SolarTechnology.PageObjects
{
internal class SolarPanelsPage : BasePage
{
//private IPage Page;
private string shopSolarPanelsUrl = "https://solartechnology.com.ua/shop/solar-panels";
public override string GetUrl()
{
return shopSolarPanelsUrl;
}

public SolarPanelsPage(IPage page) : base(page)
{
//Page = page;
}

//public async Task GoToSolarPanelsPage()
//{
// await Page.GotoAsync(shopSolarPanelsUrl);
//}

public async Task ClickFiltersButton()
{
await Page.GetByText("Фільтр товарів").ClickAsync();
}

public async Task<string> GetFirstProductFullTitle()
{
return await Page.Locator(".list-product-title").First.InnerTextAsync();
}

public async Task ClickFilterManufacturerByName(string name)
{
//var waitForRequestTask = Page.WaitForRequestAsync("**/solar-panels?filter%5Bbrand%5D=*");
await Page.Locator($"//span[text()='{name}']").ClickAsync();

await WaitForLoader();

//await waitForRequestTask;
}

public async Task VerifySolarPanelsPageLoaded()
{
//await Page.WaitForURLAsync(shopSolarPanelsUrl);
await Page.WaitForURLAsync(GetUrl());
var linkElements = Page.GetByRole(AriaRole.Link);
await Assertions.Expect(linkElements.First).ToBeVisibleAsync();
}

public async Task VerifyFilterSectionVisible()
{
await Assertions.Expect(Page.Locator("[name='filter[elements]']").First).ToBeVisibleAsync();
}

public async Task CheckProductSectionLoaded()
{
await Assertions.Expect(Page.Locator(".list-product-title").Last).ToBeVisibleAsync();
}

public async Task<bool> IsFirstProductTitleContainsExpectedText(string expected_text)
{
await Page.WaitForSelectorAsync(".list-product-title");
string productInnertext = await Page.Locator(".list-product-title").First.InnerTextAsync();
var spaceIndex = productInnertext.IndexOf(' ');

string trimmedExpected = expected_text.Remove(spaceIndex).ToLower();
string trimmedTitle = productInnertext.Remove(spaceIndex).ToLower();

return (trimmedExpected == trimmedTitle);
}
}
}
46 changes: 46 additions & 0 deletions BasePageAndDataLoader/SolarTechologyTests/SolarTechnologyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using PlaywrigthUITests;
using SolarTechnology.Data;
using SolarTechnology.PageObjects;

namespace SolarTechnology.SolarTechologyTests;

public class SolarTechnologyTests : UITestFixture
{
private ShopPage _shopPage;
private SolarPanelsPage _solarPanelsPage;

[Description("Checking 'Solar panels' page functionality")]
[SetUp]
public void SetupShopPage()
{
_shopPage = new ShopPage(Page);
_solarPanelsPage = new SolarPanelsPage(Page);
}

[Test]
[Description("Verify that catalog filter by name works correctly")]
public async Task CheckSolarPanelCatalogFilterByName()
{
//Arrange
await _shopPage.GoTo();

//await _solarPanelsPage.GoToSolarPanelsPage();
await _solarPanelsPage.GoTo();
await _solarPanelsPage.VerifySolarPanelsPageLoaded();
await _solarPanelsPage.ClickFiltersButton();
await _solarPanelsPage.VerifyFilterSectionVisible();

//Act

var manufacturer = DataLoader.GetManufacturer();

//string manufacturer = await _solarPanelsPage.GetFilterManufacturerNameById(id);
await _solarPanelsPage.CheckProductSectionLoaded();
await _solarPanelsPage.ClickFilterManufacturerByName(manufacturer);
var result = await _solarPanelsPage.IsFirstProductTitleContainsExpectedText(manufacturer);

//Assert
Assert.True(result == true, $"Manufaturer {manufacturer} is not equal to expected product title after filtering");
}

}
36 changes: 36 additions & 0 deletions BasePageAndDataLoader/UITestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Playwright;

namespace PlaywrigthUITests
{
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class UITestFixture
{
public IPage Page { get; private set; }
private IBrowser browser;

[SetUp]
public async Task Setup()
{
var playwrightDriver = await Playwright.CreateAsync();
browser = await playwrightDriver.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
Args = new List<string> { "--start-maximized" }
});
var context = await browser.NewContextAsync(new BrowserNewContextOptions
{
ViewportSize = ViewportSize.NoViewport
});

Page = await context.NewPageAsync();
}

[TearDown]
public async Task Teardown()
{
await Page.CloseAsync();
await browser.CloseAsync();
}
}
}
6 changes: 6 additions & 0 deletions HilelTests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeleniumWithApi", "Selenium
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DifferentExamples", "DifferentExamples\DifferentExamples.csproj", "{6812F743-9C6A-4AFF-B4CD-4F124403B997}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasePageAndDataLoader", "BasePageAndDataLoader\BasePageAndDataLoader.csproj", "{30ADB641-B9F1-4ACE-AA4A-11AA59325636}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{6812F743-9C6A-4AFF-B4CD-4F124403B997}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6812F743-9C6A-4AFF-B4CD-4F124403B997}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6812F743-9C6A-4AFF-B4CD-4F124403B997}.Release|Any CPU.Build.0 = Release|Any CPU
{30ADB641-B9F1-4ACE-AA4A-11AA59325636}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30ADB641-B9F1-4ACE-AA4A-11AA59325636}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30ADB641-B9F1-4ACE-AA4A-11AA59325636}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30ADB641-B9F1-4ACE-AA4A-11AA59325636}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 386083b

Please sign in to comment.