Friday, April 1, 2022

WebDriver: Clarifying the Confusion on Why and What is the WebDriver - Part 1

 

I had a question "What is WebDriver and why should I use it to automate on a browser?"  I tried to understand it and relate its presence in code written using Selenium.  I see this question in the test engineers who are starting the practice of automation on browsers. 

And, most of us get confused with WebDriver, WebDriverManager, and WebdriverIO.  All of these are not the same but all these work around the same space that is automation on the web and mobile.

Between, I learn understanding of WebDriver is fundamental to the practice of automation on web browsers. The same idea is taken to the automation of mobile apps using Appium. 

I'm sharing this learning of me as a part of 21Days21Tips the initiative from The Test Chat community.  The tip here is to assist by providing clarity around the WebDriver and why we use it in automation on a browser.


What is WebDriver?

The WebDriver is part of the Selenium library and we use it every time when we are trying to do any interaction with and upon a browser.  It is also a language binding and helps to write the browser controlling code.  For example, if I pick Selenium's Java WebDriver,

  • it provides the APIs that I consume to control the actions on the web page displayed on a browser
  • likewise, if I pick Selenium's Python WebDriver it provides me the APIs that I consume to automate my actions on a browser
    • I code here using Python
That said, the WebDriver is a set of APIs and to be precise it is an object-oriented API adhering to the W3C standards.  As a result, the WebDriver drives the browsers effectively today as all popular browsers to the W3C standards.  The HTTP is used as the transport protocol.


Understanding the WebDriver

On a higher level, this is what WebDrier does:

  1. The tests we write make use of WebDriver API 
  2. This WebDriver API carries the commands (written in the test) to interact with the browser's driver
  3. On receiving the commands, the browser's driver and the browser will have native communication, where the driver will translate the commands to the browser to emulate the action on a browser.
  4. The browser returns the response to its driver
  5. The browser's driver will transfer information to the WebDriver
  6. Then, WebDriver shows the information to a user who is running the test
Examples of browser's driver are:
  • chromedriver of Chrome
  • geckodriver of Firefox


Representation of Selenium WebDriver's Communication


The instructions (commands) that I pass via WebDriver's object are translated to stateless information.  That is, there is no state maintained between the client and the browser's driver.



Representation of Selenium's WebDriver SPI & Browser Interaction

When the code enters into Stateless Programming Interface (SPI), it is called into a process that breaks down what the element is, by using the unique identification and then calling the command.  For example, let us look into the below statements to understand what the code looks like at SPI:


Code written using WebDriver API:

WebElement greetBox = driver.findElement(By.id("greeting_textbox"));
greetBox.sendKeys("Welcome to Testing Garage's Blog");

 SPI:

findElement(using="id", value="greeting_textbox")
sendKeys(element="greetBox", value="Welcome to Testing Garage's Blog");


Note: The findElement and sendKeys are the commands provided by Selenium's WebDriver API to find the web element on the web page and enter the text into the web element. The browser's driver receives these commands and data, then emulates the command (a user action) on the browser, and carries back the response to WebDriver.


21 Days 21 Tips -- #day13

Here are my pointers to fellow test engineers who are confused about WebDriver
  1. WebDriverManager and WebdriverIO are not WebDriver
    • But all of these are around automation of the web and mobile
  2. WebDriver interface helps in
    1. Control of the browser
    2. Identification and selection of web elements on the web page
    3. Provides assistance to debug
  3. Browser Level API
    1. driver.manage().window().maximize();
    2. driver.get("https://testingGarage.blogspot.com");
    3. driver.navigate().back();
    4. driver.navigate().forward();
    5. driver.getWindowHandle();
    6. driver.getWindowHandles();
  4. Few Page Level API
    1. driver.findElement(By by)
    2. driver.findElements(By by)
    3. driver.getCurrentURL();
    4. driver.getTitle();
    5. driver.getPageSource();
  5. If you notice, we use these APIs to automate the browser
    • The tests we write use these APIs of Selenium WebDriver along with the assertion
  6. Why are we using "driver" in the above commands?
    • This is another question and confusion among fellow test engineers starting to practice automation
    • I will share this in the next tip :)
This understanding of WebDriver, and the why and how it is instantiated (in the next post) will help you to be comfortable in starting to read the test code written using Selenium.



Saturday, March 19, 2022

Time to Update the 101s for Test Engineers

 
The thought I'm sharing in this post is growing stronger in me, in recent years. I shared it as points with the community in the #21Days21Tips initiative from The Test Chat.  I post it on my blog as well so that it will be a reminder to me when I scroll through my posts.


The 101s for Test Engineers

Today, the first few stepping lessons of Testing and Automation to be on -- "How to Overcome the Fear of Failure".  If this is not available in the team and organization, it is time to start and build it.  The present and future Test Engineers need it.

The starting lessons or 101 of the Testing and Automation need not necessarily be:

  • What are Testing and Automation?
  • Test Design and Techniques
  • Programming Language
  • Tech Stack, Full Stack
  • Clean Code
  • Design Patterns
  • Tools
  • What should I pick; how & where to start here?

All these will connect and align when one can handle self-doubts and fear of failure.


Approach Goals and Avoidance Goals

As a team, organization and mentor, help your Test Engineers to have "Approach Goals" than letting them build "Avoidance Goals" out of fear.  I see practicing the Approach Goals is a life skill, today.

I take the below lines from this post:

Approach Goals are those with positive outcomes that we work towards.  Positive can mean different things in different contexts, such as liked, desirable, pleasurable, or beneficial.

Avoidance Goals are those with negative outcomes that we work to avoid. Negative can mean different things, including disliked, undesirable, painful, or harmful.

Examples:

  • Approach Goals
    • Know why I have those fears when I think of automation?
    • I need to be skilled in automation. How can I manage my fears and proceed with practicing automation?
    • I need to do better testing with help of automation. What I should practice for doing this? What help I will need?
  • Avoidance Goals
    • I will start practicing automation next month
    • I will start automation once I understand and write good code
    • I will start automation once I understand Design Patterns and Clean Code
    • I will start automation once I learn how to write a framework
    • I need to do it well; let me learn the basics very well first and then let me do automation
    • I will check for workshops and people who can help me to do automation

Approach Goal: I will start it! No matter how small it is, I will start. I will keep updating as I learn. I start it from here -- Automate a browser to launch and open a new tab.

Frame the Approach Goal that helps to take one ahead by dealing with the fear.  The goal needs to make sure the milestones of learning are highlighted to one.  That way, one feels confident and has clarity along with skills of learning better be it what is new or what one is aware of.

Note: If the question is why I talk of automation in the examples of Approach Goals and Avoidance Goals, this is what we hear from the majority of Test Engineers today.


My Experience

When the Test Engineers know to build and use the Approach Goals, they will align and practice better on subjects:

  • With which Test Engineers are unfamiliar
  • With which Test Engineers are uncomfortable

Also, help the Test Engineers to build skills to spot Avoidance Goals.  So that, they are aware of the Avoidance Goals they tend to have and construct positive Approach Goals to learn new skills and solve the problems.