Selenium and Page Objects

	// Search Page
	public class BingSearch
    {
        private IWebElement _searchTerm;
        private List<BingSearchResult> _searchResults;

        public BingSearch()
        {
            Browser.GoTo("http://www.bing.com");
        }

        public string SearchTerm
        {
            get
            {
                return _searchTerm.Text;
            }
            set
            {
                _searchTerm = _searchTerm ?? Browser.WebDriver.FindElement(By.Name("q"));
                _searchTerm.SendKeys(value);
            }
        }

        public void Search()
        {
            if (_searchTerm != null)
            {
                if (_searchResults != null)
                {
                    _searchResults.Clear();
                }

                _searchTerm.Submit();
            }
        }

        public List<BingSearchResult> SearchResults
        {
            get
            {
                _searchResults = _searchResults ?? new List<BingSearchResult>();

                if (!_searchResults.Any())
                {
                    var elements = Browser.WebDriver.FindElements(By.CssSelector(".b_algo a"));

                    foreach (var element in elements)
                    {
                        _searchResults.Add(new BingSearchResult()
                        {
                            DisplayText = element.Text,
                            Link = element.GetAttribute("href")

                        });
                    }
                }

                return _searchResults;
            }

        }
    }
	// Search Result
  	public class BingSearchResult
    {
        public string DisplayText { get; set; }
        public string Link { get; set; }
    }
	// Browser abstraction 
	public static class Browser
    {
        private static IWebDriver _driver;
        static Browser()
        {
            _driver = new FirefoxDriver();
            _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        }
        public static void GoTo(string url)
        {
            _driver.Navigate().GoToUrl(url);
        }

        public static IWebDriver WebDriver { get { return _driver; } }

        public static void Close()
        {
            _driver.Close();
        }
    }

If you look at the BingSearch class it doesn’t expose any Selenium objects but it has a list for SearchResults which uses BingSearchResult as its generic type. The BingSearchResult represents only one result in the page. Because I don’t want to page through the search result I didn’t bother creating any type to support it.

Browser is represented using static class in this sample but it doesn’t have to be static class.

comments powered by Disqus