使用页面对象模型(POM)C#编写硒测试

时间:2018-07-05 08:05:31

标签: c# selenium selenium-webdriver

我写了硒测试。该测试已被编写为一个脚本,但是我现在想介绍页面对象模型,以便我的值不会被硬编码。我的问题是我不确定我现有的代码如何适合POM。我已经开始了(见下文),但是我似乎有很多例外。有没有简单的方法可以做到这一点。

下面是我的工作代码。我的基本代码在asos.com上搜索一对运动鞋和大小,将其添加到袋子中,然后继续向袋子中添加数量,直到达到200的值。

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



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

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

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

    }

}

我现在想将以上内容更改为页面对象模型。

我创建了一个将调用方法的主类。

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

namespace Exercise1
{
    class TestClass
    {
        private IWebDriver driver;



        public void SetUp()
        {
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();

        }

        public void SearchItemFromPage()

        {
            HomePage home = new HomePage(driver);
            home.goToPage();

        }

        public void AddItemToBag()

        {


        }

        public void TearDown()

        {
            driver.Close();
        }

    }
}

然后我创建了3个将存储值的类。

    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.PageObjects;
using OpenQA.Selenium.Support.UI;

namespace Exercise1.PageObjects
{
    class PreferencePage
    {
        private IWebDriver driver;
        private WebDriverWait wait;

        public SearchItem (IWebDriver driver)
        {
            this.driver = driver;
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            PageFactory.InitElements(driver, this);

        }

        [FindsBy(How = How.XPath, Using = ".//input[@data-testid='search-input']")).SendKeys("nike trainers"));]
        private IWebElement searchText;


        public SearchItem ()
        {
            [FindsBy(How = How.XPath, Using = ".//button[@data-testid='search-button-inline']"))).Click());
            return new searchItem(driver);
        }






    }
}



   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise1.PageObjects
{
    class Item
    {

        public void ChooseItem()
        {
            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(10));
            IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("article img")));
            webDriver.FindElement(By.CssSelector("article img")).Click();

        }

            public void SelectSize()

            {

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

            }


    }
}


    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace Exercise1.PageObjects
{
    class Value
    {

        private IWebDriver driver;




        public void ViewBag()
        {


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

0 个答案:

没有答案