{"_id":"54c520202418480d0028a2d4","user":"54c4b05742190d0d00f5fbde","version":{"_id":"54c51c5e2418480d0028a2c3","__v":4,"project":"54c51c5e2418480d0028a2c0","createdAt":"2015-01-25T16:39:58.382Z","releaseDate":"2015-01-25T16:39:58.382Z","categories":["54c51c5e2418480d0028a2c4","54c51da3796aed0d009fc80f","54c51db22418480d0028a2c9","54c51dbd1613c70d00eeacbe"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":3,"project":"54c51c5e2418480d0028a2c0","category":{"_id":"54c51db22418480d0028a2c9","version":"54c51c5e2418480d0028a2c3","__v":3,"pages":["54c520202418480d0028a2d4","54c5219a2418480d0028a2d8","54c524dcd4928c0d00d98e53"],"project":"54c51c5e2418480d0028a2c0","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-01-25T16:45:38.112Z","from_sync":false,"order":2,"slug":"page-objects","title":"Page Objects"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-01-25T16:56:00.160Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"Seleno supports the idea of [Page Objects](https://code.google.com/p/selenium/wiki/PageObjects) to model your web pages. The idea is to model each page (or part of a page) as a class, and then to use instances of those classes in your tests. To continue, with the google example, we're all familiar with this page.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/PgvuuKxzTzKvRmwZgfKh_google.png\",\n        \"google.png\",\n        \"597\",\n        \"219\",\n        \"#2353bd\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\nIn Seleno, you would model this page by inheriting from the Page class and then encapsulating the interaction with the page within methods. Seleno provides methods to find elements on the page (Find) and enter data into the text box (SendKeys). The Navigate method allows you to navigate to the results page by clicking on the Search button (btnG).\n\n    public class SearchPage : Page\n    {\n        public SearchPage InputSearchTerm(string term)\n        {\n            Find.Element(By.Name(\"q\"))\n                .SendKeys(term);\n            return this;\n        }\n\n        public ResultsPage Search()\n        {\n            return Navigate.To<ResultsPage>(By.Name(\"btnG\"));\n        }\n    }\n\n    public class ResultsPage : Page\n    {\n        ...\n    }\n\nNow your tests can just call methods on the Page Objects, without having to know anything about the interaction with the page or the use of Selenium.\n\n    public static class Host\n    {\n        public static readonly SelenoHost Instance = new SelenoHost();\n\n        static Host()\n        {\n            Instance.Run(configure => configure\n                .WithWebServer(new InternetWebServer(\"http://www.google.co.uk\")));\n        }\n    }\n\n    [TestFixture]\n    public class GoogleSearchTests\n    {\n        [Test]\n        public void should_be_able_to_search()\n        {\n            var searchPage = Host.Instance.NavigateToInitialPage<SearchPage>();\n            var resultsPage = searchPage\n                .InputSearchTerm(\"BDDfy\")\n                .Search();\n            resultsPage.Title.Should().Be(\"Google\");\n        }\n    }\n    \n## Strongly Typed Page Objects ##\nThat's quite nice, but things start to get really interesting if you use view models. Seleno also provides strongly typed Page Objects, which allow you to read from and write to the page. Here is an example, from the Seleno MvcMusicStore sample. Because Seleno is aware of the RegisterModel view model, it is able to take a populated instance of that model and enter the information into the web page using the Input().Model method.\n\n    public class RegisterModel\n    {\n        public string UserName { get; set; }\n        public string Email { get; set; }\n        public string Password { get; set; }\n        public string ConfirmPassword { get; set; }\n    }\n\n    public class RegisterPage : Page<RegisterModel>\n    {\n        public HomePage CreateValidUser(RegisterModel model)\n        {\n            Input.Model(model);\n            return Navigate.To<HomePage>(By.CssSelector(\"input[type='submit']\"));\n        }\n    }\n\nThis can then be used in a test, with a populated model generated by the CreateRegisterModel of the [ObjectMother](http://martinfowler.com/bliki/ObjectMother.html).\n\n    public class StronglyTypedPageObjectWithComponent\n    {\n        [Test]\n        public void Can_buy_an_Album_when_registered()\n        {\n            var orderedPage = Host.Instance.NavigateToInitialPage<HomeController, HomePage>(x => x.Index())\n                .Menu\n                .GoToAdminForAnonymousUser()\n                .GoToRegisterPage()\n                .CreateValidUser(ObjectMother.CreateRegisterModel())\n                .GenreMenu\n                .SelectGenreByName(\"Disco\")\n                .SelectAlbumByName(\"Le Freak\")\n                .AddAlbumToCart()\n                .Checkout()\n                .SubmitShippingInfo(ObjectMother.CreateShippingInfo(), \"Free\");\n\n            orderedPage.Title.Should().Be(\"Checkout Complete\");\n        }\n    }\n\nNotice here the `Host.Instance.NavigateToInitialPage<HomeController, HomePage>(x => x.Index())` allows us to specify navigation by selecting a controller and action (rather than a Url string). Before using this method be sure to register the application routes when you are initializing Seleno, e.g.:\n\n    <AnInstanceOfSelenoHost>\n        .Run(\"MvcMusicStore\", 12345, \n            c => c.WithRouteConfig(RouteConfig.RegisterRoutes));","excerpt":"","slug":"what-are-page-objects","type":"basic","title":"What are Page Objects?"}

What are Page Objects?


Seleno supports the idea of [Page Objects](https://code.google.com/p/selenium/wiki/PageObjects) to model your web pages. The idea is to model each page (or part of a page) as a class, and then to use instances of those classes in your tests. To continue, with the google example, we're all familiar with this page. [block:image] { "images": [ { "image": [ "https://files.readme.io/PgvuuKxzTzKvRmwZgfKh_google.png", "google.png", "597", "219", "#2353bd", "" ] } ] } [/block] In Seleno, you would model this page by inheriting from the Page class and then encapsulating the interaction with the page within methods. Seleno provides methods to find elements on the page (Find) and enter data into the text box (SendKeys). The Navigate method allows you to navigate to the results page by clicking on the Search button (btnG). public class SearchPage : Page { public SearchPage InputSearchTerm(string term) { Find.Element(By.Name("q")) .SendKeys(term); return this; } public ResultsPage Search() { return Navigate.To<ResultsPage>(By.Name("btnG")); } } public class ResultsPage : Page { ... } Now your tests can just call methods on the Page Objects, without having to know anything about the interaction with the page or the use of Selenium. public static class Host { public static readonly SelenoHost Instance = new SelenoHost(); static Host() { Instance.Run(configure => configure .WithWebServer(new InternetWebServer("http://www.google.co.uk"))); } } [TestFixture] public class GoogleSearchTests { [Test] public void should_be_able_to_search() { var searchPage = Host.Instance.NavigateToInitialPage<SearchPage>(); var resultsPage = searchPage .InputSearchTerm("BDDfy") .Search(); resultsPage.Title.Should().Be("Google"); } } ## Strongly Typed Page Objects ## That's quite nice, but things start to get really interesting if you use view models. Seleno also provides strongly typed Page Objects, which allow you to read from and write to the page. Here is an example, from the Seleno MvcMusicStore sample. Because Seleno is aware of the RegisterModel view model, it is able to take a populated instance of that model and enter the information into the web page using the Input().Model method. public class RegisterModel { public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public string ConfirmPassword { get; set; } } public class RegisterPage : Page<RegisterModel> { public HomePage CreateValidUser(RegisterModel model) { Input.Model(model); return Navigate.To<HomePage>(By.CssSelector("input[type='submit']")); } } This can then be used in a test, with a populated model generated by the CreateRegisterModel of the [ObjectMother](http://martinfowler.com/bliki/ObjectMother.html). public class StronglyTypedPageObjectWithComponent { [Test] public void Can_buy_an_Album_when_registered() { var orderedPage = Host.Instance.NavigateToInitialPage<HomeController, HomePage>(x => x.Index()) .Menu .GoToAdminForAnonymousUser() .GoToRegisterPage() .CreateValidUser(ObjectMother.CreateRegisterModel()) .GenreMenu .SelectGenreByName("Disco") .SelectAlbumByName("Le Freak") .AddAlbumToCart() .Checkout() .SubmitShippingInfo(ObjectMother.CreateShippingInfo(), "Free"); orderedPage.Title.Should().Be("Checkout Complete"); } } Notice here the `Host.Instance.NavigateToInitialPage<HomeController, HomePage>(x => x.Index())` allows us to specify navigation by selecting a controller and action (rather than a Url string). Before using this method be sure to register the application routes when you are initializing Seleno, e.g.: <AnInstanceOfSelenoHost> .Run("MvcMusicStore", 12345, c => c.WithRouteConfig(RouteConfig.RegisterRoutes));