-
Notifications
You must be signed in to change notification settings - Fork 11
Navigation
Headless supports GET and POST actions against a resource. Navigation happens either from an IBrowser instance (GoTo, GoTo<T>, PostTo, PostTo<T> - see browser navigation) or because of a simulated HTML elements event (button and link clicks - see HTML element navigation).
The two navigation models supported when navigating directly using an IBrowser instance are the page model and dynamic model.
The page model uses a custom class inheriting from IPage to represent the model of a web page. The page model class must define the default target location that IBrowser will request (unless a specific Uri is provided). A major advantage of navigating using the page model over the dynamic model is that the page model provides inbuilt validation that the final page returned is the one that was expected.
using (var browser = new Browser())
{
var page = browser.GoTo<MyPage>();
var nextPage = browser.PostTo<MyPage>(parameters);
}
The dynamic model in itself does not provide any point of navigation per sa. Unlike the page model, the dynamic page support has no idea about where the resource you require exists. As such, IBrowser requires calls that return a dynamic page be provided with a specific Uri to request. The advantage of this model is that a dynamic page is returned for which you do not require a page model to do HTML inspection.
using (var browser = new Browser())
{
var page = browser.GoTo(new Uri("https://google.com"));
var nextPage = browser.PostTo(parameters, new Uri("https://mysite.com"));
}
HTML element navigation occurs when an action is taken on an IHtmlElement instance that causes the associated IBrowser to make a GET or POST request. Examples of this are button clicks, link clicks and manual form submission.
HTML element navigation supports the page model. This means that a page model will be the result of the action. Location validation will also be executed as part of this process to validate that the intended page was actually returned.
using (var browser = new Browser())
{
var homePage = browser.GoTo<HomePage>();
var contactPage = homePage.Contact.Click<ContactPage>();
contactPage.Email.Value = "[email protected]";
contactPage.Name.Value = "My Name";
contactPage.Comment.Value = "Hi there!";
var thankYouPage = contactPage.Submit.Click<ThankYouPage>();
}
HTML element navigation supports the dynamic model. HTML element actions in this scenario will return a dynamic page from which dynamic HTML inspection and actions can be taken.
using (var browser = new Browser())
{
var homePage = browser.GoTo(new Uri("http://mysite"));
var contactPage = homePage.Contact.Click();
contactPage.Email.Value = "[email protected]";
contactPage.Name.Value = "My Name";
contactPage.Comment.Value = "Hi there!";
var thankYouPage = contactPage.Submit.Click();
}
The advantage of HTML element navigation is that the code can jump between page and dynamic models.
using (var browser = new Browser())
{
var homePage = browser.GoTo<HomePage>();
var contactPage = homePage.Contact.Click();
contactPage.Email.Value = "[email protected]";
contactPage.Name.Value = "My Name";
contactPage.Comment.Value = "Hi there!";
var thankYouPage = contactPage.Submit.Click<ThankYouPage>();
}