简体   繁体   English

硒e2e测试,更具体

[英]Selenium e2e tests, be more specific

I am using Selenium WebDriver and Protractor to run e2e tests on my angular project. 我正在使用Selenium WebDriver和Protractor在我的角度项目上运行e2e测试。 I am having a lot of trouble finding a detailed API that will help me understand how to use the driver. 我在查找详细API以帮助我了解如何使用驱动程序时遇到了很多麻烦。 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. 它们都有一个绑定到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 找到定位器by.model(“user.username”)的多个元素 - 您可能需要更具体

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. 在我尝试测试寄存器之前这不是问题,在这种情况下我不知道如何告诉它选择第二个'user.username'输入。

I tried looking through this page: https://github.com/angular/protractor/blob/master/docs/api.md 我试着浏览这个页面: https//github.com/angular/protractor/blob/master/docs/api.md

And this page: http://docs.seleniumhq.org/docs/03_webdriver.jsp 这个页面: 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". 但我似乎无法找到更全面的API,只能简单解释如何进行更复杂的“选择”。

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. 但这仍然无法帮助我,因为我正在为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. 不确定这是否严格正确,但它不会太远,基本上使用findElements获取具有相同标识符的所有元素并将它们保存为数组。 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() 编辑:我的测试代码中的原因我有findElement是因为我需要更深入地获取我正在寻找的确切元素但我可以刚刚完成.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 另一个编辑:至于更详细的文档,这是量角器的一个问题,但纯粹是因为团队专注于启动和运行添加新功能/修复错误等,我必须说,虽然我想要的大多数信息,我发现无论是在这里还是在github朱莉(不确定第二个名字,是一个量角器信息godess)并定期发布在这里和github帮助人们,这对我来说已经足够了

select.model('user.username') is just one method of "locating" an element in the DOM, a Protractor-specific method. select.model('user.username')只是一种“定位”DOM中元素的方法,这是一种特定于Protractor的方法。 WebDriver has a number of locator strategies which you may find more flexible and, in your case, more specific. WebDriver有许多定位策略,您可能会发现它们更灵活,在您的情况下,更具体。

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 Protractor规范的这一部分说明了可用的定位器机制: 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.id('username')).sendKeys('nathanadmin'); element(by.css('#username')).sendKeys('nathanadmin');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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