如何获取用于报告的 Selenium C# 控件名称
How to get Selenium C# control names for reporting
每次我的基于 selenium 的自动化框架单击控件时,我都想报告一行。我的 object 存储库正在存储这样的单个控件:
public static By ExampleControl = By.CssSelector("sidemenu > ul > li:nth-child(2) > a");
每次我的点击方法触发时,我希望它记录类似“用户点击:ExampleControl”的内容但是,当我这样做时,我得到“用户点击:sidemenu > ul > li:nth- child(2) > 一个”。这是我当前的代码:
public void Click(OpenQA.Selenium.By Control)
{
WaitForControlClickable(Control);
TestInitiator.driver.FindElement(Control).Click();
reporter.LogInfo("User clicked on: " + Control);
}
如何在日志中获取该控件以显示控件的名称而不是 css 选择器(或我用来识别 object 的任何其他方法)?
尝试使用 nameof()
,例如:
reporter.LogInfo("User clicked on: " + nameof(Control));
更多信息here。
我建议使用包装器 class 来执行此操作:
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;
}
}
这是您的静态调用:
public static ByControlWithName ExampleControl = new ByControlWithName(By.CssSelector("sidemenu > ul > li:nth-child(2) > a"), "ExampleControl");
更新后的函数:
public void Click(ByControlWithName Control)
{
WaitForControlClickable(Control.Control);
TestInitiator.driver.FindElement(Control.Control).Click();
reporter.LogInfo("User clicked on: " + Control.ControlName);
}
每次我的基于 selenium 的自动化框架单击控件时,我都想报告一行。我的 object 存储库正在存储这样的单个控件:
public static By ExampleControl = By.CssSelector("sidemenu > ul > li:nth-child(2) > a");
每次我的点击方法触发时,我希望它记录类似“用户点击:ExampleControl”的内容但是,当我这样做时,我得到“用户点击:sidemenu > ul > li:nth- child(2) > 一个”。这是我当前的代码:
public void Click(OpenQA.Selenium.By Control)
{
WaitForControlClickable(Control);
TestInitiator.driver.FindElement(Control).Click();
reporter.LogInfo("User clicked on: " + Control);
}
如何在日志中获取该控件以显示控件的名称而不是 css 选择器(或我用来识别 object 的任何其他方法)?
尝试使用 nameof()
,例如:
reporter.LogInfo("User clicked on: " + nameof(Control));
更多信息here。
我建议使用包装器 class 来执行此操作:
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;
}
}
这是您的静态调用:
public static ByControlWithName ExampleControl = new ByControlWithName(By.CssSelector("sidemenu > ul > li:nth-child(2) > a"), "ExampleControl");
更新后的函数:
public void Click(ByControlWithName Control)
{
WaitForControlClickable(Control.Control);
TestInitiator.driver.FindElement(Control.Control).Click();
reporter.LogInfo("User clicked on: " + Control.ControlName);
}