简体   繁体   中英

Selenium e2e tests, be more specific

I am using Selenium WebDriver and Protractor to run e2e tests on my angular project. I am having a lot of trouble finding a detailed API that will help me understand how to use the driver. My specific problem is as follows:

I have two controllers on one page, a login controller, and a register controller. They both have an input bound to user.username. To test login, I use the code:

element(select.model('user.username')).sendKeys('nathanadmin');

And I get the warning: warning:

more than one element found for locator by.model("user.username")- you may need to be more specific

Which isn't a problem until I try to test register, in which case I don't know how to tell it to select the second 'user.username' input.

I tried looking through this page: https://github.com/angular/protractor/blob/master/docs/api.md

And this page: http://docs.seleniumhq.org/docs/03_webdriver.jsp

But I can't seem to find a more comprehensive API with a simple explanation of how to do more complex "selecting".

EDIT:

I think something like this would be helpful:

element(select.model('user.username').first()).sendKeys('nathanadmin');

What I came up with was:

element.all(select.model('user.username')).then(function(elements) { elements[0].sendKeys('nathanadmin'); });

But this still does not help me, since I am looking for a more in-depth documentation for webdriver.

you could try using something like

 ptor.findElements(protractor.By.model('user.username')).then(function(models){
    models[0].sendKeys('nathanadmin');
    models[1].sendKeys('secondstring');
  });

not sure if thats strictly correct but it wont be far off, basically using findElements gets all elements with the same identifier and saves them as an array. i have something like this in my tests which works

 visibleFilters.findElements(protractor.By.className('dropdown')).then(function(dropdowns){
      dropdowns[2].findElement(protractor.By.id(filterID)).click();

hope this helps

EDIT: the reason in the code from my test i have findElement is because i need to go deeper to get the exact element im looking for but i could have just done .click()

ANOTHER EDIT: as for more detailed documentation, this is a problem with protractor but its purely because the team have focused on getting it up and running adding new features/fixing bugs etc, i must say though most information that i have wanted i have found either on here or on github Julie (not sure of second name, is a protractor information godess) and regularly posts on here and github helping people out, which has been enough for me

select.model('user.username') is just one method of "locating" an element in the DOM, a Protractor-specific method. WebDriver has a number of locator strategies which you may find more flexible and, in your case, more specific.

This section of the Protractor spec illustrates the locator mechanisms made available to you: https://github.com/angular/protractor/blob/master/docs/api.md#locator-strategies

Consider one of these alternative strategies to isolate a single input element: element(by.id('username')).sendKeys('nathanadmin'); element(by.css('#username')).sendKeys('nathanadmin');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM