简体   繁体   English

Java:强制方法的客户端在返回之前执行代码

[英]Java: Force Client of Method to Execute Code Before Returning

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. 我有一堆WebDriver配置文件(我们叫它们x,y和z),我需要选择一个随机配置文件来执行某些操作。 Let's call the actions A, B and C. 我们将动作称为A,B和C。

In my class WebDriverManager I've defined three methods: doA() , doB() and doC() . 在我的课堂WebDriverManager我定义了三种方法: doA() doB()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. 每种方法都会选择一个随机配置文件,其中a)当前未使用, b)有资格执行给定操作。

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. 如果client调用doA(), doB() or doC() ,则他们将获得一个有资格执行操作A,B或C的WebDriver配置文件doA(), doB() or doC()还将设置其状态。将该配置文件传递给UNAVAILABLE(通过props文件),以便在该配置文件再次可用之前,不会为其他操作(或从不同线程调用的同一操作)返回该配置文件。

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? 由于方法doA()等将通过许多客户端来访问,我怎么能强迫clientdoA() doB()doC()对的状态设置WebDriver轮廓,它返回之前? 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? 那么,如何保证返回之前调用doB() doA()doB()doC()任何方法都将配置文件状态设置为AVAILABLE?

PS 聚苯乙烯

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. 是我在SO上找到的最接近的问题,但是我认为这不能解决我的问题,因为我无法控制实际的WebDriver类,我只是希望每个上述方法的客户端在返回之前执行某种操作。

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. AutoClosable不会起作用,因为我没有尝试关闭类的特定实例(我不控制该类),我希望某个方法的客户端在完成之前执行特定的方法或代码。

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. 从听起来像已定义的方式来看, WebDriverManager的当前实例应跟踪当前测试使用的配置文件。 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. 在测试清除期间,调用WebDriverManager.SetProfileAvailable()类的东西,将使用的配置文件设置回AVAILABLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM