And*_*Ahn 5 javascript java selenium angularjs
这是一个带有 AngularJS ng-click 属性的 div,该属性在单击 div 时设置一个变量。
<div id="id"
ng-click="foo.bar = true;">
Set bar variable on foo object to true
</div>
Run Code Online (Sandbox Code Playgroud)
下面是一些使用 Selenium 单击 div 元素的 Java 代码。
By upload = By.id("id");
driver.findElement(uploadCensus).click();
Run Code Online (Sandbox Code Playgroud)
当我运行 Java 代码时,AngularJS 会永远挂起。我认为 foo.bar 在单击 div 时未设置,因此这里是一些直接设置变量的代码。
By upload = By.id("id");
((JavascriptExecutor) driver)
.executeScript("foo.bar = true;",
driver.findElement(upload));
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
未知错误:foo 未定义(会话信息:chrome=56.0.2924.87)(驱动程序信息:chromedriver=2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),平台=7 Windows NT60601616161616161616161616161616161.1.0.0.1.1.1.1.0信息)命令持续时间或超时:51 毫秒构建信息:版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 17:00:58' 系统信息:主机:'WV-VC104- 027', ip: '{ip}', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151' 驱动信息: org. openqa.selenium.chrome.ChromeDriver 功能 [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),userDataDir=C:\Users{user}\AppData\Local\Temp\scoped_dir5600_4225},takesHeapSnapshot=true, database hasEnabledScreen5false,9679f934aab9eed platform=XP,browserConnectionEnabled=false,nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true,webStorageEnabled=true,browserName=chrome,takesScreenshot=true,javascriptEnabled=true,cssSelectorsEnabled=true}] 会话 ID:a7734312eff62fe452a53895b221a58dlocationContextEnabled=true、webStorageEnabled=true、browserName=chrome、takesScreenshot=true、javascriptEnabled=true、cssSelectorsEnabled=true}] 会话 ID:a7734312eff62fe452a53895b221a58dlocationContextEnabled=true、webStorageEnabled=true、browserName=chrome、takesScreenshot=true、javascriptEnabled=true、cssSelectorsEnabled=true}] 会话 ID:a7734312eff62fe452a53895b221a58d
当我尝试设置时,foo.bar
我无法获得对该变量的引用,因为它不是全局定义的,而是隐藏在 AngularJS 源代码中的某处。我试图取消索引并寻找变量,但我似乎找不到它。我想通过 手动设置foo.bar
变量,JavascriptExecutor
但无法获得对变量的引用。我如何找到然后设置变量?
如果这似乎是触发它的错误方式ng-click
,我愿意接受想法。我确信 Protractor 有办法处理这个问题,但这个 AngularJS 应用程序部署在企业环境中,并且几个月来一直试图让业务方批准该技术。我坚持使用硒。帮助...
只需直接回答您的问题即可[不要将其用于您的案例:)]
Angular 有独立的变量作用域。因此您需要获取作用域并在其中设置变量。
angular.element("#id").scope().foo.bar = true;
Run Code Online (Sandbox Code Playgroud)
但不要将其用于您的情况。当您在系统级别测试应用程序时,您必须像用户一样测试它们。
推荐答案:等待java中的角度可测试性
无论 Protractor 能处理什么,您的 java 测试也能做到。两者都是 Selenium 绑定的包装。最终,所有 Selenese 代码都在您测试的同一浏览器中执行。
waitForAngularMethod
是你在测试中错过的最不可避免的方法。好处是您可以在 expilcit 等待中使用 JavaScriptExecutor 执行相同类型的 javascript。
如果你是 JavaScript 爱好者,可以参考Protractor 中waitForAngular方法的实现。这只是一个简单的验证检查并使用回调等待。
用于为 waitforangular 创建预期条件的 java 代码
String waitForAngularJs ="Your javascript goes here"; // as it is lengthy read it from file and store here.
ExpectedCondition<Boolean> waitForAngular= new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeAsyncScript(waitForAngularJs).equals(true);
}
};
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(waitForAngular);
Run Code Online (Sandbox Code Playgroud)
等待 Angular Javascript(针对您的 Java 执行进行了修改。但我没有检查它):
该方法需要两个属性,根定位器挂钩和回调。我已将回调设置为一个简单的函数 return true 并与根定位器挂钩,即[ng-app]
.
var testCallback = function() {
return true;
};
// Wait for angular1 testability first and run waitForAngular2 as a callback
var waitForAngular1 = function(callback) {
if (window.angular) {
var hooks = window.angular.element('[ng-app]');
if (!hooks){
callback(); // not an angular1 app
}
else{
if (hooks.$$testability) {
hooks.$$testability.whenStable(callback);
} else if (hooks.$injector) {
hooks.$injector.get('$browser')
.notifyWhenNoOutstandingRequests(callback);
} else if (!rootSelector) {
throw new Error(
'Could not automatically find injector on page: "' +
window.location.toString() + '". Consider using config.rootEl');
} else {
throw new Error(
'root element (' + rootSelector + ') has no injector.' +
' this may mean it is not inside ng-app.');
}
}
}
else {callback();} // not an angular1 app
};
// Wait for Angular2 testability and then run test callback
var waitForAngular2 = function() {
if (window.getAngularTestability) {
if (rootSelector) {
var testability = null;
var el = document.querySelector(rootSelector);
try{
testability = window.getAngularTestability(el);
}
catch(e){}
if (testability) {
testability.whenStable(testCallback);
return;
}
}
// Didn't specify root element or testability could not be found
// by rootSelector. This may happen in a hybrid app, which could have
// more than one root.
var testabilities = window.getAllAngularTestabilities();
var count = testabilities.length;
// No angular2 testability, this happens when
// going to a hybrid page and going back to a pure angular1 page
if (count === 0) {
testCallback();
return;
}
var decrement = function() {
count--;
if (count === 0) {
testCallback();
}
};
testabilities.forEach(function(testability) {
testability.whenStable(decrement);
});
}
else {testCallback();} // not an angular2 app
};
if (!(window.angular) && !(window.getAngularTestability)) {
// no testability hook
throw new Error(
'both angularJS testability and angular testability are undefined.' +
' This could be either ' +
'because this is a non-angular page or because your test involves ' +
'client-side navigation, which can interfere with Protractor\'s ' +
'bootstrapping. See http://git.io/v4gXM for details');
} else {waitForAngular1(waitForAngular2);} // Wait for angular1 and angular2
// Testability hooks sequentially
Run Code Online (Sandbox Code Playgroud)
为什么我们需要等待背后的角度和逻辑
为什么我们需要这个,因为 Angular 使用 html 模板、双向数据绑定、ajax 请求和路由。因此,在页面导航之后,我们必须等待所有这些操作(请求和承诺)完成。否则 html 将不会像您的情况那样按预期响应。
检查角度可测试性
即检查角度的可测试性。对于这个角度本身提供了一种方法。
要根据元素进行检查,
window.getAngularTestability(el).whenStable(callback); //Here el is element to be valiated.
Run Code Online (Sandbox Code Playgroud)
要检查所有可测试性,
window.getAllAngularTestabilities();
testabilities.forEach(function(testability) {
testability.whenStable(callback);
});
Run Code Online (Sandbox Code Playgroud)
等待所有 http 待处理请求完成
在检查角度可测试性之前,我们可以通过以下几行确保没有待处理的 http 请求。
angular.element(document).injector().get('$http').pendingRequest.length
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
753 次 |
最近记录: |