0

I am new to Automated testing (new to coding) and have been teaching myself C# and selenium using Visual Studio and MS Test. I am trying to get the test name (As defined from test Method) so I can insert this into a config class for my Browserstack/CBT comparison and review testing.

What I want to be able to define is something like

Testname = Name of test (from test method) so I can then insert this into my driver file

IWebDriver driver;

DesiredCapabilities caps = new DesiredCapabilities();

caps.SetCapability("name", Testname);

Reading online I know there is a TestName capability in MS Test however I am not able to figure out how to utilise it for my purpose.

Any help greatly appreciated happy to give additional info if needed.

regards

Richard

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Richard C
  • 513
  • 1
  • 7
  • 26
  • The capability you've used is correct while testing on BrowserStack. I understand the only problem here is retrieving the test method name. I looked up a few online resources and created this script - https://gist.github.com/ashwingonsalves/4a139fc1ac9824eff07b3898d199c3f4. You might need to work on this. – Mukesh Tiwari Mar 07 '17 at 12:42
  • did not post the way I wanted so will post as an answer. – Dazed Mar 07 '17 at 14:48

2 Answers2

2

The easier way to get the name of the test method in MS Test is by using the TestContext property:

First, inside your test class add the following line (if not already exist):

public TestContext TestContext { get; set; }

MS-Test will set this property to a TestContext object relevant for the current test.

Then you can use:

string testName = TestContext.TestName;
...
caps.SetCapability("name", testname);
Arnon Axelrod
  • 1,444
  • 2
  • 13
  • 21
0

I had to do something similar with Saucelabs. In my setup I added the below. You will probably need to change this a bit though to support your framework. I use SpecFlow.

So the whole point of this is that you are passing a TestName in but the TestName is not available yet.

This is a BeforeScenario hook that will start the driver and I am passing the test name aka "Title". You will need to find out where TestName is available and then pass that value in.

var Title = ScenarioContext.Current.ScenarioInfo.Title;
Browser.StartSauceDriver(Title);

Then in the StartSauceDriver I have the Title I can use.

public static void StartSauceDriver(string Title)
    {
        {

                DesiredCapabilities caps = new DesiredCapabilities();
                caps.SetCapability(CapabilityType.BrowserName, System.Environment.GetEnvironmentVariable("SELENIUM_BROWSER"));
                caps.SetCapability(CapabilityType.Version, System.Environment.GetEnvironmentVariable("SELENIUM_VERSION"));
                caps.SetCapability(CapabilityType.Platform, System.Environment.GetEnvironmentVariable("SELENIUM_PLATFORM"));
                caps.SetCapability("name", Title);
                _webDriver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), caps, TimeSpan.FromSeconds(600));
                _wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(600));

        }
Dazed
  • 1,527
  • 1
  • 13
  • 25