量角器点击功能并不总是有效

Protractor click function isn't always working

我是量角器的新手。我已经为 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');
    });
});

第一个 "it" 始终有效,但第二个和第三个有时有效。刚好只有一个没用;

这是错误:

message: Failed: unknown error: Element ... is not clickable at point (1214, 275). Other element would receive the click: ... (Session info: chrome=55.0.2883.87) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 78 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03' System info: host: 'user', ip: 'ip', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30), userDataDir='add'}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] Session ID: 45d78fbebf15daa1dde971f5f7470551

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

在执行点击操作之前用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');
   });

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

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

在某些情况下,即使在等待 elementToBeClickable.

时也会出现类似的异常

就我而言,在某些屏幕尺寸下,toast 元素覆盖了问题中的元素 - 因此尽管它是可点击的,但从未收到点击。

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

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