1

I'm wanting to report a line every time my selenium based automation framework clicks on a control. My object repository is storing individual controls like this:

public static By ExampleControl = By.CssSelector("sidemenu > ul > li:nth-child(2) > a");

Each time my click method fires I want it to log something like "User clicked on: ExampleControl" However, when I do this I'm getting "User clicked on: sidemenu > ul > li:nth-child(2) > a". Here is my current code:

public void Click(OpenQA.Selenium.By Control)
{
    WaitForControlClickable(Control);
    TestInitiator.driver.FindElement(Control).Click();
    reporter.LogInfo("User clicked on: " + Control);
}

How do I get that Control in the log to show the name of the control rather than the css selector (or whatever other method I'm using to identify the object)?

dbc
  • 104,963
  • 20
  • 228
  • 340
Staindz
  • 108
  • 2
  • 9

2 Answers2

1

I'd recommend a wrapper class to do this:

public class ByControlWithName
{
    public OpenQA.Selenium.By Control { get; set; }
    public string ControlName { get; set; }

    public ByControlWithName(OpenQA.Selenium.By ctl, string name)
    {
        this.Control = ctl;
        this.ControlName = name;
    }
}

Here's your static call:

public static ByControlWithName ExampleControl = new ByControlWithName(By.CssSelector("sidemenu > ul > li:nth-child(2) > a"), "ExampleControl");

And the updated function:

public void Click(ByControlWithName Control)
{
    WaitForControlClickable(Control.Control);
    TestInitiator.driver.FindElement(Control.Control).Click();
    reporter.LogInfo("User clicked on: " + Control.ControlName);
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
  • I tried this approach but my script is now throwing an exception: OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document – Staindz Sep 06 '18 at 15:43
  • @Staindz - i had to change it so the element's information is grabbed prior to clicking...after the click is too late because it has refreshed the page by then – Ctznkane525 Sep 06 '18 at 16:51
  • So it took me a minute to wrap my head around what your code was doing but now i understand why it's not working for me. I access most objects through xpath and CSS selector. I have a repository of objects and they have "names" but when i refer to that object it gives me me whatever selenium is using to find the object on the page (the xpath, name, id, or css selector). i already have assigned a name to this object, i just need it printed in my logs. In my original post you can see an example of how i store my objects, with "ExampleControl" being the name i want passed in to my logs. – Staindz Sep 06 '18 at 17:31
  • I think you need a 2nd parameter And pass in the nameof example to it each time you call it – Ctznkane525 Sep 06 '18 at 18:11
  • That is also the only solution i've been able to think of as well, which unfortunately, isn't a clean way of doing it since this is called constantly throughout all of my tests. I appreciate you looking through this with me though. – Staindz Sep 06 '18 at 18:43
  • @Staindz - made another recommendation – Ctznkane525 Sep 06 '18 at 19:38
0

Try using nameof(), e.g.:

reporter.LogInfo("User clicked on: " + nameof(Control));

More info here.

misterjake
  • 159
  • 5
  • Thanks for the quick response, i tried implementing this and ended up getting: "User clicked on: Control" rather than the actual name of the control. – Staindz Sep 06 '18 at 15:27