问题
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?
回答1:
For parameterization Testng provide you two methods.
- By TestNG XML file.
- 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.
回答2:
- It is preferred to keep your @Test methods without any parameters.But to answer, you can parametrize your tests as explained here Parametrized tests in TestNG
- If you are trying to pass 100 different login details, would advice you to keep it in separate spreadsheet.
来源:https://stackoverflow.com/questions/32602762/parameterization-in-selenium-and-testng