C# 如何在selenium测试c中实现while循环#

C# 如何在selenium测试c中实现while循环#,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我写了一段代码,在网站上搜索物品,然后选择物品的大小并将其添加到包中 一旦物品添加到袋子中,我想引入一个while循环,使物品的数量不断增加,直到它等于或超过200英镑。我知道while循环是最好的方法,因为我不知道我想做多少个循环 我相信循环应该在一件物品添加到包中后引入,因为只有在这一点上我才能说出物品的数量。在我的循环中,如何让我的代码在每次递增+1时验证商品数量的价格 using System; using System.Collections.Generic; using Sy

我写了一段代码,在网站上搜索物品,然后选择物品的大小并将其添加到包中

一旦物品添加到袋子中,我想引入一个while循环,使物品的数量不断增加,直到它等于或超过200英镑。我知道while循环是最好的方法,因为我不知道我想做多少个循环

我相信循环应该在一件物品添加到包中后引入,因为只有在这一点上我才能说出物品的数量。在我的循环中,如何让我的代码在每次递增+1时验证商品数量的价格

    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 Exercise3
    {

        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();

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


            webDriver.FindElement(By.CssSelector("article img")).Click();


            IWebElement Size = webDriver.FindElement(By.XPath(".//select[@data-id='sizeSelect']"));
            SelectElementFromDropDown(Size, "UK 10.5 - EU 45.5 - US 11.5");


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

            webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();


    // I believe the while loop should be implemented here
            int number = 200;
            while (number > 200)

            webDriver.Quit();
        }

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




    }

}

下面的代码可以添加到您评论的地方。
//我认为while循环应该在这里实现

String price = webDriver.FindElement(By.XPath("//span[@class='bag-item-price bag-item-price--current']"))
            .getAttribute("innerHTML");
int pricePerUnit = (int) Double.Parse(price.substring(1, price.Length - 1));
int qty = 1;
while (pricePerUnit * qty <= 200) {
    qty++;
}
qty--;
IWebElement quantityDropDown = webDriver.FindElement(
        By.XPath("//select[@class='bag-item-quantity bag-item-selector select2-hidden-accessible']"));
SelectElementFromDropDown(quantityDropDown, qty.ToString());
String price=webDriver.FindElement(By.XPath(“//span[@class='bag-item-price-bag-item-price-current']))
.getAttribute(“innerHTML”);
int pricePerUnit=(int)Double.Parse(price.substring(1,price.Length-1));
整数数量=1;

while(pricePerUnit*qty您可以在不使用循环(for/while循环)的情况下实现场景。我添加了以下逻辑。某些元素渲染需要更多时间。因此,我建议在需要时添加显式条件

请查找更新的代码并参考所有注释以了解更多详细信息

主要方法:

            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();

            //Search Result rendering will take some times.So, Explicit wait is mandatory
            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
            IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
            webDriver.FindElement(By.CssSelector("article img")).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();

            //Add to cart takes some time.So, the below condition is needed 
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[text()='Added']")));

            webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();
            //Wait condition is needed after the page load
            //wait.Until(ExpectedConditions.TitleContains("Shopping"));

            wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
            //Extract the price from the cart
            string totalPrice =webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
            //Extract the price amount by exluding the currency
            double pricePerItem = Convert.ToDouble(totalPrice.Substring(1));

            // Just hardcoded the expected price limit value
            int priceLimit = 200;

            double noOfQuantity = priceLimit / pricePerItem;

            IWebElement qty =webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
            //Quantity values are rounded off with nearest lowest value . Example, 5.55 will be considered as 5 quantity
            SelectElementFromDropDown(qty, Math.Floor(noOfQuantity).ToString());

            //After updating the quantity, update  button will be displayed dynamically.So, wait is added and then update action is performed
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
            webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();
使用循环的替代方法:

            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();

            //Search Result rendering will take some times.So, Explicit wait is mandatory
            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
            IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
            webDriver.FindElement(By.CssSelector("article img")).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();

            //Add to cart takes some time.So, the below condition is needed 
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[text()='Added']")));

            webDriver.FindElement(By.XPath("//a[@data-testid='bagIcon']")).Click();
            //Wait condition is needed after the page load
            //wait.Until(ExpectedConditions.TitleContains("Shopping"));

            wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
            //Extract the price from the cart
            string totalPrice =webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
            //Extract the price amount by exluding the currency
            double pricePerItem = Convert.ToDouble(totalPrice.Substring(1));

            // Just hardcoded the expected price limit value
            int priceLimit = 200;

            double noOfQuantity = priceLimit / pricePerItem;

            IWebElement qty =webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
            //Quantity values are rounded off with nearest lowest value . Example, 5.55 will be considered as 5 quantity
            SelectElementFromDropDown(qty, Math.Floor(noOfQuantity).ToString());

            //After updating the quantity, update  button will be displayed dynamically.So, wait is added and then update action is performed
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
            webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();
我强烈建议使用上述方法。因为,每次都会逐个增加数量,直到价格限制超过200美元

        //Extract the price from the cart
        string totalPrice = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;
        double currentTotalPrice = Convert.ToDouble(totalPrice.Substring(1));
        double itemPerPrice = currentTotalPrice;

        // Just hardcoded the expected price limit value
        int priceLimit = 200;
        int quantity = 1;

        while(currentTotalPrice < priceLimit && priceLimit-currentTotalPrice > itemPerPrice)
        {
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//select[contains(@class,'bag-item-quantity')]")));
            IWebElement qty = webDriver.FindElement(By.XPath("//select[contains(@class,'bag-item-quantity')]"));
            //Quantity values are rounded off with nearest lowest value . Example, 5.55 will be considered as 5 quantity
            SelectElementFromDropDown(qty, (++quantity).ToString());
            //After updating the quantity, update  button will be displayed dynamically.So, wait is added and then update action is performed
            wait.Until(ExpectedConditions.ElementExists(By.XPath("//button[@class='bag-item-edit-update']")));
            webDriver.FindElement(By.XPath("//button[@class='bag-item-edit-update']")).Click();

            wait.Until(ExpectedConditions.ElementExists(By.XPath("//span[@class='bag-subtotal-price']")));
            var temp = webDriver.FindElement(By.XPath("//span[@class='bag-subtotal-price']")).Text;

            currentTotalPrice = Convert.ToDouble(temp.Substring(1));
            Console.WriteLine("Current Price :" + currentTotalPrice);
        }
//从购物车中提取价格
字符串totalPrice=webDriver.findelelement(By.XPath(“//span[@class='bag-subtotal-price']))。文本;
double currentTotalPrice=Convert.ToDouble(totalPrice.Substring(1));
double itemPerPrice=当前总价;
//只是硬编码了预期的价格限制值
整数价格限制=200;
整数数量=1;
而(currentTotalPriceitemPerPrice)
{
wait.Until(ExpectedConditions.ElementExists(By.XPath(//select[contains(@class,'bag-item-quantity'))))));
IWebElement qty=webDriver.FindElement(By.XPath(//select[contains(@class,'bag-item-quantity')]);
//数量值用最接近的最小值四舍五入。例如,5.55将被视为5个数量
从下拉列表中选择Element(数量,(+数量).ToString());
//更新数量后,将动态显示更新按钮。因此,添加等待,然后执行更新操作
wait.Until(ExpectedConditions.ElementExists(By.XPath(//button[@class='bag-item-edit-update']));
webDriver.FindElement(按.XPath(//按钮[@class='bag-item-edit-update'])。单击();
wait.Until(ExpectedConditions.ElementExists(By.XPath(“//span[@class='bag-subtotal-price']));
var temp=webDriver.findelelement(By.XPath(“//span[@class='bag-subtotal-price']);
currentTotalPrice=Convert.ToDouble(临时子字符串(1));
Console.WriteLine(“当前价格:+currentTotalPrice”);
}

我知道while循环是最好的方法,因为我不知道我想做多少次循环。
何时停止?一旦金额等于或超过200英镑,它就会停止。你知道如何获取购物车的价值吗?在这种情况下,你可以做
while((获取购物车总数)<200)
否,我如何在每次添加数量时验证袋子的价值?读取网站上包含总价的标签的价值,并将其转换为整数(因为它可能是字符串)该值将存储在何处?如何从UI获取该值?对于price.Length和Integer.toString(qty)),我有两个例外;如何更改我的方法,使其能够处理span,因为这是我获得的下一个异常。能否告诉您获取异常的行我尝试运行代码IWebElement Qty=webDriver.findelelement(By.XPath(//span[@class='bag-item-price bag-item price--current']);从下拉列表中选择元素(数量,“2”);只是看看下拉列表是否正常工作。我在selectElementFromDropDown方法中遇到异常。元素应为select,但为spanQty应为select类型而非span类型的WebElement我如何更改价格限制,使其仅增加到等于或超过200英镑。您是否可以尝试使用上述代码并与我共享日志(如果有)。我已经测试了上述代码,它对我来说运行良好。@J.dockster:在这里,我已经硬编码了priceLimit值为200,并且我正在动态计算数量为double noOfQuantity=priceLimit/pricePerItem;(例如,如果单个数量价格为30英镑,预期价格限制为200英镑,则数量编号将=200/30=6.66。四舍五入后,值6将被视为数量编号),最后,我们可以根据上述数量编号直接选择数量值。确定,但是,如果我想继续添加,直到达到200英镑以上,这可能吗?我可以做一个布尔运算,使int priceLimit>200吗?