I am currently trying to confirm that a specific external function is being called when an ng-click event occurs. The code for the input declaration is shown below. While everything seems to be functioning properly in browsers, I am encountering issues during testing with Selenium as the expected result is not showing up. The myFunction is supposed to call an external myExternalFunction, which is what I need to verify. We already have jasmine tests set up with spies to validate this behavior in a different environment, but we still require Selenium for our testing. Additionally, we are not using protractor at the moment. I am seeking a solution specifically using Selenium.
<input class="myClass" ng-click="myFunction()">
For the C# Selenium tests, here is the code I have written:
// Creating a mock external function since it is not available in the context.
IJavaScriptExecutor executor = WebDriver as IJavaScriptExecutor;
executor.ExecuteScript("window.called='false'");
executor.ExecuteScript("window.external = {}");
// The behavior of the function will simply set the 'called' variable to true.
// We will retrieve this variable afterwards to check if the function was called.
executor.ExecuteScript("window.external.myExternalFunction = function() {window.called = 'true';}");
// Selecting the element - there is only one element with this classname
IWebElement element = WebDriver.FindElement(By.ClassName("myClass"));
string value = "";
// I have verified through debugging that the element is indeed valid.
// I have tried the following options.
// 1) Simply click and wait for event propagation - did not work.
element.Click(); // Did not perform any action so added a Thread Sleep for verification.
Thread.Sleep(1000);
value = WebDriver.ExecuteJavaScript<string>("return window.called;"); // returns false - expecting true.
// 2) Using actions to focus and then click on the element.
IWebDriver driver = WebDriver;
Actions a = new Actions(driver);
a.MoveToElement(element).Perform();
a.MoveToElement(element).Click().Perform();
Thread.Sleep(1000);
value = WebDriver.ExecuteJavaScript<string>("return window.called;"); // returns false - expecting true.
// 3) Selecting and clicking via javascript instead.
executor.ExecuteScript("angular.element('.myClass').click();");
Thread.Sleep(1000);
value = WebDriver.ExecuteJavaScript<string>("return window.called;"); // returns false - expecting true.
At this point, I have exhausted all my ideas. Is there no way to get Selenium to work seamlessly with angularjs? A similar question can be found here: Selenium click event does not trigger angularjs ng-click without a definitive answer.