Selenium 和 TestNG 中的参数化 [英] Parameterization in Selenium and TestNG

查看:34
本文介绍了Selenium 和 TestNG 中的参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自己学习 Selenium.我不确定在使用 Selenium 和 TestNG 框架时参数化测试用例的正确方法.是在 TestNG.XML 文件中传递参数还是保留一个单独的电子表格更好.例如,我需要使用 100 个不同用户的登录详细信息来参数化 LOGIN 功能.100 个用户的用户名/密码详细信息在哪里保存?

如果通过电子表格进行参数化效果最好,那么拥有 testng.xml 文件的目的究竟是什么?

解决方案

对于参数化 Testng 为您提供两种方法.

<块引用>

  1. 通过 TestNG XML 文件.
  2. 通过使用@DataProvider

何时使用 TestNG XML 文件?

当您想为决策定义简单的参数时,您可以使用 XML 文件.

示例: 简单地从 .xml 文件传递​​浏览器值,如 ff、chrome,即决定用于测试用例执行的浏览器.

何时使用@DataProvider?

特别是在数据驱动测试中,即当需要使用不同的数据集多次运行相同的测试方法时,您可以使用 DataProvider,它也用于处理复杂的参数或复杂的对象,例如从属性文件或数据库中读取,等

  • 在您的情况下,需要传递大约 100 个不同用户的登录详细信息,因此我建议将它们放在 excel 文件中并使用 APACHE POI 读取它们/JXL 不使用 @DataProvider 的原因在逻辑上是不好的练习在您的 DataProvider 类中传递 100 个值,这也可能导致性能不佳并增加测试的复杂性代码.

I am learning Selenium by my own. I am not sure about the right way to parameterize the test cases while using Selenium with TestNG Framework. Is it better to pass the parameters in TestNG.XML file OR keep a seperate spreadsheet. For example, I need to parameterize LOGIN functionality with 100 different users' login details. Where to keep the username/password details of 100 users?

If Parametrization through a spreadsheet works best, then what exactly is the purpose of having a testng.xml file?

解决方案

For parameterization Testng provide you two methods.

  1. By TestNG XML file.
  2. By Using @DataProvider

When to use TestNG XML file?

When you want to define simple parameters for decision making you can use XML file.

Example: Simply passing browser value like ff, chrome from .xml file ie to decide the browser to be used for test case execution.

When to use @DataProvider?

Specially in Data driven testing i.e when same test method needs to be run multiple times with different data-sets you can use DataProvider and it is also used to handle complex parameters or complex objects like reading from a property file or a database, etc.

  • In your case around 100 different users' login details needs to be passed so i would suggest put them in excel file and read them using APACHE POI / JXL reason why not to go for @DataProvider is logically its bad practice to pass 100 values in your DataProvider class which may also result in poor performance and increased the complexity of your test code.

这篇关于Selenium 和 TestNG 中的参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆