• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Wrapper classes in pattern which I use it

 
Greenhorn
Posts: 13
1
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I trying to accomplish my framework for testing. I'm still at the beginning and I miss lackunderstanding of how to do this what I want it. It has Selenium and TestNG. I use a factory pattern to instantiate objects (I won't copy-paste full code, I will only what I think is unnecessary to be understandable) :






BaseTest page which has to extend every Test class:



As you can see I want to get the full potential and to use parallel testing.
This code above is ok, I mean it works but if you have any suggestions I would like to hear everything to learn better.

Then I decided it will have my own wrapper Driver and Element class which I will call where I need it and it will have override methods with little boost .



I won't copy-paste all abstract methods. I just added to see what I really want to do it. Anyway, I have a lot of work to do in those methods.



Element class:





First I wanted to create for Driver and Element to be decorators. But I am not sure do I really need it in overridden methods to call something new, before or after from CoreElement/Driver classes. Now I'm wondering do Driver and Element will stay in SRP because I think it will have a lot of abstract methods.

But the main question of this topic is if I let my wrapper Driver and Element how to implement them in factory pattern and to call my Driver instead of WebDriver? That is the part that I can understand to continue to work on it.

Please, help me.

 
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I understand correctly, you want to achieve two things:

  • Decorate WebElement and WebDriver with your own methods.
  • Create factory of driver managers, where a driver manager wraps around a specific type of (decorated) driver.

  • First, make sure you have interfaces for your driver manager, driver and element types. Providing only abstract classes is a real pain if you decide later that you want to use a different base implementation.

    You can easily solve your problem by having your manager return instances of your custom driver interface, instead of WebDriver:

    Now all of this is massively overengineered and I don't think you need it all. Just take the bits that help.

    I also made Driver and DriverManager extend AutoCloseable. That means you'll be able to do something like this:

    This will automatically make the driver quit when the test finishes.
     
    Deyan Perov
    Greenhorn
    Posts: 13
    1
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    First, thank you very much! You showed me a new level for me how to think about implementation.

    Decorate WebElement and WebDriver with your own methods.


    Yes and no. When I researched I thought it will be useful. I am in dilemma do I need a decorator pattern here. What do you think? I want to achieve this project (skeleton of the project) can use for mobile testing too (Appium) because of that my goal is to wrap Element and Driver.
    How do I look at a decorator? Some methods for Elements may not be applicable in some situations and then I want to override them over decorator. if I really need that then am I making a mistake somewhere?

    Create factory of driver managers, where a driver manager wraps around a specific type of (decorated) driver.


    Yes.

    I am going to implement these codes.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15727
    368
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Deyan Perov wrote:I am in dilemma do I need a decorator pattern here. What do you think?


    Honestly, I don't know. What problem are you trying to solve? As far as I can tell, once you have setup a driver for a specific browser, using the driver to perform your tests is the same regardless of the browser. If you don't like the API provided by WebDriver and WebElement, you can of course provide your own façade that wraps around those classes, but you wouldn't need a custom subclass for every different type of browser. In that case, just rename WebDriverDecorator to SeleniumDriverFacade and make it final. You can then also get rid of Element and all its subtypes, and just return WebElement.

    I want to achieve this project (skeleton of the project) can use for mobile testing too (Appium) because of that my goal is to wrap Element and Driver.


    I'm not familiar with that framework, but if there is overlap with the drivers and elements used by Selenium, then yes it makes sense to write your own Element and Driver interfaces and have façades for Selenium and Appium implement those interfaces by wrapping around the classes that Selenium/Appium provide.

    How do I look at a decorator? Some methods for Elements may not be applicable in some situations and then I want to override them over decorator.


    You need to explain this a little bit better. What methods may not be applicable? In what situations?
     
    Deyan Perov
    Greenhorn
    Posts: 13
    1
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Maybe I’m not on the right track and I don't know how to understand better. I think the decorator is not what I am looking for.
    My idea is I want to create my own methods for any Element and those methods I will use for locators to interact with it. The driver will have his own methods too.
    This is one of example which probably I will use:





    Before it clicks on an element it has something to happen to be sure there is an element I am looking for, for example above to wait for elementToBeClickable, to be presented in DOM, stale element, or anything else to help me to avoid possible exceptions. I don't want just to call Selenium's WebDriver and to click() on an element. In a similar direction, it will work with other Element's methods.
    I thought decorator pattern can be useful if some methods in some situation for some element on a page doesn't work jobs well to override over ElementDecorator and to use it in that specific situation. I didn't try this in practice. That was on my mind when I start to explore how to do this. I don't have enough experience to resolve is this good or not.
    I hope I have explained a little better what I want to achieve.

    If you don't like the API provided by WebDriver and WebElement, you can of course provide your own façade that wraps around those classes, but you wouldn't need a custom subclass for every different type of browser. In that case, just rename WebDriverDecorator to SeleniumDriverFacade and make it final. You can then also get rid of Element and all its subtypes, and just return WebElement.


    Could you please show me this in a small example to be sure I understood you?
     
    Deyan Perov
    Greenhorn
    Posts: 13
    1
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did refactoring, I avoid the decorator pattern. The main problem is in BaseTest class where I need to use ThreadLocal class and to set the driver which I will use in a multi-thread. Every Test class has to extend BaseTest class.

    I have to initialize DefaultDriverManagerFactory in this class but I don't see the point to put in BaseTest Duration, Clock and Sleeper attributes which have to be put in constructor DefaultDriverManagerFactory(Duration timeout, Duration sleep, Clock clock, Sleeper sleeper). That codes haven't to be part of BaseTest.
    If I use the static method (from my first post) then I have to put static for interface DriverManagerFactory method DriverManager createManager(WebDriverType driverType); with the body. Is that good practice in this situation?
























    Can anyone help me how to do this?
     
    Deyan Perov
    Greenhorn
    Posts: 13
    1
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This topic has over 1500 views. Did I explain badly what I need? I just need some guidelines. I don't have a lot of knowledge and I hoped I will find help here. I think this question isn't for StackOverflow because I need directions on how to do and finish.
    it was shown to me in one way but in a way, I don’t know how to finish unlike mine originally.
     
    knowledge is the difference between drudgery and strategic action -- tiny ad
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic