• info@testbytes.net
  • +91 811 386 5000
whatsapp

How to Automate Feature Testing of Apps Using Selenium Web Driver

feature testing

Are you a developer and is struggling to do feature testing for your application? If that is the case then no need to worry as Selenium would give you an opportunity to test your features without any hassle.

app testing

It is a web automation testing tool which works well with almost all browsers. With the help of Selenium WebDriver you will be able to save your time and can automate any task with browser which you executed.

Selenium Webdriver is an interface which wraps and sends commands to the browser and implementation of this interface is given by difference browsers such as Mozilla Firefox, Google Chrome and IE.

How It Can Help in Feature Testing?

It depends on the kind of developer you are. If you are a developer who believes in testing all key features after deployment then Selenium Webdriver would be a panacea for you.

But there are many developers who just believe in developing new stuffs and not first testing the existing features and then go ahead with building new ones. This would lead to a lot of bugs and the cost incurred with finding a defect would increase in the defect life cycle.

Also Read : 15 Top Selenium WebDriver Commands For Test Automation

So, the strategy should be testing the existing features and then building new stuffs. If you are thinking that testing existing new features would consume a lot of time then in that case you can use Selenium Webdriver to cut the time taken to test these features.

In this way your code would be bug free and cost associated with defects will decrease.

Let’s now prepare first test with Selenium WebDriver. If you are thinking that you are not a Java Expert; then how would you start writing code and explore. Selenium Webdriver needs minimal use of Java if you want to start with it so doesn’t worry and go ahead!

Set Up Needs To Be Done

  • First thing is to download the chrome Browser as you will test your code on chrome.
  • Next is to download ChromeDriver from Seleniumhq official site and hence; you can download the chromedriver.exe file in your system.
  • You should have Java installed in your machine and this you can check by opening cmd and fire command “java –version”.
  • You should have Eclipse installed in your machine as it is a perfect IDE to develop the test to check the features.
  • You can now open eclipse and then click on New -> Java Project -> Next -> Give Project Name and Finish
  • Now add Selenium library to your project by first downloading the Jars from Seleniumhq site. Then click on Properties of your project and click on Build Path and then click on “Add External Jars”.
  • After this, you are ready to start with your code. All initial setup is ready.

First Test With Selenium Webdriver To Test Login Feature

For writing code you need to make a class file in your project. For doing that; click on New -> Class -> Next -> Name your Class -> Finish. Now, you can start writing your code in this section. As of now just copy the code below and run it on your local machine.

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class Test1 {

static WebDriver webDriver;

public static void main(final String[] args) throws InterruptedException {

// Telling the system where to find the chrome driver

System.setProperty(

“webdriver.chrome.driver”,

“C:/PATH/TO/chromedriver.exe”);

// Open the Chrome browser

webDriver = new ChromeDriver();

// Maximize the browser window

webDriver.manage().window().maximize();

if (testlogin()) {

System.out.println(“Test WordPress Login: Passed”);

} else {

System.out.println(“Test WordPress Login: Failed”);

}

// Close the browser and WebDriver

webDriver.close();

webDriver.quit();

}

private static boolean testlogin() {

try {

// Open google.com

webDriver.navigate().to(“https://www.YOUR-SITE.org/wp-admin/”);

// Type in the username

webDriver.findElement(By.id(“user_login”)).sendKeys(“YOUR_USERNAME”);

// Type in the password

webDriver.findElement(By.id(“user_pass”)).sendKeys(“YOUR_PASSWORD”);

// Click the Submit button

webDriver.findElement(By.id(“wp-submit”)).click();

// Wait a little bit (7000 milliseconds)

Thread.sleep(7000);

// Check whether the h1 equals “Dashboard”

if (webDriver.findElement(By.tagName(“h1”)).getText()

.equals(“Dashboard”)) {

return true;

} else {

return false;

}

// If anything goes wrong, return false.

} catch (final Exception e) {

System.out.println(e.getClass().toString());

return false;

}

}

}

Run this you will find Chrome Browser opening up and then testing the login functionality. Let me explain the steps one by one for better understanding.

  • setProperty(“webdriver.chrome.driver”,”C:/PATH/TO/chromedriver.exe”);

This statement is to tell your program where it can find chromedriver.exe file in your local.

  • webDriver = new ChromeDriver();
  • manage().window().maximize();

These commands will open Chromedriver instance and then would maximize the size of the browser.

  • if (testlogin())

This would test if the login feature is properly working or not. If it is not working then “Test failed” would be printed else “Test passed”.

Also Read : 52 Software Tester Interview Questions That can Land You the Job

  • Testlogin module has try and catch statements inside it. If everything goes as expected as per you then code in try block would be executed else code in catch would be executed. Suppose when you try to locate an element and it is not present in DOM then it would throw an exception “NOSUCHELEMENTEXCEPTION” and this code in catch block should be executed.
  • In this testlogin function steps are mentioned.

webDriver.navigate().to(“https://www.YOUR-SITE.org/wp-admin/”);

webDriver.findElement(By.id(“user_login”)).sendKeys(“YOUR_USERNAME”);

webDriver.findElement(By.id(“user_pass”)).sendKeys(“YOUR_PASSWORD”);

automation testing

These would be hit the URL in the browser and then find username and password fields. With the help of sendKeys you will be able to write to these text boxes.

  • findElement(By.id(“wp-submit”)).click();

With the help of this command you are clicking on submit form.

  • sleep(7000);

This sleep is included in order to follow the test visually. 7000 means 7000 ms that means 7s.

  • Now if the login is successful then the headline of the page should change to “Dashboard” and h1 headline is used only once in every page so you can get it by using tagname h1. You can get the text out of the webelement and then compare with “Dashboard” to test whether you have successfully login or not. Below code is for that.

if (webDriver.findElement(By.tagName(“h1”)).getText().equals(“Dashboard”))

{

return true

} else {

return false;

}

  • You can find the elements using various locator like ID, Name, Xpath, Css Selector and Link Text. Mostly you should prefer ID and name when they are not dynamic. Otherwise it is good to go with Xpath and CSS.

Conclusion

Thus after executing one test you would got to know the power of Selenium WebDriver. The more you will dive into it the more you will get proficient. You could even test same test with all browsers at the same time using parallel testing using Selenium GRID. So, when there is a need for you to do feature testing just go with Selenium Webdriver and leverage the benefits out of it. All the best!!

Testbytes