硒点击不';t处理angularjs ng点击事件

本文关键字:处理 angularjs 事件 ng | 更新日期: 2023-09-27 18:29:55

我想验证一个特定的外部函数是否在ng-click事件上被调用。输入声明如下。这在浏览器中运行良好,所以功能上没有问题,只是在使用Selenium进行测试时,我没有看到正确的结果。myFunction正在调用一个外部myExternalFunction,这是我试图验证的行为。我们已经有了茉莉花测试,间谍在不同的环境中验证了这种行为,但无论如何都需要硒。此外,我们目前不使用量角器。我正在寻找硒的解决方案。

<input class="myClass" ng-click="myFunction()">

在C#Selenium测试中,我正在写:

// Creating the mock external function since the context doesn't have it.
IJavaScriptExecutor executor = WebDriver as IJavaScriptExecutor;
executor.ExecuteScript("window.called='false'");
executor.ExecuteScript("window.external = {}");
// The behavior will be just setting the called variable to true.
// We will retrieve the variable and check the value to see if the function was called.
executor.ExecuteScript("window.external.myExternalFunction = function() {window.called = 'true';}");
// Select the element - there's only one element with this classname
IWebElement element = WebDriver.FindElement(By.ClassName("myClass"));
string value = "";  
// Verified via debugging that the element is actually valid.
// Tried the following options.
// 1) Just click and wait for event propagation - didn't work.
element.Click(); // Didn't do anything so added a Thread Sleep to make sure.
Thread.Sleep(1000);
value = WebDriver.ExecuteJavaScript<string>("return window.called;"); // returns false - expected to be true.
// 2) Use the actions to focus and click.
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 - expected to be true.
// 3) Select and click via javascript instead.
executor.ExecuteScript("angular.element('.myClass').click();");
Thread.Sleep(1000);
value = WebDriver.ExecuteJavaScript<string>("return window.called;"); // returns false - expected to be true.

在这个阶段我几乎没有什么想法。难道没有办法让Selenium和angularjs玩得很好吗?类似的问题在这里:硒点击事件不会触发angularjs ng点击没有决定性的答案。

硒点击不';t处理angularjs ng点击事件

我建议使用xpath来定位元素,这样应该会很好。如果可能的话,请共享xpath。

您可以使用css选择器,它应该可以工作:

By.cssSelector("input[ng-click='myFunction()']")