方法中的参数数量
我有填写表格的硒测试。我有一个方法,但这个方法的参数数量已经过多 -
newMerchantPage.addEditMerchant(merchantDomain, merchantName,
merchantCategory, true, merchantDescription, merchantNotes,
merchantTags, true, true, false, false, merchantTitle,
additionalDescription, merchantHeading, dummyCouponLink, true);
只有字符串和布尔值。 我正在考虑使用集合,然后在被调用方法中迭代集合以进行更多处理。尽管还不确定这是否是正确的方法。有什么建议吗?
修改方法:
在实现几个建议后,我的方法(不同方法的)调用看起来像 -
ContactPage contactPage = new ContactPage(driver);
setContactFormData();
contactPage.setName(name).setEmailAddress(emailAddress).setSubject(subject).setM essage(message);
contactPage.submitContactForm(contactPage);
SubmitContactForm 依次调用不同的实用程序方法。看起来有多糟糕?特别是最后一行(对象上的方法调用和作为参数传递的同一对象)?
I have Selenium test which fills a form. I have a method for it but this method has overgrown in terms of number of parameters -
newMerchantPage.addEditMerchant(merchantDomain, merchantName,
merchantCategory, true, merchantDescription, merchantNotes,
merchantTags, true, true, false, false, merchantTitle,
additionalDescription, merchantHeading, dummyCouponLink, true);
There are only Strings and boolean.
I was thinking to use collection and then iterate through collection in called method to do some more processing. Though yet not sure if this is the way to go about. Any recommendations?
MODIFIED METHOD:
After implementing couple of sugggestions my method (of a different method) call looks like -
ContactPage contactPage = new ContactPage(driver);
setContactFormData();
contactPage.setName(name).setEmailAddress(emailAddress).setSubject(subject).setM essage(message);
contactPage.submitContactForm(contactPage);
submitContactForm in turn calls different utility methods. How bad does it look? Especially the last line (method call on object and same object being passed as argument) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种常见的方法是将参数包装在类中。然后,此类可以提供返回
this
的设置方法,以实现良好的链接。 (请参阅ProcessBuilder
< /a> 是一个很好的例子。)示例:
One common approach is to wrap the parameters in a class. This class could then provide set-methods which return
this
to allow for a nice chaining. (SeeProcessBuilder
for a good example.)Example:
我假设您正在使用 Selenium 服务器(或 RC)。
将数据包装到 Merchant 类中的建议都很好并且很有意义,特别是从纯 Java 编码的角度来看。
然而,在 Selenium 中您主要关心的是您正在填写的表单,而不是商家域对象。
也许您可以将您的方法分解为更小的方法,例如
打开商户表格(...)
typeNameInMerchantForm(...)
selectMerchantCategory(...)
等等,具体取决于表单上设置的控件类型。这将反映您正在测试的行为,而不是直接设置域对象等。
希望有所帮助。
I'm assuming that you are using Selenium server (or RC).
The suggestions for wrapping the data up into a Merchant class are all good and make sense, especially from pure Java coding point of view.
However, your main point of concern here in Selenium is the form you are filling, rather that the merchant domain object.
Maybe you could then break your method up into smaller methods such as
openMerchantForm(...)
typeNameInMerchantForm(...)
chooseMerchantCategory(...)
and so on, depending on what type of control is being set on the form. That will reflect the behaviour you are testing rather than setting the domain objects directly etc.
Hope that helps.
也许 - 编写一个类
Merchant
,使用方法值创建一个实例并传递该实例?优点:您可以将测试参数保存在文件中并执行以下操作:
Maybe - write a class
Merchant
, create an instance with the method value and pass the instance instead?The advantage: you can keep the test parameters in files and do something like:
您是否考虑过创建一个类,如果您指定的参数属于同一对象,则将对象作为参数传递。
Have you considered making a class that is if the parameters you specify belong to something same,then pass the object as the parameter.