Commit 4bea7b21 authored by hasan  khaddour's avatar hasan khaddour

Lab 3 homework code

parent d22657f8
using System.Collections.Generic;
namespace Dummy.SeleniumTests.SigninTets.Pages
{
public interface IStorePage
{
/// <summary>
/// this function return a list of the codes of the items in the shop
/// </summary>
/// <returns>List of int represent the items code</returns>
public IEnumerable<int> GetItemsCodeList();
/// <summary>
/// returns the code of the first item in the shop
/// </summary>
/// <returns>an integer represent the code of the first item or null if no items are available</returns>
public int? FirstItemCode();
/// <summary>
/// returns the code of the last item in the shop
/// </summary>
/// <returns>an integer represent the code of the last item or null if no items are available</returns>
public int? GetLastItemCode();
/// <summary>
/// set the quantity for a specific item in the shop
/// </summary>
/// <param name="quantity">rhe desired quantity to set for the item</param>
/// <param name="code">the code of the item for which the quantity will be sets</param>
public void SetItemQuantity(int quantity, int code);
/// <summary>
/// add an item to the cart
/// </summary>
/// <param name="code">the code of the item to add it to the cart</param>
public void AddItem(int code);
/// <summary>
/// calculates the total fee for a given item based on it s code and the quantity
/// </summary>
/// <param name="itemCode">the code of the item</param>
/// <param name="quantity">the quantity of the item</param>
/// <returns>the calculated total fee as an integer</returns>
public int CalculateFee(int itemCode, int quantity);
/// <summary>
/// retrieves the GST amount for the cart
/// </summary>
/// <returns>The GST amount as an integer</returns>
public int GetGSt();
/// <summary>
/// gets the fee of a specific item by its code
/// </summary>
/// <param name="itemCode">he code of the item</param>
/// <returns>the fee of the item as an integer</returns>
public int GetFee(int itemCode);
/// <summary>
/// retrieves the total cost of items in the cart
/// </summary>
/// <returns>ش string represent the total cost</returns>
public int GetTotal();
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Dummy.SeleniumTests.SigninTets.Pages
{
public class StorePage : IStorePage
{
private IWebDriver _driver;
private WebDriverWait _wait;
//Locators ( UI Elements)
private readonly By totalCost = By.Id("cart_total_price");
private readonly By gst = By.Id("cart_total_sales_tax");
private readonly By itemRowSelector = By.CssSelector(".billing_item_services");
// item quantity and add button prefixes
private readonly String itemQuantityPrefix = "quantity-";
private readonly String itemAddButtonPrefix = "add_service_";
public StorePage(IWebDriver driver)
{
_driver = driver;
_wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(1));
}
public IEnumerable<int> GetItemsCodeList()
{
List<int> itemCodes = new ();
// Find all item rows and extract codes friom them
var itemRows = _wait.Until(d => d.FindElements(itemRowSelector));
foreach (IWebElement row in itemRows)
{
// Locate the anchor tag within the row
// // to retrieve the code from its id attribute
var addButton = row.FindElement(By.XPath(".//td/a[starts-with(@id, 'add_service_')]"));
string idAttribute = addButton.GetAttribute("id");
// Extract the code from the id attribute, like add_service_33 ===> 33
if (int.TryParse(idAttribute.Split('_').Last(), out int code))
{
itemCodes.Add(code);
}
}
return itemCodes;
}
public int? FirstItemCode()
{
var itemCodes = GetItemsCodeList();
return itemCodes.Any() ? itemCodes.First() : (int?)null;
}
public int? GetLastItemCode()
{
var itemCodes = GetItemsCodeList();
return itemCodes.Any() ? itemCodes.Last() : (int?)null;
}
public void SetItemQuantity(int quantity,int code )
{
var selectItem =_wait.Until(d => d.FindElement(By.Id(itemQuantityPrefix + code)));
selectItem.SendKeys(quantity.ToString());
}
public void AddItem(int code)
{
var addButton= _wait.Until(d => d.FindElement(By.Id(itemAddButtonPrefix + code)));
addButton.Click();
}
public int CalculateFee(int itemCode,int quantity)
{
int fee = GetFee(itemCode);
return (quantity * (fee))/10;
}
public int GetGSt()
{
var gstAmount= _wait.Until(d => d.FindElement(gst));
return parseCurrency(gstAmount.Text);
}
public int GetFee(int itemCode )
{
// XPath to locate the row containing the specific item code in the anchor tah
string feeXPath = $"//tr[td/a[@id='add_service_{itemCode}']]/td[3]";
var fee = _wait.Until(d => d.FindElement(By.XPath(feeXPath)));
return parseCurrency(fee.Text) ;
}
public int GetTotal()
{
var totalAmount = _wait.Until(d => d.FindElement(totalCost));
return parseCurrency(totalAmount.Text);
}
private int parseCurrency(string currencyText)
{
return int.TryParse(currencyText.Replace("A$", "").Replace(",", "").Trim(), out int result) ? result : 0;
}
}
}
using Dummy.SeleniumTests.SigninTets.Pages;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Dummy.SeleniumTests.ShoppingCartTest.Tests
{
public class StoreTest
{
public IStorePage _storePage;
public SigninPage _siginPage;
private IWebDriver _driver;
//
public string HostName = "https://whenwise.agileway.net/biz/wise-pool/store";
public string LoginHostName = "https://whenwise.agileway.net/sign-in";
[SetUp]
public void SetUp()
{
_driver = new ChromeDriver();
_driver.Navigate().GoToUrl(LoginHostName);
_siginPage = new SigninPage(_driver);
_siginPage.EnterUserName("james@client.com");
_siginPage.EnterPassword("test01");
_siginPage.Submit();
_driver.Navigate().GoToUrl(HostName);
_storePage = new StorePage(_driver);
}
[Test]
public void GetItemsCodeList_ShouldReturnListOfCodes()
{
// Act
var itemCodes = _storePage.GetItemsCodeList();
// Assert
Assert.That(itemCodes, Is.Not.Empty);
Assert.IsTrue(itemCodes.All(code => code > 0));
}
[Test]
public void TestCartStore_FullScenario()
{
int qty = 10;
int? fcode = _storePage.FirstItemCode();
Assert.IsTrue(fcode is not null, "the list should conatin an item");
_storePage.SetItemQuantity(qty, (int)fcode);
_storePage.AddItem((int)fcode);
int tfee = _storePage.CalculateFee((int)fcode, 10);
int itemPrice = _storePage.GetFee((int)fcode);
int actualTotal = _storePage.GetTotal();
int actualGST = _storePage.GetGSt();
Assert.IsTrue(actualGST.Equals(tfee), "GST does not match the expected value");
Assert.IsTrue(actualTotal.Equals(tfee + qty * itemPrice), "Total cost does not match the expected value");
}
[Test]
public void GetTotal_ShouldReturnCorrectTotalAmount()
{
// Acts
// get item codes list
var itemCodes = _storePage.GetItemsCodeList();
// setting quantity
int quantity = 2;
// get fee
int fee = _storePage.GetFee(itemCodes.First());
// set the quantity
_storePage.SetItemQuantity(quantity, itemCodes.First());
// add the item
_storePage.AddItem(itemCodes.First());
// the expected total
int expectedTotal = fee * quantity;
// Assert
Assert.AreEqual(expectedTotal, _storePage.GetTotal(), "Total amount does not match expected value");
}
[Test]
public void AddItem_ShouldAddItemToCart()
{
// Acts
var itemCodes = _storePage.GetItemsCodeList();
_storePage.AddItem(itemCodes.First());
// verify that item has been added by checking the total
int fee = _storePage.GetFee(itemCodes.First());
int expectedTotal = fee;
// Assert
Assert.AreEqual(expectedTotal, _storePage.GetTotal(), "Total does not match expected value after adding one item");
}
[Test]
public void FirstItemCode_ShouldReturnFirstItemCode()
{
var itemCodes = _storePage.GetItemsCodeList();
var firstItemCode = _storePage.FirstItemCode();
Assert.AreEqual(itemCodes.FirstOrDefault(), firstItemCode, "First item code does not match the expected value");
}
[Test]
public void GetLastItemCode_ShouldReturnLastItemCode()
{
// Acts
var itemCodes = _storePage.GetItemsCodeList();
var lastItemCode = _storePage.GetLastItemCode();
// Assert
Assert.AreEqual(itemCodes.LastOrDefault(), lastItemCode, "Last item code does not match the expected value");
}
[Test]
public void CalculateFee_ShouldReturnCorrectTotalForQuantity()
{
// Acts
var itemCodes = _storePage.GetItemsCodeList();
int quantity = 3;
int fee = _storePage.GetFee(itemCodes.First());
int expectedFee = quantity * fee / 10 ;
int calculatedFee = _storePage.CalculateFee(itemCodes.First(), quantity);
// Assert
Assert.AreEqual(expectedFee, calculatedFee, "Calculated fee does not match expected value");
}
[TearDown]
public void TearDown()
{
_driver.Quit();
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
namespace Dummy.SeleniumTests.SigninTets.Pages
{
public class StorePage
{
private IWebDriver _driver;
private WebDriverWait _wait;
//Locators ( UI Elements)
private By totalCost = By.Id("cart_total_price");
private By gst = By.Id("cart_total_sales_tax");
public String firstItemFee = "//*[@id=\"store_cart_products\"]/tbody/tr[1]/td[3]";
public String secondItemFee = "//*[@id=\"store_cart_products\"]/tbody/tr[2]/td[3]";
private String itemQuantity = "quantity-";
private String itemAddButton = "add_service_";
public int FirstItemCode()
{
return 41;
}
public int SecondItemCode()
{
return 33;
}
public StorePage(IWebDriver driver)
{
_driver = driver;
_wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
}
public void SetItemQuantity(int quantity,int code )
{
var selectList =_wait.Until(d => d.FindElement(By.Id(itemQuantity + code)));
selectList.SendKeys(""+quantity);
}
public void SetFirstItemQuantity(int quantity)
{
SetItemQuantity(quantity,SecondItemCode());
}
public void SetSecondItemQuantity(int quantity)
{
SetItemQuantity(quantity, FirstItemCode());
}
public void AddItem(int code)
{
var addButton= _wait.Until(d => d.FindElement(By.Id(itemAddButton + code)));
addButton.Click();
}
public int calculateFee(string feeXPath,int quantity)
{
int fee= GetFee(feeXPath);
return (quantity * (fee))/10;
}
public string GetGSt()
{
var gstAmount= _wait.Until(d => d.FindElement(gst));
return gstAmount.Text;
}
public int GetFee(String feeXPath )
{
var fee = _wait.Until(d => d.FindElement(By.XPath(feeXPath)));
return Int32.Parse(fee.Text.Split("$")[2]) ;
}
public string GetTotal()
{
var totalAmount = _wait.Until(d => d.FindElement(totalCost));
return totalAmount.Text;
}
}
}
using Dummy.SeleniumTests.SigninTets.Pages;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Dummy.SeleniumTests.SigninTets.Tests
{
public class StoreTest
{
public StorePage _storePage;
public SigninPage _siginPage;
private IWebDriver _driver;
//
public String HostName = "https://whenwise.agileway.net/biz/wise-pool/store";
public String LoginHostName = "https://whenwise.agileway.net/sign-in";
[SetUp]
public void SetUp()
{
_driver = new ChromeDriver();
_driver.Navigate().GoToUrl(LoginHostName);
_siginPage = new SigninPage(_driver);
_siginPage.EnterUserName("james@client.com");
_siginPage.EnterPassword("test01");
_siginPage.Submit();
_driver.Navigate().GoToUrl(HostName);
_storePage = new StorePage(_driver);
}
[Test]
public void TestCartStore()
{
int qty = 10;
int fcode=_storePage.FirstItemCode();
_storePage.SetItemQuantity(qty,fcode);
_storePage.AddItem(fcode);
int tfee = _storePage.calculateFee(_storePage.firstItemFee, 10);
int itemPrice = _storePage.GetFee(_storePage.firstItemFee);
string actualTotal = _storePage.GetTotal();
string actualGST = _storePage.GetGSt();
Assert.IsTrue(actualGST.Equals("A$"+tfee+".00"));
Assert.IsTrue(actualTotal.Equals("A$"+tfee+qty*itemPrice+".00"));
}
[TearDown]
public void TearDown()
{
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment