2

I have a bunch of WebDriver profiles (let's call them x, y and z) and I need to select a random profile to perform certain actions. Let's call the actions A, B and C.

In my class WebDriverManager I've defined three methods: doA(), doB() and doC(). Each method will select a random profile that a) is not currently being used and b) that is eligible to perform the given action.

If a client calls doA(), doB() or doC(), they will get a WebDriver profile eligible to do action A, B or C. doA(), doB() or doC() will also set the status of the said profile to UNAVAILABLE (via props file) so that the profile will not be returned for a different action (or the same action if called from a different thread) until it becomes available again.

My problem is the profile will only become available when the client finishes using the said profile for its intended purpose.

Since the methods doA() etc will be accessed through many clients, how can I force the client of doA(), doB() or doC() to set the status of WebDriver profile to AVAILABLE before it returns? If sometime in the future I forget to change the status of the given profile to AVAILABLE (in the client), it will remain UNAVAILABLE forever which will cause a lot of problems.

So how can I guarantee that whatever method calls doA(), doB() and doC() sets the profile status to AVAILABLE before returning?

P.S.

This is the closest question I found on SO but I believe this does not address my issue because I cannot control the actual WebDriver class, I simply want the client of each of the said methods to perform a certain action before they return.

AutoClosable won't work either because I'm not trying to close a specific instance of class (I do not control the class), I want the client of a certain method to execute a specific method or code before it finishes.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
David Lynch
  • 119
  • 1
  • 9
  • Maybe pass in a `Consumer` and ask the client to do everything they needs in a lambda/method reference? – Sweeper Jan 17 '18 at 06:59

1 Answers1

0

From the way it sounds like you have it defined, the current instance of WebDriverManager should track what profile is being used for the current test. If it doesn't already, you will have to write this code. During test cleanup, call something like WebDriverManager.SetProfileAvailable() that sets the used profile back to AVAILABLE.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • How can WebDriverManager track/know when the client is finished? All WebDriverManager knows is when it's being called, not when client returns. – David Lynch Jan 17 '18 at 16:04
  • Your test would know when it's done. I don't know what framework you are using but if you use TestNG it has the concept of `@AfterTest`. You create a method that you want run after each test, label it with the attribute `@AfterTest`, and inside that method you would call `WebDriverManager.SetProfileAvailable()`. Here's a link to the [TestNG docs](http://testng.org/doc/documentation-main.html) to give you a better idea of how this works. There are other annotations you can use for different cases. There are also other frameworks, e.g. JUnit, that you can use. – JeffC Jan 17 '18 at 16:13