I am using Selenium and I have the following extensions methods for executing javascript.
private const int s_PageWaitSeconds = 30;
public static IWebElement FindElementByJs(this IWebDriver driver, string jsCommand)
{
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(d => d.FindElementByJs(jsCommand));
}
return driver.FindElementByJs(jsCommand);
}
public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand)
{
return FindElementByJsWithWait(driver, jsCommand, s_PageWaitSeconds);
}
In my HomePage class I have the following attribute.
public class HomePage : SurveyPage
{
[FindsBy(How = How.Id, Using = "radio2")]
private IWebElement employerSelect;
public HomePage(IWebDriver driver) : base(driver)
{
}
public override SurveyPage FillOutThisPage()
{
employerSelect.Click();
employerSelect.Submit();
m_driver.FindElement(By.)
return new Level10Page(m_driver);
}
}
However, employerSelect is generated by Javascript, so is there a way to do something like this:
public class HomePage : SurveyPage
{
const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";
[FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
private IWebElement employerSelect;
public HomePage(IWebDriver driver) : base(driver)
{
}
public override SurveyPage FillOutThisPage()
{
employerSelect.Click();
employerSelect.Submit();
m_driver.FindElement(By.)
return new Level10Page(m_driver);
}
}
In essence, I want to replace the raw ExecuteJs call as part of the FindsBy attribute such as:
const string getEmployerJsCommand = "return $(\"li:contains('Employer')\")[0];";
IWebElement employerSelect = driver.FindElementByJsWithWait(getEmployerJsCommand);
into part of the FindsBy attribute like this:
const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";
[FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
private IWebElement employerSelect;
What could I extend to do something like that?