使用Selenium测试Angularjs应用程序

我正在测试角度js应用程序

链接Angular js App

当我点击Web应用程序上的UI工具包链接时,我收到以下错误 –

at demoaj.Ajapp.main(Ajapp.java:16)引起:org.openqa.selenium.NoSuchElementException:无法找到元素:{“method”:“xpath”,“selector”:“html / body / div 1 / div 1 / 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 App测试。

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

请帮忙,

提前致谢。

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

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

这是你的答案 –

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

优点是你可以编写Javascript代码(比Java简单)来测试你的角度应用程序,你不必担心AngularJS如何在你的页面上工作。 只有可能让您困惑的事情是使用从WebdriverJS获取的promises。 希望这可以帮助。

我认为angular已经渲染了你的元素和selenium正在寻找它,但它还没有被点击。 您可以通过获取元素的某些属性(即文本)来找到它。 问题是角度未完成$ 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 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"; };