I believe that to write high-quality tests we need to prevent the copy-paste development. To write UI tests we usually utilise the Page Object Design Pattern. Most people to create a new page, they copy an existing one and delete the redundant code. I believe that this practice is error prompt and tedious. Because of that in this publication, I am going to show you how to create item templates to solve this problem. You can find similar solutions in the other articles from the Automation Tools Series.

What is an Item Template?

Definition The metadata file (*.vstemplate) that Visual Studio uses to define how to display the project item in the integrated development environment (IDE) and, if you have specified the appropriate properties, to customize how the project item is created.

How to Create Single Page Template for Standard Page Object?

In my article Page Object Pattern in Automated Testing, I showed you how to use the default WebDriver Page Objects. You can find below the default code for a regular page. Usually, if you want to create a new page, you will copy one of the existing pages, remove the redundant code and start to add the new logic. However, for me, this process is tedious and error prompt. Because of that, I believe that if we create a template for this boilerplate code, we will reduce the amount of the required work and errors. Two years ago while we were working on the first version of the BELLATRIX test automation framework, I did this research so that we can find the most convenient way for creating page objects. Create Clean Default Template 1. Copy the above page and start deleting the redundant code. Leave only the bare minimum required. 2. Now it is time to export the file as a template. On the File menu, click Export Template. 3. Click Item Template, select the project that contains the item and click Next. 4. Select the item for which you want to create a template, and click Next. 5. You can select the assembly references to be included in the template, but we will skip this part. ​ 6. Next type the icon file name, preview image. You can find the most appropriate from some of the most popular sites such as FlatIcon. 7. Type the template name and description, and click Finish. The files for the template are added to a .zip file and copied whatever directory you specify in the dialog. The default location is ..Users/<username>/Documents/Visual Studio <Version>/My Exported Templates/ folder.

Import Exported Templates in Visual Studio

By default, the template is imported automatically. However, if for some reason you have deleted it. You need to copy the exported template to ..Users/<username>/Documents/Visual Studio <Version>/Templates/ItemTemplates/ How to Use the Created Items Templates? To use your new template add a new item.

It is possible not to see your template at first when you open the new item window. To fix the problem close all Visual Studio instances and open your solutions' folder. Delete the .vs folder.

After that, open your solution again and try to create a new item.

The following new file is created.

The class name and the filename are automatically changed to the value that you have specified in the new item window. This is how looks the current version of the exported template- MyTemplate.vstemplate file.