-
Notifications
You must be signed in to change notification settings - Fork 2
How to
The base for a testing scenario is to work against a Session. First off, you choose authentication type (if needed by the type you also provide credentials) and then it's time to access a resource.
The way to access resources (by a url) is thought of in a restful matter, where there is some resource which can be either fetched by a GET and manipulated by methods like PUT or DELETE or created using a POST.
The result of the access will redeem in a NavigationResult which contain some information about the request (like how long the request took in milliseconds), raw HTML from the response along with some helpers to convert JSON to any object you like or find any element in a html response (either by a css- or xpath-selector)
##Sample code A sample in nUnit:
[Test]
public void Get_URL_ReturnSOMEDATA()
{
var session = new Session(USERNAME, PASSWORD);
var result = session.Get(URL);
Assert.That(result.ResponseContent, Is.StringContaining(SOMEDATA));
}
[Test]
public void Get_URL_ReturnSOMETHINGAsJson()
{
var session = new Session();
var result = session.Get(URL).FromJsonTo<dynamic>();
Assert.That(result.SOMETHING, Is.EqualTo(SOMETHING));
}
[Test]
public void Get_URL_ReturnTwoBeers()
{
var session = new Session(USERNAME, PASSWORD);
var elements = session.Get(URL).Find("li.beer");
Assert.That(elements.Count(), Is.EqualTo(2));
}
[Test]
public void Get_URL_ReturnElementWithText()
{
var session = new Session(USERNAME, PASSWORD);
var elements = session.Get(URL).Find("#myelement");
//elements.Text extracts the innerHtml of the first element
Assert.That(elements.Text, Is.StringContaining("carlsberg"));
}
[Test]
public void Get_URL_ReturnElement()
{
var session = new Session(USERNAME, PASSWORD);
var elements = session.Get(URL).Find("#myelement");
//elements.Raw extracts the outerHtml of the first element
Assert.That(elements.Raw, Is.StringContaining("<em id='myelement'>jippi</em>"));
}
[Test]
public void Follow_LinkFromPage_ReturnText()
{
var session = new Session(USERNAME, PASSWORD);
//Navigate to URL, then find #mylink and look at href-attribute
//and do another get and find the heading on that page
var elements = session.Get(URL).Follow("#mylink").Find("h1");
Assert.That(elements.Text, Is.StringContaining("Some heading"));
}