【问题标题】:Protractor click function isn't always working量角器点击功能并不总是有效
【发布时间】:2017-05-11 14:59:24
【问题描述】:

我是量角器的新手。我已经为 Angular 编写了一个测试,该测试首先用户登录到应用程序,然后单击特定按钮来计算一个数字。有时有效,有时无效!

这是代码:

describe('Protractor Demo App', function () {

    it('should login to app', function () {
        browser.get('http://localhost:8080/main');
        var userName = element(by.id('username'));
        var passWord = element(by.id('pwd'));
        var loginBtn = element(by.id('loginBtn'));
        userName.sendKeys('user');
        passWord.sendKeys('1');
        loginBtn.click();
        expect(browser.getCurrentUrl()).toEqual('http://localhost:8080/main/#/intro');
    });


   it('should calculate something', function () {
        browser.get('http://localhost:8080/main/#/something');
        var next = element(by.css('[ng-click="calculate()"]'));
        element(by.model('accountNo')).sendKeys('0293949223008');
        next.click();  
        expect(element(by.binding('result')).getText()).toEqual('55017000000029');
    });

    it('should show error for invalid input no', function () {
        browser.get('http://localhost:8080/main/#/something');
        var next = element(by.css('[ng-click="calculate()"]'));
        element(by.model('accountNo')).sendKeys('9999456123789');
        next.click();
        expect(element(by.css('[class="messageObject warning"]')).getText()).toEqual('message for invalid input');
    });
});

第一个“它”总是有效,但第二个和第三个,有时有效。碰巧只有一个没有工作;

这是错误:

消息: 失败:未知错误: 元素 ... 在点 (1214, 275)。其他元素会收到点击:... (会话信息:chrome=55.0.2883.87) (驱动信息: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 10.0.10240 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:78 毫秒 构建信息:版本:'2.53.1',修订:'a36b8b1',时间:'2016-06-30 17:37:03' 系统信息:主机:'user',ip:'ip',os.name:'Windows 8.1',os.arch:'amd64',os.version:'6.3',java.version:'1.8.0_45' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, 铬={铬驱动程序版本=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30), userDataDir='add'}, 需要HeapSnapshot=true,pageLoadStrategy=正常, databaseEnabled=false,handlesAlerts=true,hasTouchScreen=false, 版本=55.0.2883.87,平台=WIN8_1,browserConnectionEnabled=false, nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true, webStorageEnabled=true,browserName=chrome,takeScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, 意外警报行为=}] 会话 ID:45d78fbebf15daa1dde971f5f7470551

我不知道问题是什么。谁能帮我? 提前致谢。

【问题讨论】:

    标签: javascript angularjs jasmine protractor end-to-end


    【解决方案1】:

    在执行点击操作之前使用 ExpectedConditions.isElementToBeClickable() 方法实现 browser.wait()。您按照以下方式进行操作:

       var EC = protractor.ExpectedConditions;
    
     it('should show error for invalid input no', function (){
        browser.get('http://localhost:8080/main/#/something');
        var next = element(by.css('[ng-click="calculate()"]'));
        element(by.model('accountNo')).sendKeys('9999456123789');
    
        browser.wait(EC.elementToBeClickable(next ), 10000,'Element not  
                                                      clickable');
        next.click();
        expect(element(by.css('[class="messageObject  
        warning"]')).getText()).toEqual('message for invalid input');
       });
    

    【讨论】:

      【解决方案2】:

      这是对 Suresh Salloju 上述回答的扩展,解决方案是使用 Protractor 的 API 等待元素可点击,如下所示:

      browser.wait(EC.elementToBeClickable(element), 10000,'Element not clickable');
      

      在某些情况下,即使在等待elementToBeClickable 时也会发生类似的异常。

      在我的例子中,在某些屏幕尺寸下,一个 toast 元素覆盖了问题中的元素 - 所以虽然它是可点击的,但它从未收到点击。

      【讨论】:

        【解决方案3】:

        作为最后的手段,您可以使用 javascript 点击。请看下面的测试代码。

        var homelink= element(by.linkText('Home')).click(); browser.executeScript("arguments[0].click();", homelink);

        【讨论】:

          猜你喜欢
          • 2015-10-21
          • 2011-06-18
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          • 1970-01-01
          • 2017-11-21
          • 2016-05-10
          • 2012-11-17
          相关资源
          最近更新 更多