2

I have added 2 products in my basket. in the first step of my Test. In the last step I assert that same product that were added in the first step of the test comes in the Last step which is the "Order Summary page". Please find below the code and screen shots. Here there are 2 items, all the features of the 2 items displayed have same classes. just the indexing of the div is different. rest is the same.

I am using the Scenario Context functionality of the Specflow.

Mindwell, i want to achieve like this image , i have code currently for only 1 product, and i want to do the same for multiple products.

1) Basketpage. In this step i take all the elements of the page and take their values in the scenario context.

enter image description here

string productname = pdpPage.getBrandName();
pdpPage.ExpandSideBar();
pdpPage.SelectProductQuantity(Quantity);
var hp = new HeaderPage(driver);
int currentBagQuantity = hp.getBagQuantity();
decimal currentTotalBagPrice = hp.getBagTotalPrice();

ScenarioContext.Current.Add("Product Name",productname);
ScenarioContext.Current.Add("QuantityAdded", int.Parse(Quantity));
ScenarioContext.Current.Add("BagQuantity", currentBagQuantity);
ScenarioContext.Current.Add("CurrentBagPrice", currentTotalBagPrice);
ScenarioContext.Current.Add("ProductPrice", pdpPage.getProductPriceInDecimal());

2) OrderSummary Page. In this step i assert the values , This is the order summary page. enter image description here

var os = new OrderSummaryPage(driver);
string brandname = os.getOrderProductName();
            int quantity = os.getOrderQuantity();
            decimal price = os.getOrderPrice();

Assert.IsTrue(brandname.Equals((string)ScenarioContext.Current["Product Name"]), "Err! Product is different!, on pdp is :" + ScenarioContext.Current["Product Name"] + "on order summary is" + brandname);
Assert.IsTrue(quantity.Equals((int)ScenarioContext.Current["QuantityAdded"]), "Err! Quantity is different from ordered!");
Assert.IsTrue(price.Equals((decimal)ScenarioContext.Current["ProductPrice"]), "Err! Product price is appearing to be different!");
Assert.IsTrue(GenericFunctions.isElementPresent(os.Delivery_Address), "Delivery Address details are not present");
Assert.IsTrue(GenericFunctions.isElementPresent(os.Billing_Address), "Billing Address details are not present!!");

I am new to this stuff!! How to loop these and get the dynamic stuff. I want to check and verify each items Product name , price , quantity.

Doing this :

My Step File :

[When(@"I check the items on basket page")]
        public void WhenICheckTheItemsOnBasketPage()
        {
            var bp = new BasketPage(driver);
            var h = bp.getLISTItemsFromOrderPage();
        for (int i = 0; i <= h.Count; i++)
        {
            ScenarioContext.Current.Add("item", h[i]);
        }
         }

BasketPage.cs

public IList getLISTItemsFromOrderPage()
        {
          List<BasketItems> orderProducts = new List<BasketItems>();
          var elements = (driver.FindElements(By.Id("basketitem")));

          foreach (IWebElement element in elements)
          {
            orderProducts.Add(CreateOrderProduct(element));
          }
          return orderProducts;
        }

public BasketItems CreateOrderProduct(IWebElement item)
                    {
                        return new BasketItems()
                       {
                           BrandName = item.FindElement(By.TagName("a")).Text.Trim(),
                           Quantity = GenericFunctions.DropDown_GetCurrentValue(item.FindElement(By.TagName("select"))),
                           Price = Convert.ToDecimal(item.FindElement(By.ClassName("col-md-2")).Text.Substring(1))
                       };
                    }

BasketItem.cs

public class BasketItems : BasketPageOR
    {
        public string BrandName { get; set; }
        public string Quantity { get; set; }
        public decimal Price { get; set; } 
    }

Please Help! Thanks in Advance!!

Arpan Buch
  • 1,380
  • 5
  • 19
  • 41
  • 1
    I dont know equivalent in C#, but in java you could use `getElements` function to get all items with same tag. Here you can get elements with `id=productorderitems`. `getElements `returns a List of elements (in java), therefore you can iterate over the list to get your desired validations to perform. – Vivek Singh Dec 15 '14 at 13:12
  • @VivekSingh : I think you were getting at **"findElements"**, right ? because **getElements** is not a pre-defined method in java.. :) – Subh Dec 15 '14 at 13:58
  • yup sorry my mistake...just wrote it incorrectly...use `foreach(IWebElement element in list)` to iterate over it... – Vivek Singh Dec 15 '14 at 19:18

1 Answers1

2

you method os.getOrderProductName(); doesn't make any sense if an order can have multiple products.

You should have a method os.getOrderProducts(); which will return a collection of OrderProduct objects. It should do this by finding all elements which have an id="productorderelement" (although you should not have elements with the same id, you should really use a class for this) and then loop over each element extracting the information to build the OrderProduct soemthing like this should allow you to get the elements with the id:

List<OrderProduct> orderProducts = new List<OrderProduct>();
var elements = (Driver.FindElements(By.XPath("//*[@id=\"productorderelement\"]")))
foreach (element in elements)
{
    orderProducts.Add(CreateOrderProduct(element));
}

public class OrderProduct
{
    public string BrandName{get;set;}
    public int Quantity{get;set;}
    public double Price{get;set;}    
}

public OrderProduct CreateOrderProduct(IWebElement element)
{
     return new OrderProduct()
     {
          BrandName= element.Something, //you need to extract the appropriate bit of the webelement that holds the brandname, quantity and price, but you don't show us the structure so I can't help there
          Quantity= element.FindElement(By.Class("quantity")).Text, //for example
          Price= element.GetAttribute("Price") //again another example
     }
}
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • ok.. So i have few questions here (1) What is the here and . (2) What will be CreateOrderProduct(element) ?? – Arpan Buch Dec 17 '14 at 05:56
  • `OrderProduct` is a class you need to create which will hold the details of a product, basically the brandname, quantity & price, and `CreateOrderProduct` if a method which extract those values from each `productorderitems` div and create a `OrderProduct` from it – Sam Holder Dec 17 '14 at 09:25
  • It's not heavy at all, example added. – Sam Holder Dec 19 '14 at 10:47
  • you are just awesome Sam! Thanks,I have almost reached to what i wanted. i am going to the next part of solution, keep u posted!! Thanks so much :) – Arpan Buch Dec 19 '14 at 13:24
  • okay now i figured it out how this works!! But Sam one more question arises is how will I assert the values such as quantity name price. I will need to extract data from object – Arpan Buch Dec 19 '14 at 18:57