Is a companion library for Selenium C# that provides functionality to easily populate forms, navigate pages and construct PageObjects.
vardriver=newFirefoxDriver();varfixture=newFixture(driver);fixture.Navigate.To("http://ipjohnson.github.io/SeleniumFixture/TestSite/InputForm.html");fixture.AutoFill("//form");Provides short hand for clicking and double clicking elements
// click button with id someButtonfixture.Click("#someButton");// click button of type submitfixture.Click("button[type='submit']");// DoubleClick any button that has the html class 'some-class'fixture.DoubleClick("button.some-class",ClickMode.Any);Fill allows you to easily populate form elements with a given set of values. Currently textboxs, radiobutton, checkbox, select and textarea are supported.
// Navigate to page and fill out formfixture.Navigate.To("http://ipjohnson.github.io/SeleniumFixture/TestSite/InputForm.html");fixture.Fill("//form").With(new{FirstName="Sterling",LastName="Archer",Active=true,Gender="Male"}).ThenSubmit();// fill all elements with the html class some-class with the value 1111 fixture.Fill(".some-class").With(1111);Sometimes its useful to populate a form with random data. The AutoFill method can be used to fill a html input elements.
// AutoFill form elementfixture.AutoFill("//form");// AutoFill form but use Bob to fill the FirstName fieldfixture.AutoFill("//form",seedWith:new{FirstName="Bob"});// Find an element with the id #someDiv and populate all child input and select elementsfixture.AutoFill("#someDiv");It's useful to wait for certain things to happen on page, the Wait API provides a number of helpful wait methods for things like ajax or form elements to exists.
// wait for ajax calls to finishfixture.Wait.ForAjax();// wait for #someElement to be present on the pagefixture.Wait.ForElement("#someElement");// long hand for waiting for element 20 secondsfixture.Wait.Until(i =>i.CheckForElement("#someElement"),20);The PageObject pattern is very useful and SeleniumFixture fully supports using PageObject classes to help package logic. SimpleFixture is used to create instances of PageObjects allowing for very complex object. For example, injecting constructor parameters , properties (using ImportAttribute), and etc.
vardriver=newFirefoxDriver();varfixture=newFixture(driver,"http://ipjohnson.github.io/SeleniumFixture/TestSite/");varformPage=fixture.Navigate.To<FormPage>("InputForm.html");formPage.FillOutForm();publicclassFormPage{publicvoidFillOutForm(){I.Fill("//form").With(new{FirstName="Sterling",LastName="Archer"});}protectedIActionProviderI{get;privateset;}}The IActionProvider allows a page object to import the functionality of the Fixture into a local property. When PageObjects are constructed any IActionProvider property with a setter will be Populated with an instance of IActionProvider. Usually this property is named I
I.Fill("//form").With(new{FirstName="Sterling"});I.Click("#submitButton").Wait.ForAjax().Then.Yields<HomePage>();PageObjects can validate themselves upon creation one of two ways. You can either have a method name "Validate" or a property called "Validate" that is an Action. Either choice will be called once the page object has been created.
publicclassHomePage{publicHomePage(){Validate=()=>I.Get.PageTitle.Should().Be("Home");}protectedActionValidate{get;privateset;}protectedIActionProviderI{get;privateset;}}Yields is how you can create new PageObject instances. It takes the type of page as the generic parameter and will instantiate a new instance of T. Note: your page types do not have to inherit from any particular type, you could use structs if you felt so inclined.
// click the submit button and yield a new HomePage object I.Click("#submitButton").Yields<HomePage>();// click the link Some Text then yield a new OtherPage object// and pass the value 5 into the constructor parameter someParamI.Click(By.LinkText("Some Text")).Yields<OtherPage>(constraints:new{someParam=5});publicclassOtherPage{publicOtherPage(intsomeParam){Validate=()=>I.Get.PageTitle.Should().EndWith(someParam.ToString());}privateActionValidate{get;set;}privateIActionProviderI{get;set;}}Note: Should() is part of Fluent Assertions.
xUnit 2.0 is a very extensible testing framework that SeleniumFixture.xUnit provides support for out of the box. Currently there are a set of attributes that make setup and tear down of IWebDrivers and Fixture easier. You can customize initialization and finalization of WebDriver & Fixtures. It also supports creation of PageObjects as well as xUnit DataAttributes
publicclassAutoFillTests{/// <summary>/// Test navigates to the input form and auto fills the form./// Runs against Chrome, Firefox and Internet Explorer/// </summary>/// <param name="fixture">populated fixture</param>[SeleniumTheory][ChromeDriver][FirefoxDriver]publicvoidFixture_FillForm_PopulatesCorrectly(Fixturefixture){fixture.Navigate.To("http://ipjohnson.github.io/SeleniumFixture/TestSite/InputForm.html");fixture.AutoFill("//form");fixture.Get.Value.From("#FirstName").All(char.IsLetter).Should().BeTrue();fixture.Get.Value.From("#LastName").All(char.IsLetter).Should().BeTrue();}/// <summary>/// Test case that uses attribute to navigate to the input form/// then autofills the page then tests that the first and last names are all letters/// </summary>/// <param name="inputPage">Page object returned from InitializeToInputForm</param>[SeleniumTheory][ChromeDriver][FirefoxDriver][InitializeToInputForm]publicvoidFixture_Initialize_ToInputForm(InputPageinputPage){inputPage.AutoFill();inputPage.Get.Value.From("#FirstName").All(char.IsLetter).Should().BeTrue();inputPage.Get.Value.From("#LastName").All(char.IsLetter).Should().BeTrue();}/// <summary>/// Test that page object is created correctly and that InlineData is correctly populated into variables/// </summary>/// <param name="inputPage">page object from initialization attribute</param>/// <param name="helloString">Hello string</param>/// <param name="intValue">5 value</param>[SeleniumTheory][ChromeDriver][FirefoxDriver][InlineData("Hello",5)][InitializeToInputForm]publicvoidFixture_InlineData_CorrectDataPopulated(InputPageinputPage,stringhelloString,intintValue){helloString.Should().Be("Hello");intValue.Should().Be(5);inputPage.AutoFill();inputPage.Get.Value.From("#FirstName").All(char.IsLetter).Should().BeTrue();inputPage.Get.Value.From("#LastName").All(char.IsLetter).Should().BeTrue();}}Note: because each driver has slightly different behavior your tests and PageObjects need to be a little bit more robust.