So, without in any way encouraging you to screw patterns into code everywhere, I am starting to publish a series of articles on pmp proxy exam patterns.
A little more about the format and let’s go
The articles/books/video courses on patterns themselves are pretty decent. And I’m sure you can easily find a resource with a competent explanation, examples in your favorite language, emojis, etc.
The purpose of this and subsequent articles is to tell about the patterns to those who already work with the code, but do not have a good base on the theory. So these articles, perhaps, for me, in the first place.
Well, since I myself am a tester, and there are many of the same in my circle of friends, the stories will be with an emphasis on test automation.
Some of the patterns will seem strange and not very useful. And with some, on the contrary, you will find that you have been implementing something similar for a long time, but you didn’t know that it was called that.
In any case, I hope you find something useful for yourself.
And yes, I will be very glad to constructive criticism in the comments.
- about himself – said
- what will happen – said
- why – said
- for whom and how – told
You can start.
Proxy – Proxy – Deputy
Proxy pattern language publications, Deputy ). The idea is to issue not a real object for work, but a substitute that uses the methods of the object + our logic, if we added one. So click to find out more
How to do it:
1. We create an interface with public methods of the object that we want to replace
2. We create a class that
– implements this interface
– has access to the original object in order to call its methods
3. We add our own logic to the methods of the created class
In practice, it should be clearer.
Task: I really need to log how many elements were found by the selector.
Every time I call a method
Solution 1. In the forehead. What is there: I used it.
We just take it and, with each call, we write how much was found:
The option is normal, so far there are, say, 7 such calls. Although it is already unpleasant and the disadvantages of this approach are obvious to anyone who has ever “slightly modified their code”.
Solution 2. Use a proxy.
WebDriver is an interface. Methods are declared, but there is no implementation. The implementation contains ChromeWebDriver, FirefoxWebDriver, etc.
We, in tests, do not have to work with any particular class for chrome or safari. It is only necessary that the class implement the WebDriver interface. Let’s do this:The pattern is good if you need to hang logging, caching, lazy initialization, access control to methods. In general, if you are writing code and you really want to have some kind of layer with your features, one of the options is a proxy.
Now that we know one pattern more, let me remind you that when you have a hammer in your hand, everything around seems to be nails.