方法中的参数数量

发布于 2024-12-04 14:59:04 字数 797 浏览 0 评论 0原文

我有填写表格的硒测试。我有一个方法,但这个方法的参数数量已经过多 -

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

家住魔仙堡 2024-12-11 14:59:04

一种常见的方法是将参数包装在类中。然后,此类可以提供返回 this 的设置方法,以实现良好的链接。 (请参阅ProcessBuilder< /a> 是一个很好的例子。)

示例:

MerchantData data = new MerchantData();   // initialize with sensible defaults

data.setDomain("example.com")
    .setName("Some Name")
    .setTags("tag1, tag2);

newMerchantPage.addEditMerchant(data);

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. (See ProcessBuilder for a good example.)

Example:

MerchantData data = new MerchantData();   // initialize with sensible defaults

data.setDomain("example.com")
    .setName("Some Name")
    .setTags("tag1, tag2);

newMerchantPage.addEditMerchant(data);
第七度阳光i 2024-12-11 14:59:04

我假设您正在使用 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.

紧拥背影 2024-12-11 14:59:04

也许 - 编写一个类Merchant,使用方法值创建一个实例并传递该实例?

newMerchantPage(Merchant merchant);

优点:您可以将测试参数保存在文件中并执行以下操作:

Merchant merchant = new Merchant();
merchant.populate(File testdata, int record);

Maybe - write a class Merchant, create an instance with the method value and pass the instance instead?

newMerchantPage(Merchant merchant);

The advantage: you can keep the test parameters in files and do something like:

Merchant merchant = new Merchant();
merchant.populate(File testdata, int record);
扎心 2024-12-11 14:59:04

您是否考虑过创建一个类,如果您指定的参数属于同一对象,则将对象作为参数传递。

Have you considered making a class that is if the parameters you specify belong to something same,then pass the object as the parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文