Click() 函数在量角器脚本中不起作用

2023-11-29

我正在尝试使用 Protractor 和 Appium 对 iPad 模拟器中带有 jasmine 框架的 AngularJS 网站进行自动化测试,sendkeys()函数适用于用户名和密码,但是当我单击登录按钮时,测试通过了,但操作未完成:没有重定向到主页,并且登录按钮没有显示单击效果,我确定该元素位置正确!因为当我期望 gettext() 等于“LOGIN”时,它会被传递,但即使我输入也不会重定向browser.sleep(8000);这是我的测试脚本:

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

还有其他方法可以正确定位单击按钮吗? 这是我的 html 代码:

​​<div class="lo​​gin_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>

​​我把Appium记录器关于登录按钮的详细信息

Appium Inspector


造成这种情况的原因可能有多种,而且无论如何这都将是一场猜谜游戏。

  • 可能有另一个元素与.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();
    
  • add a 小延迟在单击该元素之前(很愚蠢,但我发现这有时会有所帮助):

    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();
    
  • 另一个愚蠢的尝试,点击两次(我不敢相信我实际上建议这样做):

    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();
    browser.sleep(500);
    loginButton.click();
    

    或者,如果你愿意介绍一个习惯sleep() action, 你可以做:

    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 显式更改,请参阅:

  • Protractor - 通用等待 URL 更改

作为旁注,在 Protractor 中,您可以使用$ and $$捷径对于 CSS 定位器:

var loginButton = $('.login-form .login-button');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Click() 函数在量角器脚本中不起作用 的相关文章

随机推荐