使用 Selenium 测试 Angularjs 应用程序

Testing Angularjs Application with Selenium

我正在测试一个 angular js 应用程序

Link Angular js App

当我在 Web 应用程序上单击 UI 工具包 link 时,出现以下错误 -

at demoaj.Ajapp.main(Ajapp.java:16) Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/div1/div1/aside/div/div/ul/li[2]/a"} Command duration or timeout: 51 milliseconds For documentation on this error, please visit: 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 应用程序测试。

  1. 我想我必须使用量角器?我猜
  2. 如果我使用量角器,我必须在 java 脚本或 jquery?
  3. 中编写代码
  4. 如果我使用量角器,我可以使用 eclipse ide 或 intellij 在 java 中编写我的代码吗?

请帮忙,

提前致谢。

这是你的答案 -

  • 是的,对于 AngularJS 测试,您需要使用 Protractor,因为它具有等待 Angular 加载的内置支持。
  • 如果您使用 Protractor,那么您需要在 JavaScript 而不是 jquery 中编写代码。
  • 如果您使用 Protractor,则不能使用 Java,因为 Protractor 构建于 Selenium WebDriverJS 之上。

优点是您可以编写 Java 脚本代码(比 Java 更简单)来测试您的 angular 应用程序,您不必担心AngularJS 在您的页面上有效。唯一可能让您感到困惑的是使用取自 WebdriverJS 的 promises。希望这有帮助。

以下是我用来测试 AngularJS 应用程序的一些技巧:

  1. 查看 Karma 框架。它非常好,也有很好的文档。 https://karma-runner.github.io/0.8/plus/AngularJS.html
  2. 如果您更喜欢端到端测试,请使用量角器,因为它一次又一次地被证明是最好的。 https://angular.github.io/protractor/#/

我认为 angular 已经呈现了您的元素并且 selenium 正在找到它,但它还不可点击。您可以通过获取元素的一些属性(即文本)来找到它。问题是 angular 没有完成 $http 调用(可能还有 $digest 循环)。一种认为您可以做的是等到 angular 处理完未决的 $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";
                };