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
Understanding the WebDriver
On a higher level, this is what WebDrier does:
- The tests we write make use of WebDriver API
- This WebDriver API carries the commands (written in the test) to interact with the browser's driver
- 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.
- The browser returns the response to its driver
- The browser's driver will transfer information to the WebDriver
- Then, WebDriver shows the information to a user who is running the test
- chromedriver of Chrome
- geckodriver of Firefox
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.
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
- WebDriverManager and WebdriverIO are not WebDriver
- But all of these are around automation of the web and mobile
- WebDriver interface helps in
- Control of the browser
- Identification and selection of web elements on the web page
- Provides assistance to debug
- Browser Level API
- driver.manage().window().maximize();
- driver.get("https://testingGarage.blogspot.com");
- driver.navigate().back();
- driver.navigate().forward();
- driver.getWindowHandle();
- driver.getWindowHandles();
- Few Page Level API
- driver.findElement(By by)
- driver.findElements(By by)
- driver.getCurrentURL();
- driver.getTitle();
- driver.getPageSource();
- 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
- 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 article was curated as a part of #52nd Issue of Software Testing Notes newsletter.
ReplyDeletehttps://softwaretestingnotes.substack.com/p/issue-52-software-testing-notes