2015-09-14 121 views
6

我測試的角度JS應用測試Angularjs應用與硒

鏈接Angular js App

當我點擊Web應用程序我得到以下錯誤的UI套件鏈接 -

在demoaj.Ajapp.main(Ajapp.java:16)引起: org.openqa.selenium.NoSuchElementException:無法找到元素: {「method」:「xpath」,「selector」:「html/body/div 1/div 1 /拋開/ DIV/DIV/UL /李[2]/A「} 命令持續時間或超時:51毫秒有關此 錯誤文檔,請訪問: http://seleniumhq.org/exceptions/no_such_element.html

我是新來這個我沒在此方面的研究AngularJS

Java代碼

package demoaj; 

    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.openqa.selenium.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.WebDriverWait; 

    public class Ajapp { 

     public static void main(String[] args) throws InterruptedException { 
     WebDriver d=new FirefoxDriver(); 
     d.manage().window().maximize(); 
     d.get("http://iarouse.com/demo/index.html?product=square"); 
     WebDriverWait wait = new WebDriverWait(d, 20); 
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div[1]/div[1]/aside/div/div/ul/li[2]/a"))).click(); 
     //d.findElement(By.xpath("html/body/div[1]/div[1]/aside/div/div/ul/li[2]/a")).click(); 

     } 

} 

我想是因爲在angularjs DOM不可渲染它無法找到的元素。當我檢查頁面的源代碼時,它沒有顯示任何東西隱藏後做了一些關於angularjs測試的研究,我有幾個問題請幫忙,

用於Angular App測試。

  1. 我想我必須使用量角器?我猜
  2. 如果我使用量角器我必須在JavaScript或jQuery中編寫代碼?
  3. 如果我使用量角器可以使用eclipse ide或intellij在java中編寫我的代碼嗎?

請幫幫忙,

在此先感謝。

回答

3

這裏是你的答案 -

  • 是爲AngularJS測試需要使用量角器,因爲它有內置的支持,以等待角加載。
  • 如果您使用的是Protractor,那麼您需要使用JavaScript編寫代碼而不是jquery。
  • 如果您使用量角器,您不能使用Java作爲量角器構建在Selenium WebDriverJS之上。

優勢在於您可以編寫Javascript代碼(比Java更簡單)來測試您的角度應用程序,而且您不必擔心AngularJS如何在您的頁面上工作。唯一可能會讓你感到困惑的是使用從WebdriverJS獲取的promises。希望這可以幫助。

0

我認爲棱角已經呈現您的元素,硒正在發現它,但它不是可點擊的。你可以通過獲取元素的一些屬性(即文本)來找到它。問題是角度未完成$ http調用(可能是$ digest循環)。一個人認爲你可以做的就是等待角落完成掛起的$ http請求,然後找到你想要的元素並調用.Click()就可以了。以下是我用於C#測試項目的工作代碼片段。

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(angularIsDoneLoading); 
//Add the rest of your code in here(like finding element, clicking on it etc.) 


     Func<IWebDriver, bool> angularIsDoneLoading = (IWebDriver drv) => 
       { 
        string ngFinishedAllRequests = 
         @"var injector = window.angular.element('body').injector(); 
         var $http = injector.get('$http'); 
         return ($http.pendingRequests.length === 0)"; 

        return ((IJavaScriptExecutor)drv).ExecuteScript(ngFinishedAllRequests).ToString() == "True"; 
       };