Send keys on div / non input

时间:2015-07-28 22:28:56

标签: javascript angularjs selenium selenium-webdriver protractor

I'm testing a site (in Chrome) that has a "spotlight search" feature like on Macs, where an input shows up when you press a certain key. There is a directive that takes care of the logic for this, and you can type anywhere in the application, as long as it's not an input. I need to test this feature by sending keys through my protractor tests, but I get unknown error: cannot focus element when I target a div. Is it possible to send keys on an element that isn't an input?

1 个答案:

答案 0 :(得分:6)

You would probably need to wait for the element to become visible with a visibilityOf Expected Condition and browser.wait():

var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(by.css("div#myid")), 5000);

var div = element(by.css("div#myid"));
div.sendKeys("test");

Also, you can try utilizing browser.actions():

browser.actions().mouseMove(div).sendKeys('test').perform();

Also, you may need to click the element before sending keys:

div.click().sendKeys("test");