Wednesday, May 7, 2014

When to Automate Software - Part 1

Problem

There are times in the application development when new feature and/or functionality keep adding into the existing software and there are functionality that are unchanged but have its own value in the application, sometimes such functionality becomes a bit boring for the testers to test over and over again after each feature functionality integration and that increases the chances of defect slippage, testers started to make assumptions and based on those assumption she might skip some key tests in that functionality that results in defect in later stages.

Solution

The solution for the above problems in which similar tests need to be executed again and again it is better we should automate those tests that are repeatable. Each time the test is going to be executed its value increases and confidence of each run will remain unshakable as it was in the first run.

See the following example, in which the form Reset functionality needs to be tested over and over again since it is one of the basic feature the EfroTech site provides.

Scenario
  • Launch application
  • Click on Jobs
  • Fill up the form
  • Click Reset button

Expected: All fields on the form are reset to default except Salary fields.

Test Case Class

 package com;  

 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.support.ui.Select;  

 public class EfroTechCareers {  
      private WebDriver driver;  
      public EfroTechCareers(WebDriver driver)  
      {  
           this.driver = driver;  
           if(!"Jobs and Career at Efrotech | Efrotech Services".equals(driver.getTitle()))  
           {  
                throw new IllegalStateException("This is not the Careers Page");  
           }  
      }  
      By applyingFor = By.id("ddlJobs");  
      By candidateName = By.id("txtName");  
      By candidateFatherName = By.id("txtFName");  
      By candidateGender = By.id("ddlGender");  
      By candidateAddress = By.id("txtAddress");  
      By candidateCity = By.id("ddlResumeCity");  
      By candidateEmail = By.id("txtEmail");  
      By candidateHomePhone = By.id("txtPhoneHome");  
      By candidateOfficePhone = By.id("txtPhoneOff");  
      By candidateCellPhone = By.id("txtPhoneMob");  
      By candidateCNIC = By.id("txtCNIC");  
      By candidateExperience = By.id("ddlExperience");  
      By candidateCurrentSalary = By.id("txtCurrentSalary");  
      By candidateExpectedSalary = By.id("txtExpectedSalary");  
      By btnFormReset = By.id("btnReset");  
      public void formFillCareers()   
      {  
           driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[height='520']")));  
           new Select(driver.findElement(applyingFor)).selectByVisibleText("QA Engineer");  
           driver.findElement(candidateName).sendKeys("Candidate Name");  
           driver.findElement(candidateFatherName).sendKeys("Cand. Father's Name");  
           new Select(driver.findElement(candidateGender)).selectByVisibleText("Male");  
           driver.findElement(candidateAddress).sendKeys("XYZ Lane 123 Road");  
           new Select(driver.findElement(candidateCity)).selectByVisibleText("Karachi");  
           driver.findElement(candidateEmail).sendKeys("name@domainname.com");  
           driver.findElement(candidateHomePhone).sendKeys("12345678");  
           driver.findElement(candidateOfficePhone).sendKeys("87654321");  
           driver.findElement(candidateCellPhone).sendKeys("03331234567");  
           driver.findElement(candidateCNIC).sendKeys("12345-6789458-9");  
           new Select(driver.findElement(candidateExperience)).selectByVisibleText("5 Years");  
           driver.findElement(candidateCurrentSalary).sendKeys("123456");  
           driver.findElement(candidateExpectedSalary).sendKeys("654123");  
           driver.findElement(btnFormReset).click();  
      }  
      public boolean verifyFormReset()  
      {  
           if (  
                     ("-Select-".equals(new Select(driver.findElement(applyingFor)).getFirstSelectedOption().getText()) &&    
                     "".equals(driver.findElement(candidateName).getText()) &&  
                     "".equals(driver.findElement(candidateFatherName).getText()) &&   
                     "-Select-".equals(new Select(driver.findElement(candidateGender)).getFirstSelectedOption().getText()) &&   
                     "".equals(driver.findElement(candidateAddress).getText()) &&  
                     "-Select-".equals(new Select(driver.findElement(candidateCity)).getFirstSelectedOption().getText()) &&   
                     "".equals(driver.findElement(candidateEmail).getText()) &&   
                     "".equals(driver.findElement(candidateHomePhone).getText()) &&   
                     "".equals(driver.findElement(candidateOfficePhone).getText()) &&   
                     "".equals(driver.findElement(candidateCellPhone).getText()) &&  
                     "".equals(driver.findElement(candidateCNIC).getText()) &&  
                     "-Select-".equals(new Select(driver.findElement(candidateExperience)).getFirstSelectedOption().getText()) &&  
                     "012345".equals(driver.findElement(candidateCurrentSalary).getAttribute("value")) &&  
                     "065412".equals(driver.findElement(candidateExpectedSalary).getAttribute("value"))   
                     )  
            )  
                          return true;  
                                         else   
                                              return false;  
      }  

 } 


 

Main Class Calling the Test Case Class

 package com;  

 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.openqa.selenium.support.PageFactory;  

 public class EfroTechMain {  
      /**  
       * @param args  
       *   
       */  
      public static WebDriver driver;  
      public static String baseUrl;  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           baseUrl = "http://www.efrotech.com";  
           driver = new FirefoxDriver();  
           driver.manage().window().maximize();  
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
           driver.get(baseUrl +"/careers");  
           EfroTechCareers eTC = PageFactory.initElements(driver,                            EfroTechCareers.class);  
           eTC.formFillCareers();  
           if (eTC.verifyFormReset() == true)  
           {  
                System.out.println("All fields are reset to defaults");  
           } else  
                System.out.println("Some fields are not reset to defaults");  
      }  

 }


The above scenario of the scripted/automated tests can be used to solve the problem we started with discussion in this post. The test can be repeated as many time as the need arises. Its value keep increases and cost decreases as many time we execute the tests.

What approach you take while automating the test. Do share here if it is worth sharing.

No comments:

Post a Comment