-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from OleksiiKhorunzhak/basePage
Add PasePage, DataLoader and Serialization examples from lesson 32
- Loading branch information
Showing
9 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
//} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
BasePageAndDataLoader/SolarTechologyTests/SolarTechnologyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters