• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Practice Project

 
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I used JUnit, Selenium, and Cucumber to make a basic web app test and I had some questions about how I might be able to improve it. This is just a personal practice type thing. Here is what I have so far:

CucumberRunner class:



Feature File:



And my step definitions:



(If you try and run any of that, obviously you'd need the jars for cucumber, selenium, and junit, plus firefox installed)

1. When I am populating the fields, instead of hardcoding 1 value in each, I'd like to set it up to be data driven through excel or MySQL (what word would I use to describe that? would robust be accurate there? or maybe versatile).

2. Also when populating the fields, that code looks cumbersome, and I'm trying to figure out a way I could use a loop to populate for each field, and what type of loop it would be?

3. Being new to automation (QA Engineer Level I intern), and testing in general, is there another/better method or tool to use if I wanted to stick with Java for automation? My job had me using QTP and VB Script, which was really powerful, for a little while to kind of introduce this to me, but I'd rather stick with Java because Java is where I want to focus (and they're giving me the option, so it's not like I'm being insubordinate with them)

Thanks in advance!

EDIT:

If you do decide to try and run this, these are the jars in the build path:

cucumber-core-1.1.5.jar
cucumber-html-0.2.3.jar
cucumber-java-1.1.5.jar
cucumber-junit-1.1.5.jar
cucumber-jvm-dep-1.0.3.jar
gherkin-2.12.1.jar
hamcrest-all-1.3.jar
junit-4.11.jar
selenium-server-standalone-2.44.0.jar

As an aside everything currently works perfect, so just clarifying that I don't have errors or anything like that. More so just looking for tools, or practices that I may be missing or not aware of in general, that I could improve this with. And again, thanks in advance!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
First of all, I've given you a cow for asking such a great question. It's clear, interesting and contains all the relevant information.

David Borchgrevink wrote:When I am populating the fields, instead of hardcoding 1 value in each, I'd like to set it up to be data driven through excel or MySQL (what word would I use to describe that? would robust be accurate there? or maybe versatile).


I'd say using a datasource. CSV is probably going to be better than Excel for your needs. Easier to edit the file. Consider using OpenCSV to read the file in.

If I'm understanding the data you want to externalize right, I don't think you need the Parameterized Test Case pattern here. I recommend reading up on it anyway though. It's a good tool to have in your tool box.

David Borchgrevink wrote:2. Also when populating the fields, that code looks cumbersome, and I'm trying to figure out a way I could use a loop to populate for each field, and what type of loop it would be?


That code does look cumbersome. One approach would be to have two arrays. One would contain the two "in" values and the other would contain the 20 "ex" values. Then you could use two for loops to create the field names with the right index and build the list that way.

David Borchgrevink wrote:3. Being new to automation (QA Engineer Level I intern), and testing in general, is there another/better method or tool to use if I wanted to stick with Java for automation? My job had me using QTP and VB Script, which was really powerful, for a little while to kind of introduce this to me, but I'd rather stick with Java because Java is where I want to focus (and they're giving me the option, so it's not like I'm being insubordinate with them)


You picked the best of breed tools.

I see two other things to remark on in your code:
1) The FirefoxDriver is a lot slower than the HtmlUnitDriver in Selenium. If you need to test in a real browser, you have to live with it. But if you are just testing functionality and a "simulated" browser does the job, it is worth considering.
2) In Java, we general throw Exception and not Throwable. The later includes system errors like running out of memory that automatically get thrown.
2) I see in a number of places that you catch an exception and print a stack trace. When this happens, you should fail the test and not proceed silently. You could add a fail statement. But it would be better to remove the try/catch block entirely and let JUnit deal with the error. Conveniently the default behavior is to fail the test and give you a stack trace!
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:David,
First of all, I've given you a cow for asking such a great question. It's clear, interesting and contains all the relevant information.



Thanks!

If I'm understanding the data you want to externalize right, I don't think you need the Parameterized Test Case pattern here. I recommend reading up on it anyway though. It's a good tool to have in your tool box.



So I know Cucumber can do the data table thing using Examples in the feature files, which is nice, but I could see MySQL being much easier to work with. If I'm understanding you right, can I just drop the MySQL connector jar into my build path, and use normal SQL queries in my step definitions where needed? I guess I'm green, and naive, but I didn't think Cucumber and SQL would play nice together. If so, that would be awesome!

That code does look cumbersome. One approach would be to have two arrays. One would contain the two "in" values and the other would contain the 20 "ex" values. Then you could use two for loops to create the field names with the right index and build the list that way.



That is a great idea. I feel like I was close to thinking of that, but I can't take the credit Quick question though, if I were to try and create a hypothetical framework for web app testing, I would have to know the html source for each element for each site tested ahead of time right? Because not every site will have id's or name's that are identical? Is there a way to make a "universal" method to do things like that? If not, meh whatever, but now I'm curious.

You picked the best of breed tools.

I see two other things to remark on in your code:
1) The FirefoxDriver is a lot slower than the HtmlUnitDriver in Selenium. If you need to test in a real browser, you have to live with it. But if you are just testing functionality and a "simulated" browser does the job, it is worth considering.
2) In Java, we general throw Exception and not Throwable. The later includes system errors like running out of memory that automatically get thrown.
2) I see in a number of places that you catch an exception and print a stack trace. When this happens, you should fail the test and not proceed silently. You could add a fail statement. But it would be better to remove the try/catch block entirely and let JUnit deal with the error. Conveniently the default behavior is to fail the test and give you a stack trace!



1) Yea I prefer Chrome for normal browser business, but for the sake of this project Firefox was just slightly easier to work with. I will definitely look into HtmlUnitDriver though, that sounds like it could be super beneficial.
2) I do realize that Exception would have been better (isn't best practice to use the most specific expected exception first, then work your way down to throwable or exception?). The only reason I kept the step definition class' method declarations like that was because Cucumber populates them automatically for you when you run the Feature file with no methods in the step definition file, and it was quick and easy to use.
2)So if I'm understanding you correctly (again, I'm a newbie to all of this), just having the JUnit jar files added to the project will do that for me? That's great! I guess I added the try/catch blocks around each step definition after the fact because I thought it was good unit testing practice (I could very well be using that term incorrectly), but either way that is good to know

Thanks again for the response, and good information
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Borchgrevink wrote:That is a great idea. I feel like I was close to thinking of that, but I can't take the credit Quick question though, if I were to try and create a hypothetical framework for web app testing, I would have to know the html source for each element for each site tested ahead of time right? Because not every site will have id's or name's that are identical? Is there a way to make a "universal" method to do things like that? If not, meh whatever, but now I'm curious.


Sorry, no magic. You'd need there to be a pattern to the ids/names for this approach to work. In your example, there happens to be one.

David Borchgrevink wrote:
2) I do realize that Exception would have been better (isn't best practice to use the most specific expected exception first, then work your way down to throwable or exception?). The only reason I kept the step definition class' method declarations like that was because Cucumber populates them automatically for you when you run the Feature file with no methods in the step definition file, and it was quick and easy to use.


Yech.

David Borchgrevink wrote:
2)So if I'm understanding you correctly (again, I'm a newbie to all of this), just having the JUnit jar files added to the project will do that for me? That's great! I guess I added the try/catch blocks around each step definition after the fact because I thought it was good unit testing practice (I could very well be using that term incorrectly), but either way that is good to know


Correct.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic