Click here to Skip to main content
14,487,207 members
Articles » Languages » C# » General » Revisions

Xenon

Rate this:
4.75 (4 votes)
Please Sign up or sign in to vote.
4.75 (4 votes)
6 Nov 2014CPOL
Xenon is a framework which helps you to write stable acceptance tests
This is an old version of the currently published article.

Introduction

Xenon is a framework which helps to write stable (non flakey) acceptance tests with a fluent API. We currently only support Selenium browser automation, but we will implement more if required.

The advantage of using this framework is that you don't have to worry about your page taking time to load, or your elements being dynamically added to the page. All this is handled internally using pre and post waits. These waits do not slow down your test either as for example and as soon as the element appears on the page it will interact with. If you want to wait upon something out of the ordinary, you can do this using custom pre and post waits.

Background

We wrote this framework as there was one issue that kept annoying us on our build server. Our tests passed and failed whenever they felt like it. We would spend ages trying to fixing the tests only to find a few days later they were failing again.

We then came up with Xenon to make our tests consistently pass with great success. Now all our tests pass all the time (unless we broke something) and we hope that it can do the same for you.

Using the Code

Setting up Xenon

Installing

You can either download the source code from our GitHub repo here and compile it yourself. You also install it from nuget:

As currently selenium is the only supported browser automation framework, we recommend that you use Xenon.Selenium as Xenon only contains the core code and would require you to manually implement the browser automation side.

Configuring

To use selenium, you must setup a couple of things (only needs to be done once):

Setting up the options
XenonTestOptions.Options = new XenonTestOptions
{
    //This is required as we did no make Xenon framework 
    //specific so you have to specify you test frameworks assert method
    Assert.Method = (passed, message) => Assert.IsTrue(passed, message),

    //This is optional and specifies how long Xenon 
    //should wait for something to happen by default it is 5
    WaitForSeconds = 10
}

Writing a Test (Basic)

To write a simple but reliable test is easy using our fluent API. As demonstrated below (we will use selenium in this example and search for something on Google):

void TestMethod(){
    //This setups the browser so that it can be used in the test
    var browser = new SeleniumXenonBrowser( new ChromeDriver() );
    new XenonTest( browser )
        .GoToUrl( "http://www.google.co.uk" )
        .EnterText( "[name='q']", "Xenon is amazing" )
        .Click( "[name='btnG']" )
        .Assert( a => a.PageContains( "results") );
}

This test will now reliably pass even if going to the results page takes a while as Xenon will give the browser a chance to go to that page before failing.

Writing this test is fine for simple tests but when tests get more complicated, we created Screens which are explained below.

Writing a test (Using Screens)

To write more complex tests, you can use screens. These allow you to reuse common code throughout all of your tests. To create a screen, you write a class that inherits from XenonScreen as demonstrated below:

public class GoogleHomeScreen : XenonScreen<GoogleHomeScreen>{
    public GoogleHomeScreen( IXenonBrowser browser ) : base( browser ){
        GoToUrl("http://www.google.co.uk");
    }

    public GoogleSearchResultsScreen Search( string text ){
        return EnterText( "input[name='q']" )
            .Click( "[name='btnK']" )
            .Switch<GoogleSearchResultsScreen>();
    }
}

public class GoogleSearchResultsScreen : XenonScreen<GoogleSearchResultsScreen>{
    public GoogleSearchResultsScreen( IXenonBrowser browser ) : base( browser ){}
} 

You can the write your test as follows:

void TestMethod(){
    new GoogleHomeScreen( new SeleniumXenonBrowser( new ChromeDriver() )
        .Search( "Xenon is the best" )
        .Assert( a => a.PageContains( "results" ) );
}

Notes

If you would like to view our source code, you can access it at our GitHub repo here.

If you have any recommendations or questions, please leave a comment below or an issue on GitHub.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jake Rote
United States United States
No Biography provided

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.

Article
Posted 6 Nov 2014

Stats

9K views
12 bookmarked