2

Is it possible to get get name of variable that was passed as parameter?

As you asked me to explain why I need this, here is my explain. I'm building a testing framweork using selenium. Here is example where I need that name of variable that was passed as parameter.

I have a page object with field:

[FindsBy(How = How.CssSelector, Using = "input[name='sample']")]
private IWebElement SampleButton { get; set; }

I'm clicking it like:

Click(SampleButton, nameof(SampleButton));

Here is a body of Click function that u see above:

public void Click(IWebElement element, string elementName)
        {
            element.Click();
            Logger.Log("Click: '{0}'", elementName);
        }

Result: Button that was passed to function was clicked and in log file I have "Click: SampleButton"

Now I'm looking how to change Click function that it woul be call like:

Click(SampleButton)

It would look like:

public void Click(IWebElement element)
        {
            element.Click();
            Logger.Log("Click: '{0}'", getNameOfVariableThatWasPassedToThisParameter(element));
        }

Expected result of getNameOfVariableThatWasPassedToThisParameter(element) will be string = "SampleButton"

I'm am using things like SendKeys(SampleInput, nameof(SampleInput), valueToSet) and it set value and log where it was set. So I just want to get rid of that nameof(SampleInput). I want to reduce amount of variables than need to be passed to function.

vTad
  • 309
  • 2
  • 13
  • 1
    While it is possible, there are only a very few cases where it's useful. Can explain your *problem* in more detail? Knowing the name of a variable that was passed should not be necessary at all. – nvoigt Mar 10 '16 at 13:05
  • 1
    @nvoigt It is possible? Now you've got my attention.... – atlaste Mar 10 '16 at 13:06
  • 3
    You could use an [Expression](https://msdn.microsoft.com/de-de/library/system.linq.expressions.expression(v=vs.110).aspx) to do this. For example, see [this](http://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct). However, I strongly suspect you have an XY-problem. Describe your problem instead of taking the solution for granted and you may get a way better answer. – nvoigt Mar 10 '16 at 13:11
  • @nvoigt Ah, that's what you meant. Nice one. Yes, I suppose that will do for this scenario. – atlaste Mar 10 '16 at 13:13
  • @ago I'm bulidinng testing framwork. I need it to function that will click button that will be passed by parameter and Log it like "Clicked: buttonName". In that case I'm using it like Click(LoginButton, nameof(LoginButton)). And I was just wondering if it is possible to have only Click(LoginButton); – vTad Mar 11 '16 at 08:54
  • This will not be possible in the general case. There is no variable name in this case: `Click(GetSampleButton())`. Even if you constrain it to the case where the call is done using a variable name it will be next to impossible to get the variable name at run-time. The IL call instruction has a value on the stack. You need to discover how the value ended up on the stack. Did it originate from a variable? And then you need the symbols to get the name of the variable. And don't try this in a release build. – Martin Liversage Mar 11 '16 at 11:56
  • @vTad, did you find any solution of your problem? I have exactly the same case, need logging selenium steps... – kotoj Jan 30 '17 at 14:09

0 Answers0