簡體   English   中英

Selenium單擊不適用於angularjs ng-click事件

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

我想要驗證在ng-click事件上調用特定的外部函數。 輸入聲明如下。 這在瀏覽器中工作正常,因此在功能上沒有問題,只有在使用Selenium進行測試時我沒有看到正確的結果。 myFunction正在調用外部myExternalFunction,這是我正在嘗試驗證的行為。 我們已經進行了茉莉花測試,間諜在不同的環境中驗證了這種行為,但無論如何都需要Selenium。 我們目前也不使用量角器。 我正在尋找Selenium的解決方案。

<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玩得很好嗎? 類似的問題: Selenium點擊事件不觸發angularjs ng-click沒有確定的答案。

我建議,使用xpath來定位元素,你應該很好。 如果可能的話,請分享xpath。

你可以使用css選擇器,它應該工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM