如何找到指令并使用量角器调用ng-click?

时间:2015-10-08 10:47:34

标签: angularjs testing protractor

我应该找到指令tile-mini并在ng-click中调用该函数。我该怎么办?

HTML:

<tile-mini data="tableTile" ng-click="addDenuncia()" id="add-denuncia-tile"></tile-mini>

spec.js:

it('Inserimento DU', function() {
  browser.get('/#/valoritalia/admin/denunce-uva/inserimentoDU/all');
  element(by.model('autodichiarazione.viticoltore')).sendKeys('erbaluna 034').sendKeys(protractor.Key.ENTER);
  element(by.css(".button-next")).click();
  element(by.directive("tile-mini")).click();
  browser.pause();
  expect(browser.getTitle()).toBe('Inserimento DU DIONISO');
});

我尝试过:

element(by.id("add-complaint-tile")).click ();
element(by.css('[ng-click = "addDenuncia()"]')).click()
directive.element(by.<something>); // Failed: Object [object Object] has no method 'directive'

这些都不起作用..

编辑1

var elementToFind = by.id("add-denuncia-tile");
browser.wait(function() {
  return browser.isElementPresent(element(elementToFind));
}, 30000).then(function(isPresent){
  if(isPresent){
    element(elementToFind).click();
  }
});

错误:

1) Dioniso Test Case Inserimento DU
  Message:
    Failed: element not visible
      (Session info: chrome=45.0.2454.101)
      (Driver info: chromedriver=2.19.346063 (38b35413bd4a486d436a9749e090454bc9ff6708),platform=Mac OS X 10.11.0 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 19 milliseconds
    Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
    System info: host: 'YoBre-work.local', ip: '192.168.1.51', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11', java.version: '1.8.0'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/7s/f_15rz4d7_d7t6rl6yxkbsk80000gn/T/.org.chromium.Chromium.uHFDeg}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=45.0.2454.101, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: 7e22695fbe8a4a29cda083576bf34439

1 个答案:

答案 0 :(得分:0)

您应该尝试单击元素并将其余部分留给量角器以调用ng-click上的函数。此外,量角器中没有directive对象可以使用。因此,您可以使用css或其他元素定位器来单击元素。这是怎样的 -

element(by.id('add-denuncia-tile')).click(); //Notice the id of the element which is typo in your question

如果仍未点击,请确保在点击之前加载并启用了该元素。您可以在量角器中使用elementToBeClickable等待元素 -

var elem = element(by.css('[ng-click = "addDenuncia()"]'));
browser.wait(protractor.ExpectedConditions.elementToBeClickable(elem), 10000).then(function(){
     elem.click();
});

希望有所帮助