1

Using C#, is it possible to start a selenium test?

The only way I found is via the UI by right clicking on the test itself and starting it manually.

enter image description here

5
  • "Programatically start a Selenium test" -> when would you expect it automatically to start? The reason why it's in the UI is because you, as the developer of that code, know when you want to run your tests, you know when your code is complete enough to run them. Commented Apr 23, 2014 at 14:19
  • @Arran The reason is not the point of this post. But if you really need it, I want to build a form from which I can call my tests. I want to add a few things from there like some environment settings and variables. I also want this form to be the easy to use UI so lot of users can run my tests from there. Commented Apr 23, 2014 at 14:32
  • It is important. You have asked how to programatically run these tests. In most cases people just implement a CI solution so that every time they push/commit to their repository, the tests are run for them. You've now expanded and mentioned this isn't what you are after. What kind of form? Web app? Windows Forms? Will this be run on other machines (if so will you need to consider the requirements for the tests, like Selenium & Chrome being installed?). An easy solution is to just have your form call MSTest on the command line. Commented Apr 23, 2014 at 14:46
  • @Arran The thing is, if my question was simply to know if it was somehow possible to automate them, what you are saying would be fine. Right now it shouldn't matter to you how I plan to implement it or where. The only thing I need to know is : "Using C#, is it possible to start a selenium test?". I don't need to know if it is easier from the command line or to explain how I plan to integrate the answer in my project. Just this very simple question. Using C#, is it possible to start a selenium test? Commented Apr 23, 2014 at 14:57
  • Absolutely, it's programming. :) You can do (almost) anything. Quickest solution is to call the command line. MSTest & NUnit both have command lines for this very reason. After all, that's all the UI will be doing. Time to get coding: msdn.microsoft.com/en-us/library/ms182489.aspx Commented Apr 23, 2014 at 15:05

2 Answers 2

3

As what Arran said, this question is really just about how to run tests written using Visual Studio Unit Testing Framework and has nothing to do with Selenium.

Since you can run the test from command line, what you need is just to start calling the command from your C# code.

For example, here is how to start mstest.exe with your tests (see MSDN documentation from more test options please):

Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(PATH_TO_MSTEST_EXE, "/testcontainer:" + PATH_TO_TEST_DLL);

myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, just to expand on the original post, you can run selenium test as a C Sharp class as follows:

Download Visual studio community edition.

Create a project with template Visual C#: unit test project

Add selenium webdriver package

add selenium support

under the new project, add a new C sharp class: Add : Unit Test

A selenium test is a C# class

Now you create the test case

Add [TestClass] and [TestMethod] and so on

Now you can run the selenium test

You can keep adding test cases under the same project

Each test case is saved as a .cs file

After a while , you will have a bunch of test cases under the same project and you can run them automatically one after the other by just right clicking the editor and click 'run tests'

A lot more is involved, and its best to buy books on this area. A lot of reading to be done. Personally, I prefer Python for test scripting but I am sure others have their own favorite language. It boils down to whichever language you have the most experience with(your comfort zone).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.