VaraPrasad Pakalapati
VaraPrasad Pakalapati

Reputation: 177

Selenium IJavaScriptExecutor `ExecuteScript()` method throwing an exception when passing page objects as params

I am creating a BDD framework using Selenium 2, C#, SpecFlow, Page Objects and Page Factory to initialize the page objects.

For reporting purpose and exception handling I have overridden the event listeners of Selenium basic actions using EventFiringWebDriver class and added report calls in overridden methods.

public class WebDriverActionListener : EventFiringWebDriver
{
     protected override void OnElementClicked(WebElementEventArgs e)
     {
        base.OnElementClicked(e);
        const string expectedDescription = "Click on the element";
        var actualDescription = String.Format("Successfully clicked on the element '{0}'", ScenarioContext.Current["element"]);
        Reporter.ReportEvent(expectedDescription, actualDescription, Status.Pass); // Logging the low level reporting
        _driver.Sync();
     }
}

Now I am creating object for WebDriverActionListener class like below:

return new WebDriverActionListener(new FirefoxDriver());

I am initializing the page objects using the same driver which is initialized above.

[FindsBy(How = How.Id, Using = "Login1_UserName")]
private IWebElement _userName;

public LoginPage(IWebDriver driver)
{
  _driver = driver;
  PageFactory.InitElements(_driver, this);
  if (!(_userName.Displayed && _loginButton.Displayed))
    throw new InvalidPageObjectException("Login page is not displayed", Status.Fail);
}

public void SomeMethod()
{
 _userName.JsClick(); // Java Script click call method
}

I have created a method to click some page objects using javascript(very legacy application). I am getting exception when I try to pass the page object element into ExecuteScript() method. As per help provided it should accept `IWebElement' as params.

public static void JsClick(this IWebElement element)
{
  ((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].click();", element);
}

But I am unable to use page objects in 'ExecuteScript()` method.

Upvotes: 0

Views: 3501

Answers (2)

Saifur
Saifur

Reputation: 16201

Read it too fast. If I am not mistaking(without testing as well) the extension method is required to be static. Reference

Upvotes: 1

user4769435
user4769435

Reputation:

Well, I don't have a suitable solution but I have a work around for this.

Element which you want to click using Java script, find them using FindElement() method of WebDriver rather than creating a page object (only element you want to click using Java script)

_driver.FindElement(By.Id("Login1_UserName")).JsClick();

This approach should work without any exception.

Upvotes: 2

Related Questions