Wiki
Clone wikiWebAutomation / Home
WebAutomation
WebAutomation library provides an easy to use layer for writing Selenium tests using the Page Object Pattern. Based on XML, you can automatically generate all necessary C# classes, which will represent the tested websites
License
WebAutomation is Open Source software and is released under the MIT license
Installation
To install WebAutomation, run the following command in the Package Manager Console
PM> Install-Package WebAutomation
How to start
To start using WebAutomation library:
- create XML + T4 text template (see next paragraph),
- create new instance of AutomationDriver
AutomationDriverFactory.Get()
.
To learn more, you can also download our repository with samples WebAutomation-Samples.
Create XML file
XML file is used to map all important objects from your tested website. It consists of two types of nodes:
container
- represents a page or its partcomponent
- represents a single element of a page, for example button, textbox, etc.
Inside containers, you can place other sub containers or web components. In this way, you can create a hierarchical structure using inheritance (like in C#). To learn how to create correct XML, download project HowTo.XML from WebAutomation-Samples repository.
Following example illustrates how you can represent your website with the XML file.
In the XML, you can use several types of attributes. Some of them are mandatory (name
), and others are use to locate web components (id
, xpath
). Besides predefined attributes, you can also create new custom attributes. Those attributes can be then used to execute some extra activities during loading a container / performing an action of component (see links below).
Generate C# classes based on XML
Based on the previously created XML file, you can automatically generate C# classes for your test. To do this, create a .tt file (T4 text template) with the following content- see code below. After that, right click on the .tt file (in Solution Explorer) and select "Run custom tool'. Whenever you make a change in the XML file, repeat previous step to regenerate the C# classes.
<#@ template debug="true" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ assembly name="$(ProjectDir)WebAutomation.Generator.dll" #> <#@ import namespace="WebAutomation.Generator" #> <# // Update following variables // * Name of XML file describing the tested websites (placed in the same folder) // * The namespace in which the generated C# classes should be placed // * [Optional] The namespace which should be included in case of using custom attributes. // ====================================================== string xmlFilename = "SampleWebsite.xml"; string desiredNamespace = "YourNamespace"; string additionalUsing = string.Empty; // ====================================================== #> <#=WebObjectsGenerator.Generate(Host.ResolvePath(xmlFilename), desiredNamespace, additionalUsing)#>
Create a test
After generating C# classes, you can access them by using Get<T>
method in AutomationDriver
class.
var atDriver = AutomationDriverFactory.Get(); atDriver.WebDriver = new FirefoxDriver(); atDriver.WebDriver.Navigate().GoToUrl("http://localhost/"); var homePage = atDriver.Get<HomePage>(); var newsPage = atDriver.Get<NewsPage>();
Returned WebContainer object contains all properties defined in the XML file (WebComponents). You can use them to interact with elements on the tested website.
If some components require parameters (i.e.when using parameterized xpath), you can pass the parameters by using method With()
.
var logo = newsPage.LogoImg; var article = newsPage.ArticleByHeader.With("Article 1");
Component state
In each component, you can check current / future
-
state (
Is
,WillBe
)bool isLogoDisplayedNow = newsPage.LogoImg.Is.Displayed; bool willLogoBeDisplayedInFewSeconds = newsPage.LogoImg.WillBe.Displayed;
-
value (
Has
,WillHave
).bool hasText = newsPage.Header.Has.Text("NEWS"); bool willHaveText = newsPage.Header.WillHave.Text("NEWS");
In addition, you can assert that in the same line
newsPage.LogoImg.Assert.Is.Displayed(); newsPage.Header.Assert.Has.Text("NEWS");
Component actions
In each component you can also perform an action (or perform the action only if element exists without throwing the exception)
newsPage.MenuContact.Perform.Click(); newsPage.ArticleByHeader.With("Article X").PerformIfExists.Click();
Logging
Since version 1.0.124, new log mechanism has been introduced. You can see detailed information about performed actions without making any changes in your tests.
See example below:
[Info] Click the element > Type : Mouse > Result : True > Name : SampleWebSite.EnabledButton > Search by : xpath > Expression : //*[@id='EnabledButton'] [Info] Verify whether the element has value > Type : Equals > Expected : New value > Actual : New value > Result : True > Name : SampleWebSite.TextboxRandom > Search by : xpath > Expression : //*[@id='TextboxRandom'] [Error] Cannot find element > Name : SampleWebSite.ParagraphByText > Search by : parameterized xpath > Parameters : 'Wrong text', 'wrong param' > Expression : //p[text()='Wrong text']a
Tips & tricks
How to check the content of a table
Let's assume that we have a simple table on a page, and we want to validate content of that table.
<table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tbody> <tr> <td>Row 1 C 1</td> <td>Row 1 C 2</td> </tr> <tr> <td>Row 2 C 1</td> <td>Row 2 C 2</td> </tr> </tbody> </table>
Based on above example, all we have to do is to add a new entry in our XML file that will represent a single row. To do so, let's use a parameterized xpath
<container name="SampleWebSite"> <component name="TableRow" pxpath="//table/tbody/tr[./td[1][text()='{0}']][./td[2][text()='{1}']]" /> </container>
- Parameter
{0}
represents the text content of the first cell in a row. - Parameter
{1}
represents the text content of the second cell in a row.
C# code to validate the content
var expectedValues = new string[][] { new string[] { "row 1 col 1", "row 1 col 2"}, new string[] { "row 2 col 1", "row 2 col 2"} }; var page = atDriver.Get<SampleWebsite>(); for (int i = 0; i < expectedValues.Length; i++) { page.TableRow.With(expectedValues[i]).Assert.Is.Displayed(); }
Extensions
- WebAutomation.SpecFlow - integration between WebAutomation and SpecFlow
- WebAutomation.SpecFlowGenericSteps - ready to use SpecFlow steps
Updated