Search code examples
c#seleniumtestingwebdriver

How can I tell FindsBy to search elements on this particular sites


I've started learning Selenium Webdriver. It seems pretty easy but I'm not sure how to use PageObject Pattern. I understand the idea, I guess, but I don't know how to map many pages in one project. For example: the site contains many subsites (i.e. login page, create an account page) and I'd like to create PageObject for each of them. When I execute my script, I'm getting messages that elements haven't been found. What should I do? Should I create a separete drviers or what?


Solution

  • Seems that you are doing everything correct: create a new object for each page. Create different objects for common elements that are the same on a lot of pages (like footer, header menu, etc.).

    Then to combine page objects, first you need to start somewhere. For example from you main page. So use the MainPageObj to interact wit the main page. As soon as you navigate to another page you need to use a method in your MainPageObj class. This method must return a page object of the new page. So if you open a Login page from the main page then your method OpenLoginPage() should return LoginPageObj.

    Example: MainPageObj and LoginPageObj are page object classes.

    MainPageObj class has method:

    public LoginPageObj OpenLoginPage()
    {
        LoginButton.Click();
        return new LoginPageObj();
    }
    

    So use this method in this way:

    MainPageObj mainPage = new MainPageObj ();
    LoginPageObj loginPage = mainPage.OpenLoginPage();
    

    Simple?)

    If you want to make it more beautifyl then add static class Pages and add static methods for each page object you have to this class. Each method must return a new instance of your specific page object.

    Example:

    public static class RealSites
    {
        public static class Kinopoisk
        {
            public static Kinopoisk.MainPage MainPage
            {
                get { return new Kinopoisk.MainPage(); }
            }
        }
    }
    

    And in your tests use it this way:

    Pages.RealSites.Kinopoisk.MainPage.OpenMoviePage(url);
    Pages.RealSites.Kinopoisk.MoviePage.DoSomethigElseMethod();