What's the best way to use Selenium PageObject Design Pattern

此生再无相见时 提交于 2019-11-28 15:54:46
Boler

Use PageFactory.InitElements(_driver, this); on the constructor of your base page class:

public class Page
{
    public IWebDriver _driver;

    public Page(IWebDriver driver)
    {
        this._driver = driver;
        PageFactory.InitElements(_driver, this);
    }
}

Please see the PageFactory documentation

I would avoid the Asserts in the tests and stick with the LoginPage.signIn method, which will throw an exception in case of unsuccessful login. I'm not familiar with NUnit but I guess it supports the 'expected to fail' behavior.

It's better to keep your page-dependent logic in one place(the page class).

I guess you'll have to modify the web UI tests a lot as the main app evolves anyway.

Create a Browser class to create driver and similar functions such as GoTo() for navigation and Teardown() for closing the browser.`

public class Browser
    {
        static IWebDriver webDriver = new FirefoxDriver();
        //static IWebDriver webDriver = new ChromeDriver();
        //InternetExplorerOptions  options = new InternetExplorerOptions(); 
        //static IWebDriver webDriver = new InternetExplorerDriver(@"C:\Program Files\Selenium\");
        public static void GoTo(string url)
        {
            //webDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));
            webDriver.Url = url;
        }
        public static ISearchContext Driver
        {
            get { return webDriver; }
        }
        public static void Teardown()
        {
            webDriver.Quit();
        }   
        public static void MaximizeWindow()
        {
            webDriver.Manage().Window.Maximize();
        }

Create individual classes for pages and use PageFactory to initailize the elements.

 public class Admin
    {
        public static AdminPage AdminPage
        {
            get
            {
                var adminpage = new AdminPage();
                PageFactory.InitElements(Browser.Driver, adminpage);
                return adminpage;
            }

        }
    }
    public class AdminPage
    {
        string Url = "http://172.18.12.225:4444/admin/admin.aspx";
        string Title = "Login";
        string Text = "Admin";
        public void GoTo()
        {
            Browser.GoTo(Url);
        }
        public bool IsAt()
        {
            return Browser.Title == Title;
        }
        public bool Is_At()
        {
            return Browser.Title == Text;
        }
        [FindsBy(How = How.Id, Using = "ctl16_lblUdpSageMesageCustom")]
        public IWebElement UpdateMessage { get; set; }

        [FindsBy(How = How.Id, Using = "hypPreview")]
        public IWebElement BackHomeLink { get; set; }
        //Login
       // [FindsBy(How = How.Id, Using = "ctl14_UserName")]
       // public IWebElement UserNameLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_Password")][CacheLookup]
        public IWebElement PasswordLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_LoginButton")][CacheLookup]
        public IWebElement LoginLink { get; set; }
        //Forgot Password
        [FindsBy(How = How.Id, Using = "ctl14_hypForgotPassword")][CacheLookup]
        public IWebElement FPWLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_wzdForgotPassword_txtUsername")][CacheLookup]
        public IWebElement FPWUserNameLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_wzdForgotPassword_CaptchaValue")][CacheLookup]
        public IWebElement FPWCaptchaLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_wzdForgotPassword_StartNavigationTemplateContainerID_StartNextButton")][CacheLookup]
        public IWebElement FPWNextLink { get; set; }
        [FindsBy(How = How.Id, Using = "ctl14_wzdForgotPassword_StartNavigationTemplateContainerID_CancelButton")][CacheLookup]
        public IWebElement FPWCancelLink { get; set; }
        [FindsBy(How = How.Id, Using = "sfToppane")][CacheLookup]
        public IWebElement TopPane { get; set; }
        [FindsBy(How = How.Id, Using = "sidebar")][CacheLookup]
        public IWebElement sidebar { get; set; }
        //Role
        //[FindsBy(How = How.Id, Using = "ctl19_rptDashBoard_ctl01_hypPageURL")]
        //public IWebElement Role { get; set; }       
        //User
        //[FindsBy(How = How.Id, Using = "ctl19_rptDashBoard_ctl02_hypPageURL")]
        //public IWebElement User { get; set; } 
        public void LogIn(string Username, string Password)
        {
            Browser.MaximizeWindow();
            IWebElement UserNameLink = Browser.WaitForElement(By.Id("ctl14_UserName"), 15);
            UserNameLink.Click();
            UserNameLink.Clear();
            UserNameLink.SendKeys(Username);
            PasswordLink.Click();
            PasswordLink.Clear();
            PasswordLink.SendKeys(Password);
            LoginLink.Click();
        }
}

It is a small example with a login functionality. I hope this might help even though a late reply.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!