为selenium pagefactory设置BasePage

时间:2017-12-13 12:37:20

标签: c# selenium selenium-webdriver selenium-chromedriver page-factory

我已经设置了我网站的前两页,两个页面共享页面顶部的实用程序导航栏。正如你在两页中看到的那样,我不得不复制用于查找的代码并单击这些元素。我已经读过你可以将常用元素移动到基页,然后让其他页面共享该基页中的代码。

我的问题是,如何将重复的代码移动到新文件,但仍然能够访问其他页面中的所有元素。

DashboardPage:

 public class DashboardPage
    {
        private IWebDriver driver;

        #region WebElement

        [FindsBy(How = How.Id, Using = "AgentPAS")]
        private IWebElement Policy;

        [FindsBy(How = How.Id, Using = "Billing")]
        private IWebElement Billing;

        [FindsBy(How = How.LinkText, Using = "Activity")]
        private IWebElement Activity;

        [FindsBy(How = How.LinkText, Using = "Premium")]
        private IWebElement Premium;

        [FindsBy(How = How.LinkText, Using = "Production Summary")]
        private IWebElement ProductionSummary;

        [FindsBy(How = How.LinkText, Using = "Quote to Bind")]
        private IWebElement QuoteToBind;

        #endregion

        public DashboardPage()
        {
            PageFactory.InitElements(ObjectRepository.Driver, this);
        }

        #region Actions

        public void PolicyButton()
        {
            Policy.Click();
        }

        public void BillingButton()
        {
            Billing.Click();
        }

        public void ActivityLink()
        {
            Activity.Click();
        }

        public void PremiumLink()
        {
            Premium.Click();
        }

        public void ProductionSummaryLink()
        {
            ProductionSummary.Click();
        }

        public void QuoteToBindLink()
        {
            QuoteToBind.Click();
        }

        #endregion

    }

ClientDetailsOnePage:

public class ClientDetailsOnePage
    {
        private IWebElement driver;

        #region WebElement

        [FindsBy(How = How.Id, Using = "AgentPAS")]
        private IWebElement Policy;

        [FindsBy(How = How.Id, Using = "Billing")]
        private IWebElement Billing;

        [FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Title']")]
        private IWebElement Title;

        [FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.FirstName']")]
        private IWebElement FirstName;

        [FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Name']")]
        private IWebElement LastName;

        [FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.DesignatedAuthority']")]
        private IWebElement DesignatedAuthority;

        [FindsBy(How = How.XPath, Using = "//span[@data-ref='displayEl']")]
        private IWebElement UKResident;

        [FindsBy(How = How.LinkText, Using = "Save & Exit")]
        private IWebElement SaveExit;

        [FindsBy(How = How.LinkText, Using = "Next")]
        private IWebElement Next;

        #endregion

        public ClientDetailsOnePage()
        {
            PageFactory.InitElements(ObjectRepository.Driver, this);
        }

        #region Actions

        public void PolicyButton()
        {
            Policy.Click();
        }

        public void BillingButton()
        {
            Billing.Click();
        }

        public void TitleDropDown(string text)
        {
            Title.SendKeys(text);
        }

        public void FirstNameTextBox(string text)
        {
            FirstName.SendKeys(text);
        }

        public void LastNameTextBox(string text)
        {
            LastName.SendKeys(text);
        }

        public void DesignatedAuthorityDropDown(string text)
        {
            DesignatedAuthority.SendKeys(text);
        }

        public void UKResidentCheckBox()
        {
            UKResident.Click();
        }

        public void SaveExitButton()
        {
            SaveExit.Click();
        }

        public void NextButton()
        {
            Next.Click();
        }

        #endregion
    }

1 个答案:

答案 0 :(得分:2)

两个页面都有一个导航栏,因此导航栏是页面的一部分。您可以为导航栏创建单独的类,并在您喜欢的每个页面上重复使用它。

这称为作文

快速举例:

foreach ($comments as $comment) {
    dump($comment->user); // $comment is an object
}