1

I am working in selenium with page object design pattern. I have got a feedback about my code and I want to make my code better.

Code Review

"You need to keep the test data separately. Not scattering all over the script. Keep the test data in JSON. And read it from there where ever it is necessary."

Test Data

Test data should be separate from the rest of the automation code. The reason is this: if you do not separate your test data, as you add new tests you will duplicate field identifiers and the data to enter. When there is a change to the code, you will have a maintenance nightmare.

Also, if your test data is in a separate, non-compiled format, it's a lot easier for other people to add new tests. If the data format for each type of test is clearly defined, all that's needed to add an iteration of a test is to edit a text file and add a new row of data.

Anybody, please show an example? And above mentioned test data is using with the data provider, is it right?

closed as unclear what you're asking by Rsf, NarendraC, IAmMilinPatel, Shailendra Rathore, Peter M. Jan 17 '17 at 16:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • what is the question ? – Rsf Jan 17 '17 at 8:51
  • @Rsf I need an example of how to keep test data separatley? – toastmaster Jan 17 '17 at 8:52
  • You might do some research on "data driven" testing or "data driving" tests, also similar terms. – AdrianHHH Jan 17 '17 at 9:21
  • @AdrianHHH Excuse me So i should learn data driven testing testing it is selenium framework right? please tell me the importance of learning JSON – toastmaster Jan 17 '17 at 9:29
  • @toastmaster I did not write "should", I wrote "might". Your review said it should use JSON. Data driven testing is applicable to many (perhaps all) testing systems and it can be done with JSON or with several other data formats (eg CSV, Spreadsheet, Database, plain text) depending on the needs and the working practices of the company. – AdrianHHH Jan 17 '17 at 9:40
2

"You need to keep the test data separately. Not scattering all over the script. Keep the test data in Json. And read it from there where ever it is necessary."

It depends.

Are your test data bound to the test environment and system under test? For instance, if you are reusing in your tests data that are defined in the system you test, then it's good to keep them in one place. This makes maintenance easier, e.g., when you want actual test user gets removed from the system, then you have only one place to update. It also makes easier to reuse same test data across different tests, e.g., same test user. However, if each of your tests is using different test data and those data are not bound to the system, it might be better to keep your test data closer to the test class. Otherwise, by updating test data for one test you may break another test.

Also, if your test data is in a separate, non-compiled format, it's a lot easier for other people to add new tests. If the data format for each type of test is clearly defined, all that's needed to add an iteration of a test is to edit a text file and add a new row of data.

Again, it depends.

Do you really have non-technical tests that will be adding new test data entries? In a flight domain I worked we had less technical tests that were aware of booking flight format and it was easier for them to understand separate test data files than reading code would be. Where I work now we have more technical testers to work and writing test data in a separate Java class seems more natural to us.

Are your test data easier to read in a from of separate files? For instance, it was useful for me to keep flight bookings as separate XML files when I was working in a flight domain. On the other hand, where I work now, it's more natural to describe test data in same programming language as the rest of the code.

How complex is updating your test data? If your test data are in the same programming language as your test scripts, then many things become easier:

  • finding which tests are using given piece of test data with just one click in your IDE
  • refactoring test data structure will be easier with just one click

Here's an example of same data that Lesmana Sitepu described, but this time as a Java class

public class TestUsers {

 public static final JOHN_SMITH = new User()
   .firstName("John")
   .lastName("Smith")
   .with(new Address()
    .streetAddress("21 2nd Street")
    .city("New York"));

}
  • hai please give some examples – toastmaster Jan 17 '17 at 9:39
  • @toastmaster added. – dzieciou Jan 17 '17 at 9:49
  • so this java class example is like a json format. and I don't know about json. i want to study more about json – toastmaster Jan 17 '17 at 9:55
  • @toastmaster It's like a JSON but it is not JSON. And has certain advantages. – dzieciou Jan 17 '17 at 10:25
2

Based on the feedback you got, it seems that you keep your test data scattered all over your script. What your code reviewer means is that it will be much better if you separate your test data in JSON file, not directly put the value of your test data with your script/code. You could easily change the value of any given field/parameter you want to change for other tests.

This is an example of a Customer Form JSON file which you might see used to store configuration settings to setup your system. It might also be used to contain record information which can be easily shared across components using the simple JSON format.

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
 }

You could learn more about JSON here and here.

Also, here is a useful website to validate your JSON file.

Hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.