Use a JUnit rule.
Note that this code assumes your WebDriver can take a screenshot, which isn't true of all implementations...
Labels:
Use a JUnit rule.
Note that this code assumes your WebDriver can take a screenshot, which isn't true of all implementations...
11 Comments
Hide/Show CommentsJan 04, 2012
Anonymous
Thanks for sharing. Since I am setting driver in setUp() method, I have an empty constructor and added:
public void setBrowser(WebDriver driver) {In setUp():
public void setUp() throws Exception {Jan 04, 2012
James Richardson
Glad to be of use.
It looks like you are statically setting up test data and other state (including the webdriver instance) - in general its best to avoid all static state in tests, as you can lose track of what state gets created where, and so your test can end up being very non-obvious in the state it actually needs.
Additionally, i would always suggest to to not have no-arg constructors for classes, as this can mean that your objects are set up in an inconsistent (or not ready to work) state. Imagine - if you forgot to call the setter, then later on your class would fail with an NPE, and it wouldn't be obvious where the bug was. (It could be you didn't call the setter, that you called it with null, or you called it multiple times, and the last time was null)In this case if you had an empty constructor, then you would need to add two setters....and then have two methods you need to call all the time.
Unfortunately, spring encourages this and so lots of people have got into the habit.
Although a webdriver is something that you don't want to create and destroy for every test, you might find it useful to hide this behind some other abstraction.
James
Feb 09, 2012
Anonymous
Feb 09, 2012
Anonymous
Feb 09, 2012
Anonymous
Feb 10, 2012
Anonymous
Feb 10, 2012
Anonymous
Feb 08, 2012
Anonymous
Feb 09, 2012
Anonymous
Feb 09, 2012
Anonymous
Feb 09, 2012
Anonymous