Click() 函数在量角器脚本中不起作用
[英] Click() function isn't working in protractor scripts
本文介绍了Click() 函数在量角器脚本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Protractor 和 Appium 为 iPad 模拟器中带有 jasmine 框架的 AngularJS 站点自动化我的测试,sendkeys()
函数适用于用户名和密码,但是当我点击进入登录按钮 测试通过,但操作未完成:没有重定向到主页,也没有显示登录按钮的点击效果,我确定该元素的位置正确!因为当我期望 gettext() 等于LOGIN"时它通过但没有重定向,即使我把 browser.sleep(8000);
这是我的测试脚本:
严格使用";要求(茉莉花期望");var wd = require(wd");描述('我的应用程序',函数(){it('应该进行登录测试',function() {//browser.ignoresynchronization=true;browser.get(http://10.0.22.82:8080/jws/fetablet");期望(browser.getCurrentUrl()).toEqual(("http://10.0.22.82:8080/jws/fetablet/#/login"));element(by.model('credentials.username')).sendKeys('RET02').then(function(){element(by.model('credentials.password')).sendKeys('RET02').then(function(){element(by.css('.login-button')).click().then(function(){浏览器睡眠(8000);expect(browser.getCurrentUrl()).not.toEqual("http://10.0.22.82:8080/jws/fetablet/#/login");});});});});});
是否有另一种方法可以正确定位点击按钮?这是我的 html 代码:
<md-button class="lang_button";ng-click="changeLang()">{{lang}}
<div layout="column"flex layout-align="center center"class="md-padding splash-background"><div class="login-logo"><img src="{{logoSrc}}">
<form class="login-form";名称=登录表单"ng-submit="登录()"><字段集><md-input-container class="md-block"><label translate="login.USERNAME";ng-class="{'floating-label-rtl':dir==='rtl'}";class="login-label">用户名<input required ng-model="credentials.username";ng-focus="onFocus()";类型=文本"><div ng-messages="loginForm.credentials.username.$error";ng-show=loginForm.credentials.username.$dirty"><div ng-message=必需"反式晚=login.MESSAGE_REQUIRED">这是必需的.</div>
</md-input-container><md-input-container class="md-block"><label translate="login.PASSWORD";ng-class="{'floating-label-rtl':dir==='rtl'}";class="login-label">密码</label><input required ng-model="credentials.password";ng-focus="onFocus()";类型=pa提供ssword"><div ng-messages="loginForm.credentials.password.$error";ng-show=loginForm.credentials.password.$dirty"><div ng-message=必需"translate=login.MESSAGE_REQUIRED">这是必需的.</div>
</md-input-container><div layout-align="center center"布局=列"ng-if=oneTimePassword"><p class="login-otp-message";translate=login.OTP_MESSAGE">输入您通过短信收到的代码</p><md-button class="md-warn login-otp-retry";翻译=登录.OTP_RETRY";ng-click="retry()">Retry</md-button>
<md-input-container class="md-block";ng-if=oneTimePassword"><label translate="login.SECURITY_CODE";class="login-label">安全代码</label><input required ng-model="credentials.securityCode";ng-focus="onFocus()";类型=密码"><div ng-messages="loginForm.credentials.securityCode.$error";ng-show=loginForm.credentials.securityCode.$dirty"><div ng-message=必需"translate=login.MESSAGE_REQUIRED">这是必需的.</div>
</md-input-container><div layout-align="center"><section layout-align="center";布局=行"layout-sm=列"><div id="登录错误";md-caption class="msg-error";ng-show=错误"class="label">{{error}}
<md-button type="submit";class="md-raised login-button";ng-disabled=点击"translate=login.LOGIN">登录</md-button></节>
</fieldset></表单><md-divider></md-divider><页脚类=登录页脚"><div layout="row";layout-align="center center"><md-button ng-click="goToCustomerCare()";class =登录页脚链接"translate="login.CUSTOMER_CARE">联系客户服务</md-button><div>|
<md-button ng-click="showDisclaimer()";class =登录页脚链接"translate="login.DISCLAIMER">Disclaimer</md-button>
</页脚>
我把Appium记录器关于登录按钮的细节放了
解决方案
造成这种情况的原因可能有多种,无论如何这将是一场猜谜游戏.
可能是有另一个元素与 .login-button
定位符匹配,而您正在单击不同的元素.让我们改进定位器:
element(by.css(".login-form .login-button")).click();
等待元素可点击:
var EC = protractor.ExpectedConditions;element(by.model('credentials.username')).sendKeys('RET02');element(by.model('credentials.password')).sendKeys('RET02');var loginButton = element(by.css('.login-form .login-button'));browser.wait(EC.elementToBeClickable(loginButton), 5000);loginButton.click();
在点击元素之前添加一个小延迟(愚蠢,但我认为有时会有所帮助):
element(by.model('credentials.username')).sendKeys('RET02');element(by.model('credentials.password')).sendKeys('RET02');浏览器睡眠(500);element(by.css('.login-form .login-button')).click();
另一个愚蠢的尝试,点击 2 次(我不敢相信我真的建议这样做):
var loginButton = element(by.css('.login-form .login-button'));loginButton.click();loginButton.click();
禁用角度动画
通过点击按钮browser.actions()
移动到点击前的元素:
var loginButton = element(by.css('.login-form .login-button'));browser.actions().mouseMove(loginButton).click().perform();
某种对之前方法的扩展.移动到元素,休眠半秒,然后点击:
browser.actions.mouseMove(loginButton).perform();浏览器睡眠(500);loginButton.click();
或者,如果您要引入自定义 sleep()
操作,你可以这样做:
browser.actions.mouseMove(loginButton).sleep(500).click().perform();
点击元素通过javascript:
var loginButton = element(by.css('.login-form .login-button'));browser.executeScript("arguments[0].click();", loginButton);
而且,表单提交后,可以不用browser.sleep()
,而是等待URL显式改变,请看:
<小时>作为旁注,在量角器中,您使用 $
和 $$
快捷键用于 CSS 定位器:
var loginButton = $('.login-form .login-button');
I'm trying to automate my tests with Protractor and Appium for an AngularJS site with jasmine framework in iPad simulator, sendkeys()
function is working for username and password, but when i click into the login button the test is passed, but the action isn't done : no redirection to home page, and no on click effect is displayed for login button, i'm sure that element is located correctly ! because when i expect the gettext() to be equal to"LOGIN" it is passed but no redirection even if i put browser.sleep(8000);
Here my test script :
"use strict";
require("jasmine-expect");
var wd = require("wd");
describe('my app', function() {
it('should make the login test',function() {
// browser.ignoresynchronization=true;
browser.get("http://10.0.22.82:8080/jws/fetablet");
expect(browser.getCurrentUrl()).toEqual(("http://10.0.22.82:8080/jws/fetablet/#/login"));
element(by.model('credentials.username')).sendKeys('RET02').then(function(){
element(by.model('credentials.password')).sendKeys('RET02').then(function(){
element(by.css('.login-button')).click().then(function(){
browser.sleep(8000); expect(browser.getCurrentUrl()).not.toEqual("http://10.0.22.82:8080/jws/fetablet/#/login");
});
});
});
});
});
Is there another method to locate the click button correctly?
Here my html code :
<div class="login_lang"> <md-button class="lang_button" ng-click="changeLang()">{{lang}}</md-button> </div>
<div layout="column" flex layout-align="center center" class="md-padding splash-background"> <div class="login-logo"> <img src="{{logoSrc}}"> </div> <form class="login-form" name="loginForm" ng-submit="login()">
<fieldset> <md-input-container class="md-block">
<label translate="login.USERNAME" ng-class="{'floating-label-rtl':dir==='rtl'}"
class="login-label">Username</label>
<input required ng-model="credentials.username" ng-focus="onFocus()" type="text">
<div ng-messages="loginForm.credentials.username.$error" ng-show="loginForm.credentials.username.$dirty">
<div ng-message="required" trans
late="login.MESSAGE_REQUIRED">This is required.</div> </div> </md-input-container> <md-input-container class="md-block"> <label translate="login.PASSWORD" ng-class="{'floating-label-rtl':dir==='rtl'}"
class="login-label">Password</label> <input required ng-model="credentials.password" ng-focus="onFocus()" type="pa
ssword">
<div ng-messages="loginForm.credentials.password.$error" ng-show="loginForm.credentials.password.$dirty"> <div ng-message="required" translate="login.MESSAGE_REQUIRED">This is required.</div> </div> </md-input-container>
<div layout-align="center center" layout="column" ng-if="oneTimePassword"> <p class="login-otp-message" translate="login.OTP_MESSAGE">Enter the code which you received by SMS</p> <md-button class="md-warn login-otp-retry" translate="login.OTP_RETRY" ng-click="retry()">Retry</md-button> </div> <md-input-container class="md-block" ng-if="oneTimePassword"> <label translate="login.SECURITY_CODE" class="login-label">Security code</label> <input required ng-model="credentials.securityCode" ng-focus="onFocus()" type="password"> <div ng-messages="loginForm.credentials.securityCode.$error" ng-show="loginForm.credentials.securityCode.$dirty"> <div ng-message="required" translate="login.MESSAGE_REQUIRED">This is required.</div> </div> </md-input-container> <div layout-align="center"> <section layout-align="center" layout="row" layout-sm="column"> <div id="login-error" md-caption class="msg-error" ng-show="error" class="label">{{error}}</div>
<md-button type="submit" class="md-raised login-button" ng-disabled="clicked" translate="login.LOGIN">Login</md-button> </section>
</div> </fieldset> </form> <md-divider></md-divider> <footer class="login-footer"> <div layout="row" layout-align="center center"> <md-button ng-click="goToCustomerCare()" class="login-footer-link" translate="login.CUSTOMER_CARE">Contact Customer Care</md-button> <div> | </div> <md-button ng-click="showDisclaimer()" class="login-footer-link" translate="login.DISCLAIMER">Disclaimer</md-button> </div> </footer> </div>
I put the details of Appium recorder about the login button
解决方案
There might be multiple reasons for that and it is going to be a guessing game anyway.
it could be that there is an another element matching the .login-button
locator and you are clicking a different element. Let's improve the locator:
element(by.css(".login-form .login-button")).click();
wait for the element to be clickable:
var EC = protractor.ExpectedConditions;
element(by.model('credentials.username')).sendKeys('RET02');
element(by.model('credentials.password')).sendKeys('RET02');
var loginButton = element(by.css('.login-form .login-button'));
browser.wait(EC.elementToBeClickable(loginButton), 5000);
loginButton.click();
add a small delay before clicking the element (silly, but I see that helped sometimes):
element(by.model('credentials.username')).sendKeys('RET02');
element(by.model('credentials.password')).sendKeys('RET02');
browser.sleep(500);
element(by.css('.login-form .login-button')).click();
another silly try, click 2 times (I cannot believe I actually advise that):
var loginButton = element(by.css('.login-form .login-button'));
loginButton.click();
loginButton.click();
disable angular animations
click the button via browser.actions()
moving to the element before the click:
var loginButton = element(by.css('.login-form .login-button'));
browser.actions().mouseMove(loginButton).click().perform();
sort of an extension to the previous approach. Move to element, sleep for half a second and then click:
browser.actions.mouseMove(loginButton).perform();
browser.sleep(500);
loginButton.click();
Or, if you would introduce a custom sleep()
action, you can do:
browser.actions.mouseMove(loginButton).sleep(500).click().perform();
click the element via javascript:
var loginButton = element(by.css('.login-form .login-button'));
browser.executeScript("arguments[0].click();", loginButton);
And, after the form is submitted, instead of browser.sleep()
, you can wait for URL to change explicitly, please see:
As a side note, in Protractor, you use the $
and $$
shortcuts for the CSS locators:
var loginButton = $('.login-form .login-button');
这篇关于Click() 函数在量角器脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文