0

In short, im creating a Visual Studio extension in c# that enables non-automation testers to create automation scripts for web based solutions (using selenium).

We've created a template already for users that lays out the project file structure (test runners etc) and im using windows forms toolbox controls to create the dialogue windows where they will enter things like URls, Xpaths, page names etc etc

My question is this;

How on earth do i go about creating .CS files dynamically based on their inputs in these toolbox windows?

The simplest (he says) window i have is one that simply allows them to enter a URl and a webpage name. When they click the 'ok' button on this window, it should create a new .CS file with the webpage name as the class name, the url as a string that the webdriver can use to kick off the test and a few other bits and pieces as a template for them to start adding web elements to.

Any guidance would be greatly appreciated, there seems to be precious little around the web about creating VS extensions!

  • Look at Reflections: https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class . You can probably add some custom code to your VSIX project, that uses reflections to dynamically generate .cs files – Gašper Sladič Feb 24 '20 at 21:17
  • Many thanks! ill take a look! – Jamesbiff Feb 25 '20 at 07:59

1 Answers1

2

You got a few options here.

Typically, most people would do this by implementing a custom project item template, along with a custom IWizard based wizard associated with your template.

If you are displaying a modal UI from your IWizard.RunStarted, you can simply populate the ReplacementsDictionary with the text gleaned from your custom UI, which would then be swapped for the tokens in your templatized .cs file.

Or you could programmatically add code to the file after it was generated and added to the project (admittedly a much uglier and more difficult to code).

And finally, you could just generate the file in the project directory, and programmatically add it after the fact.

There's a number of old blog articles from the archived VSX Arcana blog that you might also find helpful.

Ed Dore
  • 2,013
  • 1
  • 10
  • 8
  • Many thanks! ill take a look! My initial idea was to get the current project directory (as we cant be sure our testers will always use the /repos folder) and then just drop a .CS file in there. But i have a feeling there may be a permissions issue down the line as not all of them have local admin access. – Jamesbiff Feb 25 '20 at 08:00