5

Does it wrap around Selenium and provide a simpler or different method of invoking the functionality of Selenium?

I looked it up on Google and the best information I could find was this one https://www.ontestautomation.com/using-wrapper-methods-for-better-error-handling-in-selenium/.

This doesn't explicitly explain what a Selenium wrapper is but gives enough information to help understand what it is.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
dc.niel
  • 63
  • 1
  • 5

3 Answers3

3

One of the definitions of a "wrapper" is:

In the context of software engineering, a wrapper is defined as an entity that encapsulates and hides the underlying complexity of another entity by means of well-defined interfaces.

So, any custom code you might use that implements Selenium code could be understood as a wrapper.

For example, Katalon Studio is a testing tool that uses Selenium under the hood i.e. Katalon's WebUI class methods are a wrapper around Selenium methods. The following two pieces of code are equivalent - they do the same thing:

  1. Selenium (and Java)
WebElement element = driver.findElement(By.cssSelector("css-selector-of-the-element"));
element.click();
  1. Katalon
WebUI.click(testObject) //testObject defined elsewhere

This is just a simple example, but it shows how can you hide complexity behind simpler commands.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • Sure. I've clicked the up arrow. Will give this a few days in case anyone else answers before clicking the checkmark. – dc.niel Jul 03 '20 at 10:07
  • I upvoted your answer. It just doesn't publicly display because I have less than 15 points against my account on stackoverflow. However the upvote has been recorded. – dc.niel Jul 10 '20 at 12:53
  • I've built an entire framework that wraps selenium for testing purposes. Now other developers can use the methods without the hassle of asserting all the time. Assertions and wait elements are done on framework side. Also multiple web driver available. The idea is that the developer using the framework won't be exposed to the Selenium nuget but to the Framework's. Then the tester can implement in their testing solution something called the Page Object Model, it's one of the existing pattern for automation testing. – NewBie1234 Jan 12 '21 at 14:16
2

I know this question has already been answered but I can see it was never accepted as an answer. Now, the answer above explains exactly what a wrapper is : encapsulation. Which in itself means also that it hides the underlying complexity of another entity (Selenium classes in this case).

But let me elaborate and give you an actual example.

I've built a Framework around Selenium and made a nuget package out of it internal to my company. But this is one example on how to wrap Selenium By class. Using a delegate, you can simplify a lot of the calling methods :

private delegate void ValidationMethodDelegate(By locator, int timeout = ELEM_TIMEOUT);

//This method actions a delegate for regularly used methods with a By locator parameter, 
//the value of the selector and the selector type which is a built-in framework enum
private void ActionMethod(ValidationMethodDelegate delegateMethod, string selectorValue, SelectorType selectorType)
    {
        if (!string.IsNullOrWhiteSpace(selectorValue))
        {
            switch (selectorType)
            {
                case SelectorType.Id:
                    delegateMethod(By.Id(selectorValue));
                    break;
                case SelectorType.Xpath:
                    delegateMethod(By.XPath(selectorValue));
                    break;
                case SelectorType.TagName:
                    delegateMethod(By.TagName(selectorValue));
                    break;
                case SelectorType.CssSelector:
                    delegateMethod(By.CssSelector(selectorValue));
                    break;
                case SelectorType.ClassName:
                    delegateMethod(By.ClassName(selectorValue));
                    break;
                case SelectorType.LinkText:
                    delegateMethod(By.LinkText(selectorValue));
                    break;
                case SelectorType.PartialLinkText:
                    delegateMethod(By.PartialLinkText(selectorValue));
                    break;
                default:
                    break;
            }
        }
        else
        {
            throw new AssertionException($"{this.GetType().Name}::{MethodBase.GetCurrentMethod().Name}():: Selector Value : '{selectorValue}' cannot be null or empty.");
        }
    }

//Example on how the delegate is used
public void Click(string selectorValue, SelectorType selectorType)
    {
        ActionMethod(PageHelper.Click, selectorValue, selectorType);
    }

The PageHelper is a static class that implements internally to the framework most of Selenium's methods with assertions and waiting implementations. I have several layers of complexity in my framework. But you can make it simple too. The method click for me is wrapped also in another class that implements two methods one that finds the element by and the other than waits for an element to appear on screen. Both are other wrappers around Selenium methods and assertions.

If you are only doing tests for one application and won't have further use of Selenium then a framework is not a solution for you. Also wrappers would be kind of redundant outside your test solution.

I would say that wrappers would only be useful in the context where you are making multiple use for it (like the click or find element, etc.)

NewBie1234
  • 461
  • 6
  • 23
  • 1
    Thanks for the extra details. I've upvoted your answer. – dc.niel Apr 03 '21 at 09:54
  • I also recommend you look at qa automation university, they have a free and detailed video on how to wrap the IWebElement object : https://testautomationu.applitools.com/ – NewBie1234 Apr 05 '21 at 15:35
1

"Wrapper" is more like a software development design pattern, which developers use in the codebase when it is necessary.

You can read more in the book:

https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_2?dchild=1&keywords=software+development+patterns&qid=1605187094&sr=8-2

In scope of automated testing, there are other terms. I will explain for mobile automation.

Driver (Espresso, UIAutomator, Robotium, XCUITest) - receive commands from test and send them to app specialized interface in understandable way

You sent a command to press a button to the GUI driver - it accepts it via API and sends to the app (and we see a tap on the button in GUI).

Another app (which is over driver, let's call it superstructure in this context) that interacts with the app under test via one or more drivers (increasing usability or increasing possibilities) like Appium, Calabash.

Frameworks (JUnit, TestNG, Cucumber) - app that allows us to prepare, launch and gather all info regarding test executions

It will look like this:

Framework -> Our tests -> Superstructure -> Driver -> GUI in our application