Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 硒点击不';不能使用angularjs ng单击事件_Javascript_C#_Angularjs_Selenium_Selenium Webdriver - Fatal编程技术网

Javascript 硒点击不';不能使用angularjs ng单击事件

Javascript 硒点击不';不能使用angularjs ng单击事件,javascript,c#,angularjs,selenium,selenium-webdriver,Javascript,C#,Angularjs,Selenium,Selenium Webdriver,我希望验证在ng click事件中是否调用了特定的外部函数。输入声明如下所示。这在浏览器中运行良好,因此功能上没有问题,只是在使用Selenium进行测试期间,我没有看到正确的结果。myFunction正在调用外部myExternalFunction,这是我试图验证的行为。我们已经对间谍进行了茉莉花测试,在不同的环境中验证了这种行为,但无论如何都需要硒。我们现在也不用量角器。我正在寻找含硒的溶液 <input class="myClass" ng-click="myFunction()"&

我希望验证在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.
//正在创建模拟外部函数,因为上下文没有它。
IJavaScriptExecutor executor=WebDriver作为IJavaScriptExecutor;
executor.ExecuteScript(“window.called='false');
ExecuteScript(“window.external={}”);
//行为将只是将被调用的变量设置为true。
//我们将检索变量并检查值,以查看函数是否被调用。
ExecuteScript(“window.external.myExternalFunction=function(){window.called='true';}”);
//选择元素-只有一个元素具有此类名
IWebElement元素=WebDriver.FindElement(By.ClassName(“myClass”);
字符串值=”;
//通过调试验证元素是否实际有效。
//尝试了以下选项。
//1)只需单击并等待事件传播-不起作用。
元素。单击();//没有做任何事情,所以添加了一个线程睡眠来确保。
睡眠(1000);
value=WebDriver.ExecuteJavaScript(“返回窗口.called;”);//返回false-预期为true。
//2)使用动作聚焦并单击。
IWebDriver=WebDriver;
动作a=新动作(驾驶员);
a、 MoveToElement(元素).Perform();
a、 MoveToElement(元素)。单击()。执行();
睡眠(1000);
value=WebDriver.ExecuteJavaScript(“返回窗口.called;”);//返回false-预期为true。
//3)改为通过javascript选择并单击。
ExecuteScript(“angular.element('.myClass')。click();”;
睡眠(1000);
value=WebDriver.ExecuteJavaScript(“返回窗口.called;”);//返回false-预期为true。

在这个阶段,我几乎没有主意了。没有办法让Selenium和angularjs玩得很好吗?类似的问题:没有结论性的答案。

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

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

我建议您使用xpath来定位元素,您应该会做得很好。如果可能,请共享xpath。

我将尝试此方法并返回给您,但是我已经验证了我能够选择正确的元素。该css类只有一个现有元素,在调试时,我看到了有关该元素的正确详细信息-因此问题似乎更可能与实际事件传播有关。我将尝试此方法并返回给您,但是我已经验证了我能够选择正确的元素。css类只有一个现有元素,在调试时,我看到了关于该元素的正确细节,因此问题更可能与实际的事件传播有关。这似乎解决了Firefox 47的问题。使用Css选择器确实填充了字段,但没有触发javascript。