在现有代码中实现循环C#

时间:2018-07-04 09:39:22

标签: c# selenium selenium-webdriver

此处是C#的新手。我已经写了代码,可以在网站上搜索,搜索商品,单击商品,将其添加到购物袋中,然后单击购物篮并不断添加,直到达到200美元的价格上限为止。

我现在想引入循环,这样我就不会硬编码200个限制了。相反,我希望我的循环继续添加项目直到达到200。我已经看到了一些下面的示例,但是我不确定如何将其引入现有代码中

       static void Main()
        {

            bool 200 = true;
            if (200)
            {

                Console.WriteLine();
            }
            200 = 200;
            if (200)
            {
                Console.WriteLine(false);
            }
        }






    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class Mock1
    {
        static void Main()
        {

            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
            webDriver.Manage().Window.Maximize();
            webDriver.FindElement(By.XPath(".//input[@data-testid='search-input']")).SendKeys("nike trainers");
            webDriver.FindElement(By.XPath(".//button[@data-testid='search-button-inline']")).Click();

// above code goes onto asos.com and searches for nike trainers 

            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
            webDriver.FindElement(By.CssSelector("article img")).Click();

// code to select the size of the trainers and click
                IWebElement Size = webDriver.FindElement(By.XPath(".//select[@data-id='sizeSelect']"));
                SelectElementFromDropDown(Size, "UK 10 - EU 45 - US 11");

            webDriver.FindElement(By.XPath("//*[@data-bind='text: buttonText']")).Click();


            wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[text()='Added']")));
            webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();

// Adds items to the bag till 200 is reached 
                wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));

            string totalPrice = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;

            double pricePerItem = Convert.ToDouble(totalPrice.Substring(1));


            int priceLimit = 200;

            double noOfQuantity = priceLimit / pricePerItem;

            IWebElement qty = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));

            SelectElementFromDropDown(qty, Math.Floor(noOfQuantity).ToString());


            wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
            webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();

            // webDriver.Quit();

        }



        private static void SelectElementFromDropDown(IWebElement ele, string text)
        {
            SelectElement select = new SelectElement(ele);
            select.SelectByText(text);
        }

    }

}

0 个答案:

没有答案