I believe that the research is one of the best ways to improve your existing processes. Recently I read a couple of articles about the new cool features of the C# language. There are tonnes of them. However, I am not sure that most people realize how to apply them to make their tests even better. This article will be about that- I will show you how different Selenium WebDriver tests look using C# 5.0 and how they can be prettified using the new improved C# 6.0. You can find even more attractive ideas about your Selenium WebDriver tests in my WebDriver Series. I will create a similar article dedicated to the not released yet C# 7.0.

1. Expression Bodied Function & Property C# 5.0 Version- Expression Bodied Functions Below you can find a page object that represents the SearchEngine main search page. It contains the elements of the page, a search method and a single assertion that verifies the number of the returned results. There are two methods that contain only a single line of code- Navigate and AssertResultsCount. However, each one of them spans over four lines.

C# 6.0 Version- Expression Bodied Functions With the new C# 6.0 syntax, these methods take only a single line of code. How cool is that?!

C# 5.0 Version- Expression Bodied Properties SearchEngineMainPage.Map This is another way of getting the elements of a web page. However, every element spans over seven lines of code!

C# 6.0 Version- Expression Bodied Properties SearchEngineMainPage.Map Here, we can use again the new improved C# 6.0 to rewrite the properties, now each one of them takes only a single line of code. This makes our element map class much shorter.

SearchEngineMainPage The same can be used for properties of the page such as the URL.

2. Auto-Property Initializers C# 5.0 Version Sometimes, we use an object to pass more data to our tests. Most of the times these objects have default values. If we don't specify the values through the first constructor, we use the default one where the default values are initialized.

C# 6.0 Version With the new C# 6.0 auto-property initializers, we don't need the default constructor anymore. You can assign the default values directly after the properties. In my opinion, this syntax is much more readable.

3. nameOf Expression C# 5.0 Version If we want to bulletproof our test framework's API, we usually add validations and throw exceptions. A common practice is to specify the name of the field that was not initialized properly. The problem with the code below is that if you rename the field, it won't be changed in the exception's message.

C# 6.0 Version With the new nameof operator, you can get the name of the field. If it is renamed, the message will be changed too.

4. Null Conditional Operator C# 5.0 Version When we need to automate more complex cases such as filling the billing or shipping information for creating an online purchase. Imagine a site like Online Store. We don't want to pass 10 parameters to our methods instead we use custom objects such as the ClientPurchaseInfo that holds the whole info about the client's inputs. However, some of the fields are optional and if they are null, you don't need to type anything. This is valid for the Zip and the VAT ID. It is hard for me to "parse" a syntax as the one on lines- 20, 21, 22.

C# 6.0 Version When you use the new null conditional operator (clientInfo?.Zip) and you access the VAT property if it is null, instead of throwing NullReferenceException, a null value will be returned. When we combine it with the ?? operator, and the value is null, the right value of the expression will be returned. I believe that the new C# 6.0 syntax is much more readable.

5. Static Using Syntax C# 5.0 Version

Sometimes we use static utility classes for some common actions. In the above code, we use the static TimestampBuilder to generate a unique text and the UniqueEmailGenerator to create a unique email. However, you need always to specify the name of the class that contains the static methods which make the code harder to read. C# 6.0 Version With the new using static syntax, you don't need to specify the name of the static methods' class in front of the methods.

To use the new feature, add a using static line that contains the name of the class that holds the static methods. Then you can use them without the name of the class.

6. String Interpolation C# 5.0 Version