Getting started with Automation using Selenium Webdriver.
The job of a QA engineer in any software project is essential towards achieving a solution that is of a qualitative nature. However, it is often time consuming and also places great responsibility on the shoulders of the QA engineer to ensure the timely delivery of a solution that is free of issues. Yet, it is next to impossible to deliver what we ideally call “100% perfect”. In this case, we often tend to look for tools that make the job easier so that more attention can be given to tasks that require human effort.
Therefore, QA engineers are often encouraged to learn to write automation scripts for repetitive tasks in order to make their day to day tasks easier to manage. There are many types of software products but the most popular for common use today are web and mobile app based. Let’s discuss how newbies can start with automation using Selenium Webdriver.
Selenium Webdriver is used to write automation scripts for your web application in any programming language and execute your test cases against different web browsers.
Before jumping into any kind of automation you must first know the 3 pillars of automation or the 3 A’s as they are more commonly called:
· Accessors:
These are used to locate your elements. For example, there are paragraphs, headings, tables in any web page that you might want to identify. You can identify these using a number of different ways such as by using id, class name, CSS selectors, etc. The result is stored in a variable which can then be used in assertions.
· Actions:
These are actions that you perform, for example hover over some text field or button, drag and drop of any element, clicking a button, entering some text.
· Assertions:
These are the comparisons that you make between your actual and expected outcome. It passes when the actual result meets the expected, and fails when they do not meet. When we automate test cases, assertions decide whether our application is acceptable or not.
Of course, there is a lot more to automation within these three points but these are the basics to get started.
Software Required
· JDK
· Maven or Gradle to handle dependencies
· JUnit or TestNG as the test framework & their annotations
· Any IDE such as IntelliJ or Eclipse
· Any web browser of choice such as Chrome or Firefox
Getting Started
So before starting, the first thing you need to do is launch your IDE and create your project. A good approach is to divide your files into folders in order to make them easy to manage.
This can be done in a way such that your folders contain each relevant thing needed and a hierarchical structure is created. It is not mandatory but it makes your work easier to manage and also reduces redundancy as it promotes re-usability.
Initializing Driver
The first thing that needs to be done is to initialize our driver. For this, you need to install the driver of the web browser that you will be using. I am using Chrome so I installed the latest Chrome driver and pasted it in the driver’s folder of my project, and then initialized it. Give the path in the following way:
System.setProperty(“webdriver.chrome.driver”,”driver/chromedriver.exe”);ChromeDriver driver = new ChromeDriver();
Once this is done, you can use your driver variable to control what your browser does. For example, you can use the driver to navigate to any URL, or maximize or minimize the window, or perform waits in order to wait for your web-page's elements to load.
driver.manage().window().maximize();driver.navigate().to(“https://www.yourwebsite.com");driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Using Annotations
You can use frameworks such as Junit or TestNG to include annotations in your code.
For example, under @Before annotation you can write code that you want to execute before each test case, such as preconditions. @Test is used before methods under which we write the code for the test case to be executed. @After is used for any tasks that need to be done at the end of each test case, such as deleting temporary files or cache.
Similarly, we can use a number of different annotations depending on the need of the type of work to be done.
Finding Accessors
There are two types of Accessors or Locators.
Attributes-based locators:
They rely on the attributes of the elements to locate them, for example:
- Id
- Class Name
- Name
- Link
- Partial Link
Structure-based locators:
They rely on the structure of the page to find elements:
- DOM
- Xpath
- CSS Selector
It is not preferred to use structure-based locators as such cases are hard to maintain, because you never know when the structure of your web-page might change. On the other hand, id’s are seldom changed. Even if they do change, they are comparatively easier and less time-consuming to update.
You can use the WebElement type in order to find the accessors or locators of your web page. For example, if you have a login screen, typical elements on your page would be 2 text fields for ID and password, and a login button. In order to identify these, you will be needing the id or class name or any other locator that uniquely identifies them. In order to identify them on Chrome, just right click on your web page and click inspect. The elements tab should appear at the bottom or right of the page. Now, click Ctrl + Shift + C and then click on the element you want to identify (In this case, the text field). Automatically the HTML tag of that element will be highlighted in your Elements tab as shown in the example below:
As you can see, this particular email field has a class, name, and id with which you can identify it. Similarly, carry the same steps for the rest of the elements on your page.
Now, how do you tell Selenium which elements to find? This can be done the following way:
WebElement user = driver.findElement(By.id(“username”));WebElement pass = driver.findElement(By.id(“password”));WebElement lgnBtn = driver.findElement(By.className(“login-button”));
Use the WebElement type and your driver to find the element for you by using the By class to locate the particular element. You can use variables to identify your WebElements.
In the case where you have only one class name to identify a set of elements, with no id or any other way to uniquely identify the elements separately, what you can do is replace ‘findElement’ with ‘findElements’ and just append the statement with another function called ‘get’. Within get you can send 0 or 1 or 2 as parameter to identify whether you’re looking for the first element with said class name, second element with that class name, and so on. For example, using the below statement we are getting the text of the second heading in any web page that has class name heading:
String secondHeading = driver.findElement(By.className(“heading”).get(1).getText();
Using Actions
Actions, as the name suggests, are used to perform any action. You can initialize actions in the following way:
Actions action = new Actions(driver);
Now since we have identified the accessors, the next step would be to perform actions. There are two different types of methods in Actions: for Keyboard or for Mouse. Keyboard actions include methods like sendKeys(), keyDown(), etc. Mouse actions include doubleClick(), contextClick() which is right click, and clickAndHold().
Actions are primarily used for operations like hover, drag and drop, etc. But since our test case does not require those, we will go with our required simpler actions, such as to enter ID and password and click the login button. This can be done in the following manner:
user.clear();user.sendKeys(“myname@email.com”); //to enter usernamepass.clear();pass.sendKeys(“mypassword”); //to enter passwordaction.click(lgnBtn).build().perform();
It is not mandatory to perform such tasks with actions class only. For example, you could have performed the click in the last statement in the following manner as well and it would work just fine:
lgnBtn.click();
There are a number of different types of actions that can be performed depending on the nature of your web page.
Applying Assertions
Assertions are the most important and crucial part of any automation script. Good assertions are imperative because they are what determine whether your test case has passed or failed. Since automation reduces manual effort, it makes assertions even more important. Assertions are basically a comparison between what you are expecting and what you get. If they match, the test case passes. If they do not, the test case fails. Even if a single assertion fails, your test case fails. In order for the test case to pass, all assertions must be met. You can use some pre-defined assertion methods from Junit and TestNG frameworks.
For example, after login users are expected to land on some welcome screen that may have a different URL, some Home screen heading, and a sign out button. In this case, we can populate our expected outcomes in some variables such as String and then fetch the actual outcomes using findElement() and getText(), and use assertions on them.
//for the Welcome TextString welcomeExp = “Welcome to website!”;String welcomeAct = driver.findElement(By.id(“welcomeText”)).getText();Assert.assertEquals(welcomeExp,welcomeAct);//for the URLString expURL = “https://www.yourwebsite.com/home-screen/”;String actURL = driver.getCurrentUrl();Assert.assertTrue(actURL.equals(expURL));//verify Signout sectionString signoutExp = “Sign Out”;String signoutAct = driver.findElement(By.id(“logoutLink”)).getText();Assert.assertEquals(signoutExp, signoutAct);
These are only a few examples of assertions. There are many other pre-defined methods that can be used. Similarly, it is a good practice to include as many assertions as possible in order to cover thorough testing of your web app using automation. Since automation aims to reduce manual effort, it is better to put in one-time effort by using as many assertions as you can in order to cover as many parts of your web application as possible and then in the end compile all your test cases into test suites and run them all at once.
Executing the Test Case
Notice that each time you use the annotation @Test, you will find a small green play icon next to your test method. After you are done writing your test case, click this button and then click run.
It will start your test case and you will see the fruit of your hard work be performed magically before your eyes. 😊
If your test fails at any step, you will see such a result and your test will terminate at the first failure of an assertion:
And in the event that it passes, you will see something similar to this:
This however, isn’t the only way you can execute your test cases. If you have a large number of test cases, you can create a Test Suite where you can mention all your test cases files and the methods in the following format and then run them all at once rather than executing each test case one by one. Reduce as much manual effort as possible. You can further search how test suites are created once you have a strong grip on automation basics.
Conclusion
This article is only a head start to give you the initial push in your automation journey. There is a lot more to learn such as the use of waits, handling exceptions, handling drop-downs, using loops, handling popups and modals, working with iframes, etc.
The examples here followed the simplest approach of all, i.e. the linear approach; but it is not preferable since it is too straight forward and does not allow code re-usability. A better and more preferred approach would be to use a framework such as Page Object Model or Page Factory. With a framework, you can keep all your locators in one folder, all objects in one, database connections in one, and so on. Breaking your code into chunks will allow easier updating and re-usability. This will not only make your work faster but it will also improve efficiency of your scripts.
Remember that every test case cannot be automated; there will always be some part that will require human effort which the software cannot replace. However, as engineers, we must always aim for an efficient and cost-effective approach towards work. The internet is our best source of knowledge in this regard as there are many great tutorials and platforms online where you can learn and ask your questions. Therefore, we must always polish our skills to the best of our abilities and learn to utilize our skills towards making our work easier for ourselves as well as ultimately keeping the clients happy.