Test Automation Framework for Web Application using Java

23.3k Views Asked by At

I am beginning to write a Test Automation Framework in Java (language that I am comfortable with) for my Web Application. Currently, it is entirely tested on UI. No Backend / API testing in near sight.

I plan to use Selenium Web Driver. This framework will support both Functional/Integration and Performance testing.

I am building with Open Source Solutions for the first time (over using tools like LoadRunner) and my needs are this framework will work with Continuous Integration tools like Jenkins/Hudson and an in-house Test Management tool for reporting results.

I searched for this specific scenario but could not find one. I know there will be numerous integrations, plug-ins, etc... that needs to be built. My question is can you provide some pointers (even good reads is OK) towards beginning to build this framework with Open source solutions ?

8

There are 8 best solutions below

0
On

We are begining to develop something very related to your needs; Java, Webdriver, Jenkins, Maven, etc. We are quite new to automation here, but still have good Java ressources. We are builing our framework based on Tarun Kumar from www.seleniumtests.com. He's got a lot of good videos from Youtube (sounds quality is not so good), and he manage to create something very user friendly, using PageObjects Pattern. If you don't have any clue where to start, I would start from there. Good luck!

0
On

Selenium WebDriver is surely a tool for UI automation and we use it extensively to do cross Browser testing on Cloud Solutions like Browser Stack.

Our use case let us build an open source Framework "omelet" built in Java using TestNG as test runner , which takes care of almost everything related to web-testing and leaves us to actually automated application rather than thinking about reports , parallel run and CI integration etc.

Suggestion, Contribution always welcome :)

Documentation over here and Github link over here

Do remember to checkout 5 min tutorial on website

0
On

For Functional Regression test:

Selenium Webdriver - Selenium a Web based automation tool that automates anything and everything available on a Web page. you use Selenium Webdriver with JAVA.

Watij- Web Application Testing in Java Automates functional testing of web applications through real web browsers.

TestProject - It supports for testing both web and Mobile (Android & iOS).

For Non-functional test:

Gatling- For performance testing and Stress testing

Apache JMeter - For Volume, Performance, Load & Stress testing

CI tool:

Jenkins- Jenkins provides continuous integration services for software development.

0
On

I created a java library on the top of selenium which simplifies test automation of a website. It has an implicit waiting mechanism and is easy to use:

https://github.com/gartenkralle/web-ui-automation

Example:

import org.junit.Test;
import org.openqa.selenium.By;

import common.UserInterface;
import common.TestBase;

public class Google extends TestBase
{
    private final static String GOOGLE_URL = "https://www.google.com/";

    private final static By SEARCH_FIELD = By.xpath("//input[@id='lst-ib']");
    private final static By AUTO_COMPLETION_LIST_BOX = By.xpath("//*[@id='sbtc']/div[2][not(contains(@style,'none'))]");
    private final static By SEARCH_BUTTON = By.xpath("//input[@name='btnK']");

    @Test
    public void weatherSearch()
    {
        UserInterface.Action.visitUrl(GOOGLE_URL);
        UserInterface.Action.fillField(SEARCH_FIELD, "weather");
        UserInterface.Verify.appeared(AUTO_COMPLETION_LIST_BOX);
        UserInterface.Action.pressEscape();
        UserInterface.Action.clickElement(SEARCH_BUTTON);
    }
}
0
On

For Functional Regression test:

TestProject
Selenium
Cucumber : It's a BDD tool

For Non-functional: Performance and Load testing:

JMeter

Note: TestComplete is a very good commercial tool.

0
On

I am giving here framework functions which reduces code very much

public TestBase() throws Exception{
    baseProp = new Properties();
    baseProp.load(EDCPreRegistration.class.getResourceAsStream("baseproperties.properties"));

    // Firefox profile creation
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
    profile.setPreference("browser.cache.disk.enable", false); 

    profile.setPreference("network.proxy.http", "localhost");
    profile.setPreference("network.proxy.http_port",8080);
    driver = new FirefoxDriver(profile);
    //System.setProperty("webdriver.ie.driver","E:\\Phyweb Webdriver\\IEDriverServer.exe");
    //driver = new InternetExplorerDriver();
    driver.manage().window().maximize();
}

 //To find WebElement by id
  public static WebElement FindElement(String id)
  {
      try
      {
            webElement= driver.findElement(By.id(id));
      }
      catch(Exception e)
      {
          Print(e);
      }
      return webElement;
  }

  //To find WebElement by name
  public static WebElement FindElementByName(String name)
  {
      try
      {
            webElement= driver.findElement(By.name(name));
      }
      catch(Exception e)
      {
          Print(e);
      }
       return webElement;
  }

  //To find WebElement by Class
  public static WebElement FindElementByClass(String classname)
  {
      try
      {
            webElement= driver.findElement(By.className(classname));
      }
      catch(Exception e)
      {
          Print(e);
      }
       return webElement;
  }

//To get data of a cell
public static String GetCellData(XSSFSheet sheet,int row,int col)
{
    String cellData = null;
    try 
    {
        cellData=PhyWebUtil.getValueFromExcel(row, col, sheet);
    }
    catch (Exception e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return cellData;
}

    //To click a button using id
    public static void ClickButton(String id,String label)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                PrintMessage(label+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    }   

            //To click a button using class
            public void ClickButtonByClass(String classname,String label)
            {
                try
                {
                        WebElement webElement= FindElementByClass(classname);
                        Snooze();
                        webElement.click();
                        PrintMessage(label+" is selected");
                }
                catch(Exception e)
                {
                    Print(e);
                }
            }   
     //To enter data into Textbox
     public String editTextField(int rownum, int celnum,WebElement element ,XSSFSheet sheet,String Label)
      {
            XSSFRow row = sheet.getRow(rownum);
            XSSFCell Cell = row.getCell(celnum);
            String inputValue = Cell.getStringCellValue().trim();
            element.clear();//To clear contents if present
            try
            {
                      element.sendKeys(inputValue);
                      String  elementVal=element.toString();
                      if(elementVal.contains("password"))
                      {   
                          PrintMessage("Password is entered");
                      }
                      else
                      {   
                          PrintMessage("Value entered for "+Label+" is "+inputValue);
                      }  
            }
            catch(Exception e){
                Print(e);
                //cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
            }
            return inputValue;
      }

    //To enter data into Textbox
     public String editTextFieldDirect(WebElement element ,String text,String label)
      {
            element.clear();//To clear contents if present
            try
            {
                      element.sendKeys(text);
                      String  elementVal=element.toString();
                      if(elementVal.contains("password"))
                      {   
                          PrintMessage("Password is entered");
                      }
                      else
                      {   
                          PrintMessage("Value entered for "+label+" is "+text);
                      }  
            }
            catch(Exception e){
                Print(e);
                //cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
            }
            return text;
      }
        //To select Radio button
        public void ClickRadioButton(String id)
        {
            try
            {
                    WebElement webElement= FindElement(id);
                    Snooze();
                    webElement.click();                 
                    text=webElement.getText();          
                    PrintMessage(text+" is selected");
            }
            catch(Exception e)
            {
                Print(e);
            }
        } 

    //To select Link
    public void ClickLink(String id,String label)
    {
        try
        {
                ClickButton(id,label);
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

  //To Click an Image button
    public void ClickImage(String xpath)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                text=GetText(webElement);
                PrintMessage(text+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

    //Select a checkbox
    public void CheckboxSelect(String id,String label)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                PrintMessage("Checkbox "+label+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

    //To select value in Combobox
    public void SelectData(String id,String label,String cellval)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                String elementStr=webElement.toString();
                int itemIndex=elementStr.indexOf("value");
                if(itemIndex>-1)
                {   
                    int endIndex=elementStr.length()-3;
                    String item=elementStr.substring(itemIndex+7, endIndex);
                    if(cellval=="0")
                    {   
                         PrintMessage(item+" is selected for "+label);
                    }
                    else
                    {
                        PrintMessage(cellval+"  "+label+" is selected");
                    }
                }   
                else
                {
                    PrintMessage(cellval+" is selected for "+label);
                }
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

      //To check if WebElement with id exists
      public static boolean isExists(String id) 
      {
          boolean exists = false;
          driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
              try 
              {
                       exists=driver.findElements( By.id(id) ).size() != 0;    
              } 
              catch (Exception e) 
              {
                  Print(e);
             }
              if(exists==true)


               return true;


              else


               return false;
      }

      //To check if WebElement with name exists
      public static boolean isExistsName(String name) 
      {
          boolean exists = false;
          driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
              try 
              {
                       exists=driver.findElements( By.name(name) ).size() != 0;    
              } 
              catch (Exception e) 
              {
                  if(e.getMessage().contains("InvalidSelectorError"))
                  {   
                      System.out.println("");
                  }
                  else
                      Print(e);
             }
              if(exists==true)


               return true;


              else


               return false;
      }

        //Explicit wait until a element is visible and enabled using id
        public void ExplicitlyWait(String id)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                            .until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

        //Explicit wait until a element is visible and enabled using classname
        public void ExplicitlyWaitByClass(String classname)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                            .until(ExpectedConditions.presenceOfElementLocated(By.className(classname)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

        //Explicit wait until a element is visible and enabled using id
        public void ExplicitlyWaitSpecific(int sec,String id)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, sec))
                            .until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

    //Snooze for 10 seconds
    public static void Snooze()
      {
          try
          {
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          }
          catch(Exception e)
          {
              Print(e);
          }
      }

    //Snooze for Secs
    public static void SnoozeSpecific(int seconds)
      {
          try
          {
              driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
          }
          catch(Exception e)
          {
              Print(e);
          }
      }

    //Sleep for milliSeconds
    public static void Sleep(int milisec) throws InterruptedException
    {
        Thread.sleep(milisec);
    }

    //To get text using text()
     public static String GetText(WebElement element)
     {
         try
         {  
             text=element.getText();
         }   
         catch(Exception e){


             Print(e);
         }
        return text;
     }

     //To get text using getAttribute("value")
     public static String GetTextAttribute(WebElement element)
     {
         try
         {  
             text=element.getAttribute("value");
         }   
         catch(Exception e){


             Print(e);
         }
        return text;
  }
     //To Print error messages to both Console and Results file
     public static void Print(Exception e)
     {
         Reporter.log("Exception is :"+e.getMessage());
         System.out.println(e);
     }

     //To Print messages to both Console and Results file
     public static void PrintMessage(String str)
     {
         Reporter.log(str);
         System.out.println(str);
     }

    //To Print Blank row
     public static void BlankRow()
     {
         Reporter.log("                                              ");
         System.out.println("                                              ");
     }

    //To Print Sub header
     public static void Header(String str)
     {
         BlankRow();
         Reporter.log("***********************"+str+" Verifications***********************");
         System.out.println("***********************"+str+" Verifications***********************");
         BlankRow();
     }

    //To Print Sub header
     public static void SubHeader(String str)
     {
         BlankRow();
         Reporter.log("-----------------------"+str+" Verifications-----------------------");
         System.out.println("-----------------------"+str+" Verifications-----------------------");
         BlankRow();
     }
1
On
  • Selenium will allow you to automate all your web (browsers) actions automations.
  • Junit/TestNG as the testing framework, including their default reports system
  • Maven for the project management and lifecycle (including test phase with surefire plugin)
  • Jenkins is a good integration tool that will easily run the setup above

Good luck!

1
On

So long as you have a command line for kicking off your framework and you report back using the xunit log format then you should be good for integration with any number of Continuous integration frameworks.

Your trade off on running a browser instance under load will be fewer virtual users per host and a very careful examination of your load generator resources under load. Don't forget to include monitoring API in your framework for system metrics under load and an auto evaluation engine related to SLA metrics acceptance to determine pass of fail criteria under load at a given load point.