JavaScript handler (Selenium) checks if the input is configured

I am trying to check if an element is focused using selenium webdriver in protractor. This is before loading AngularJS, so I need to use the driver as shown here:

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

I also need to know how to do the test until the input is focused. I need to wait until the model is launched, so the input will not be focused for half a second, as shown here:

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

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

+4
source share
2 answers

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

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'));
});
+8

glepretre, getAttribute promises prom.all

let activeElement = browser.driver.switchTo().activeElement().getAttribute('id');
let compareElement = element(by.id('your-element-id')).getAttribute('id'); 
webdriver.promise.all([compareElement, activeElement]).then((id) => {
    expect(id[0]).to.equal(id[1]);
});
0

Source: https://habr.com/ru/post/1533811/


All Articles