Java 使用Selenium测试Angularjs应用程序
我正在测试一个angular js应用程序 链接 当我点击web应用程序上的UI工具包链接时,我得到了以下错误- 在demoaj.Ajapp.main(Ajapp.java:16)处,由以下原因引起: org.openqa.selenium.NoSuchElementException:无法定位元素: {“方法”:“xpath”,“选择器”:“html/body/div/div/aside/div/div/ul/li[2]/a”} 命令持续时间或超时:51毫秒用于此文档 错误,请访问: 我是新来的我对AngularJS做了一些研究 java代码Java 使用Selenium测试Angularjs应用程序,java,angularjs,selenium,protractor,webdriverjs,Java,Angularjs,Selenium,Protractor,Webdriverjs,我正在测试一个angular js应用程序 链接 当我点击web应用程序上的UI工具包链接时,我得到了以下错误- 在demoaj.Ajapp.main(Ajapp.java:16)处,由以下原因引起: org.openqa.selenium.NoSuchElementException:无法定位元素: {“方法”:“xpath”,“选择器”:“html/body/div/div/aside/div/div/ul/li[2]/a”} 命令持续时间或超时:51毫秒用于此文档 错误,请访问: 我是新
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测试进行了一些研究之后,所有内容都被隐藏了。我有一些问题,请帮助
用于角度应用程序测试
提前谢谢 以下是您的答案-
- 是的,对于AngularJS测试,您需要使用量角器,因为它具有内置支持,可以等待AngularJS加载
- 如果使用量角器,则需要用JavaScript而不是jquery编写代码
- 如果使用量角器,则不能使用Java,因为量角器构建在Selenium WebDriverJ之上
优点是您可以编写Javascript代码(比Java简单)来测试AngularJS应用程序,而不必担心AngularJS在页面上的工作方式。唯一可能让您困惑的是使用WebdriverJS中的内容。希望这能有所帮助。以下是我用来测试AngularJS应用程序的一些技术:
我认为angular已经渲染了您的元素,selenium正在查找它,但它还不能单击。您可以通过获取元素的一些属性(即文本)来找到它。问题在于angular未完成$http调用(可能还有$digest循环)。您可以做的一件事是等待angular处理完挂起的$http请求,然后找到所需的元素并调用。下面是我在C#中用于测试项目的工作代码片段
newwebdriverwait(驱动程序,时间跨度从秒(20))到(angularsdoneloading);
//在此处添加其余代码(如查找元素、单击元素等)
Func angularIsDoneLoading=(IWebDriver drv)=>
{
字符串ngFinishedAllRequests=
@“var injector=window.angular.element('body').injector();
var$http=injector.get(“$http”);
返回($http.pendingRequests.length==0)”;
return((IJavaScriptExecutor)drv.ExecuteScript(ngFinishedAllRequests.ToString()=“True”;
};
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";
};