11

I need to select an element, send values to it, press tab and then send new values.

I can select the element and send values to it but am not being able to send TAB from my keyboard and then send new value.

I used ptor first but then it is being obsoleted, I now am trying to do same by using browser.key but its not working for me.

Please Help !

hny2015
  • 257
  • 2
  • 4
  • 12
  • what do you expect when sending a 'TAB'? normal behaviour would be to focus the element with a greater tab index then the currently focused element. or do you want a 'TAB' as charackter like '\t'? – nilsK Feb 09 '15 at 13:07
  • ohh I want to shift to next field, also I am trying to use ENTER key as well but I couldnt make it work either. I dont know what exactly I should write in my code. I am trying many things but nothing working as such. – hny2015 Feb 09 '15 at 13:15

1 Answers1

20

i wrote a snippet and tested it against google.de (not .com! maybe you have to adjust this) and when sending TAB the next element gets the focus (in this case it's the search button).

the snippet:

describe('Test', function () {
  it('should browse to google', function () {
    browser.ignoreSynchronization = true;
    browser.driver.get('https://www.google.de');
    expect(browser.getCurrentUrl()).toEqual('https://www.google.de/');
  });
  it('should unfocus the search field', function () {
    var search = element(by.name('q'));
    search.sendKeys(protractor.Key.TAB);
    browser.sleep(3000); // 3s to take a look ;)
  });
});
nilsK
  • 4,323
  • 2
  • 26
  • 40
  • ok, this helped me with TAB thing, now since I cannot select my other element which I have mentioned in http://stackoverflow.com/questions/28363238/hard-time-having-finding-element-using-css-or-any-other-locator-in-protractor and now when with this Tab I get focus on new field how can I just send values to it. Like for example username and password fields. I enter username and then uses TAB to get focus on password field now I just want to enter password, how should I do it. I am totally new and may ask stupid questions – hny2015 Feb 09 '15 at 14:28
  • you could just get the password input field and [check if its focused](http://stackoverflow.com/questions/22753496/how-do-i-assert-an-element-is-focused/22756276#22756276) i would suggest reading the [page object docs](https://github.com/angular/protractor/blob/master/docs/page-objects.md), helpes to keep the code clean :) if you need a more specific answer, please open a new thread to keep the board clean too ;) – nilsK Feb 09 '15 at 18:14
  • `browser.get` instead of `browser.driver.get` also works – Dmitri Zaitsev Apr 23 '15 at 15:53
  • Protip to anyone trying to do this so far this week- something with the protractor.Key.TAB isn't working in chrome driver: https://github.com/angular/protractor/issues/2370 – Andrew Luhring Jul 24 '15 at 20:57
  • 1
    You can find a list of the values for `protractor.key` in the source: https://github.com/SeleniumHQ/selenium/blob/master/javascript/webdriver/key.js – Lee Goddard Feb 29 '16 at 12:25