Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Protractor (Selenium) verify if input is focused

I'm trying to to test whether an element is focused using selenium webdriver in protractor. This is before AngularJS is loaded so I am having to use the driver as seen here:

var ptor = protractor.getInstance(),
    driver = ptor.driver;

I also need to know how to make the test wait until the input is focused. I have to wait until a model is fired so the input is not focused for half a second as seen here:

window.setTimeout(function(){
  $("input#email").focus();
}, 500);

Any idea how to verify if an input has focus after 500ms?

like image 390
Scotty Bollinger Avatar asked Jan 10 '23 22:01

Scotty Bollinger


1 Answers

Based on my answer to this question, and adapting it to your case, it would look like:

it('should focus on foo input', function () {
    // to wait 500ms+
    browser.driver.sleep(600);

    // using the Protractor 'element' helper
    // https://github.com/angular/protractor/blob/master/docs/api.md#element
    // var input = element(by.id('foo'));

    // using findElement with protractor instance
    var input = driver.findElement(protractor.By.id('foo'));

    expect(input.getAttribute('id')).toEqual(browser.driver.switchTo().activeElement().getAttribute('id'));
});
like image 60
glepretre Avatar answered Jan 31 '23 18:01

glepretre