简体   繁体   English

使用 Scenario Outlines 和 Ruby 验证不同字段中的消息

[英]Validate messages in different fields with Scenario Outlines and Ruby

I have a big problem: with all invalid Login messages appearing in a single field, I can validate it by doing this: I'm going to map the only element where the message's invalid alert appears and use expect to validate the Scenario Outlines message X the message extracted from the element's text.我有一个大问题:所有无效的登录消息都出现在一个字段中,我可以通过这样做来验证它:我要去 map 消息的无效警报出现的唯一元素,并使用 expect 来验证场景大纲消息 X从元素的文本中提取的消息。

msg_invalid = screen_login.msg_erro_login.text
  expect(msg_invalid).to eql(msgs)

  
   What if the messages appear in different fields like the attached image?

       
        Given that the user is on the login page
        When providing incorrect email "<email_login>" or password "<pswd_login>"
        Then he can see the alert message "<msgs>"

Examples:
        | email_login             |  pswd_login   |     msgs                   | 
        |                         |               | This is a required field.  | 
        |staulik@b2card.com       |               | This is a required field.  | 
        |                         | test123       | This is a required field.  | 
        |staulikb2card.com        |               | Please enter a valid email address (Ex: johndoe@domain.com). 

在此处输入图像描述 | |

TL;DR长话短说

Since the source for your HTML, CSS, JavaScript, and model/controller weren't provided, it's hard to guess what your real problem is.由于未提供您的 HTML、CSS、JavaScript 和型号/控制器的来源,因此很难猜测您真正的问题是什么。 However, you've given enough information to suggest that you're going about this particular set of tests sub-optimally, and would be better off refactoring the tests or modifying either the login implementation or the user interface to be more testable.但是,您已经提供了足够的信息来表明您正在以次优的方式进行这组特定的测试,最好重构测试或修改登录实现或用户界面以使其更具可测试性。

NB: This is a good example of why test-first development is useful.注意:这是一个很好的例子,说明为什么测试先行的开发是有用的。 Retrofitting tests onto existing code is harder than making sure the testability is baked in from the beginning!将测试改造到现有代码上比确保从一开始就融入可测试性更难!

What to Validate and Where to Validate It验证什么以及在哪里验证

This isn't really a Cucumber outline problem.这实际上不是 Cucumber 大纲问题。 The issue is most likely with the steps of your test, or underlying UI issues.该问题很可能与您的测试步骤或底层 UI 问题有关。 There's a strong likelihood that you either aren't using unique CSS ID's or other consistent and easy-to-locate information related to your flash messages if the validation is being done server-side, or that you aren't properly driving the web page if you're doing validation in JavaScript and manipulating the DOM to insert your error messages.如果验证是在服务器端完成的,则很有可能您没有使用唯一的 CSS ID 或其他与您的 flash 消息相关的一致且易于定位的信息,或者您没有正确驱动 web 页面如果您在 JavaScript 中进行验证并操作 DOM 以插入错误消息。

Furthermore, the UI itself isn't the ideal place to test anything other than valid or invalid logins.此外,UI 本身并不是测试除有效或无效登录之外的任何内容的理想场所。 If you want to know why a login is invalid, for security reasons you generally want to do the more granular testing in the model or controller tests if you want to know why a given login failed.如果您想知道登录无效的原因,出于安全原因,如果您想知道给定登录失败的原因,您通常希望在 model 或 controller 测试中进行更精细的测试。 You should never return things like "incorrect password" or "invalid username" (as opposed to "invalid login") that leaks information about whether a given account even exists.您永远不应该返回诸如“不正确的密码”或“无效的用户名”(与“无效的登录”相对)之类的东西,这些东西会泄露有关给定帐户是否存在的信息。

Additionally, email validation is generally a waste of time.此外,email验证通常是浪费时间。 While there are some very complex regular expressions that come close—see this Perl regex for RFC 822 addresses as an example—email addressing was never designed to conform to regular expression patterns.虽然有一些非常复杂的正则表达式非常接近——请参阅此Perl 正则表达式作为 RFC 822 地址的示例——电子邮件寻址从未设计为符合正则表达式模式。 Furthermore, most people implement email validations badly, and naive validations often won't accept perfectly valid email addresses such as plus-addressing or qmail-style dash extensions.此外,大多数人错误地实施了 email 验证,并且天真的验证通常不会接受完全有效的 email 地址,例如 plus-addressing 或 qmail-style dash 扩展。 Additionally, even putting in a "valid" email address that matches RFC 822, RFC 5822, or whatever RFC you're trying to validate against can't actually validate that it's a deliverable email anyway.此外,即使输入与 RFC 822、RFC 5822 或您尝试验证的任何 RFC 相匹配的“有效”地址 email,也无法实际验证它是否为可交付物email。

Given the above, you should most likely just validate that both fields are non-empty before allowing the Sign-In button to be clickable.鉴于上述情况,您很可能应该在允许单击登录按钮之前验证这两个字段是否为空。 That seems to be the main check you want to do anyway.无论如何,这似乎是您想要做的主要检查。 Otherwise, just check the page for some invariant "invalid login" text provided by the flash or whatever you're using to manipulate the DOM during text entry or when validating logins.否则,只需检查页面中 flash 提供的一些不变的“无效登录”文本,或者您在文本输入或验证登录时用来操作 DOM 的任何内容。

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

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