【问题标题】:Testing Angularjs Application with Selenium使用 Selenium 测试 Angularjs 应用程序
【发布时间】:2015-12-10 00:46:32
【问题描述】:

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

链接Angular js App

当我单击 Web 应用程序上的 UI Kit 链接时,我收到以下错误 -

在 demoaj.Ajapp.main(Ajapp.java:16) 引起: org.openqa.selenium.NoSuchElementException:无法定位元素: {"方法":"xpath","选择器":"html/body/div1/div1/aside/div/div/ul/li[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 应用测试。

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

请帮忙,

提前致谢。

【问题讨论】:

    标签: java angularjs selenium protractor webdriverjs


    【解决方案1】:

    这是你的答案 -

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我认为 angular 已经渲染了你的元素,而 selenium 正在找到它,但它还不能点击。您可以通过获取元素的一些属性(即文本)来找到它。问题在于角度没有完成 $http 调用(可能是 $digest 循环)。一种认为您可以做的是等到 angular 完成挂起的 $http 请求,然后找到您想要的元素并在其上调用 .Click() 。下面是我在 C# 中用于我们的测试项目的工作代码 sn-p。

         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";
                        };
        

        【讨论】:

          猜你喜欢
          • 2016-08-03
          • 2011-08-18
          • 2012-02-09
          • 2017-02-20
          • 2016-07-20
          • 2014-09-23
          • 1970-01-01
          • 1970-01-01
          • 2023-01-29
          相关资源
          最近更新 更多