简体   繁体   English

Selenium单击不适用于angularjs ng-click事件

[英]Selenium click doesn't work with angularjs ng-click event

I'm looking to verify a particular external function is called on a ng-click event. 我想要验证在ng-click事件上调用特定的外部函数。 The input declaration is as follows. 输入声明如下。 This works fine in the browsers, so there are no problems functionally, only during testing with Selenium I'm not seeing the correct result. 这在浏览器中工作正常,因此在功能上没有问题,只有在使用Selenium进行测试时我没有看到正确的结果。 myFunction is calling an external myExternalFunction, which is the behaviour I'm trying to validate. myFunction正在调用外部myExternalFunction,这是我正在尝试验证的行为。 We already have jasmine tests with spies validating this behaviour in a different environment, but Selenium is needed anyways. 我们已经进行了茉莉花测试,间谍在不同的环境中验证了这种行为,但无论如何都需要Selenium。 Also we don't use protractor at the moment. 我们目前也不使用量角器。 I'm looking for a solution with Selenium. 我正在寻找Selenium的解决方案。

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

In the C# Selenium tests, I'm writing: 在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.

I'm pretty much out of ideas at this stage. 在这个阶段,我几乎没有想法。 Is there no way to make Selenium play nice with angularjs? 难道没有办法让Selenium与angularjs玩得很好吗? Similar question here: Selenium click event does not trigger angularjs ng-click without conclusive answer. 类似的问题: Selenium点击事件不触发angularjs ng-click没有确定的答案。

I would suggest, use xpath for locating the element and you should be good. 我建议,使用xpath来定位元素,你应该很好。 Please share the xpath, if possible. 如果可能的话,请分享xpath。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM