Thursday, 26 December 2019

Complete framework using WebDriver with Java


                                                                                                       By  Sudhakar T

Dear Reader’s,

I am posting here complete framework using Selenium WebDriver using Java code for Acti time application. 

Pre requisite to execute this script is need to download and install older version of Acti time which



GenericUtilLibrary of Acti time Frame work

package com.actitime.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.Alert;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class GenericUtilLibrary {

 public static String getExcelData(String xlPath,String sheetName,int rowNum,int cellNum)
          {
                   try
                   {
                   FileInputStream fis = new FileInputStream(xlPath);

                   Workbook wb = WorkbookFactory.create(fis);

                   String value = wb.getSheet(sheetName).getRow(rowNum).
getCell(cellNum).getStringCellValue();

                   return value;
                   }
                   catch(Exception e)
                   {
                             return " ";
                   }
          }
          public static void acceptAlert(WebDriver driver)
          {
                   Alert alt = driver.switchTo().alert();

                   alt.accept();
          }
          public static void rejectAlert(WebDriver driver)
          {
                   Alert alt = driver.switchTo().alert();

                   alt.dismiss();
          }
          public static void expWait(int millSec)
          {
                   try
                   {
                   Thread.sleep(millSec);    
                   }
                   catch(Exception e)
                   {
                            
                   }
          }

public static void takeScreenShot(WebDriver driver,String imgPath) throws IOException
          {
                   EventFiringWebDriver efDriver = new EventFiringWebDriver(driver);

                   File srcFile = efDriver.getScreenshotAs(OutputType.FILE);

                   File destFile = new File(imgPath);

                   FileUtils.copyFile(srcFile, destFile);
          }

}





Login Page of Acti time Frame work

package com.actitime.objectpage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {

          WebDriver driver;

          @FindBy(name="username")
          private WebElement userTextbox;

          @FindBy(xpath="//input[@type='password']")
          private WebElement pwdTextbox;

          @FindBy(id="loginButton")
          private WebElement loginButton;

          public LoginPage(WebDriver driver)
          {
                   this.driver = driver;
                   PageFactory.initElements(driverthis);
          }
          public void login(String un,String pwd)
          {
                   userTextbox.sendKeys(un);
                   pwdTextbox.sendKeys(pwd);

                   loginButton.click();
          }

}




Base Page of Acti time Frame work


package com.actitime.objectpage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;

public class BasePage {

          WebDriver driver;

          @FindBy(linkText="Logout")
          private WebElement logoutLink;

          @FindBy(xpath="//div[text()='Settings']")
          private WebElement settingsTab;

          @FindBy(linkText="Billing Types")
          private WebElement billingTypesLink;

          @FindBy(className="errormsg")
          private WebElement errorMsg;

          @FindBy(className="successmsg")
          private WebElement successMsg;

          public BasePage(WebDriver driver)
          {
                   this.driver = driver;
                   PageFactory.initElements(driver, this);
          }
          public void clickOnLogout()
          {
                   logoutLink.click();
          }
          public void gotoBillingTypes()
          {
                   settingsTab.click();

                   billingTypesLink.click();
          }
          public void verfiyerrorMsg(String expRes)
          {
                   String actRes = errorMsg.getText();
                   Assert.assertEquals(actRes, expRes);

System.out.println("Actual msg "+actRes+" matches with "+expRes+" expMsg");
          }
          public void verfiySuccesMsg(String expRes)
          {
                   String actRes = successMsg.getText();

                   Assert.assertEquals(actRes, expRes);

System.out.println("Actual msg "+actRes+" matches with "+expRes+" expMsg");
          }

}


Billing Types Page of Acti time Frame work

package com.actitime.objectpage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.actitime.util.GenericUtilLibrary;

public class BillingTypes extends BasePage{
          WebDriver driver;
          @FindBy(xpath="//input[@value='Create New Billing Type']")
          private WebElement createNewBillTypeButton;
          public BillingTypes(WebDriver driver)
          {
                   super(driver);
                   this.driver = driver;
                   PageFactory.initElements(driver, this);
          }
          public void gotoCreateNewBillTypePage()
          {
                   createNewBillTypeButton.click();
          }
          public void deleteBillType(String billNameType)
          {
                   String xpath = "//tr[td[a[text()='"+billNameType+"']]]//a[contains(text(),'delete')]";
                   driver.findElement(By.xpath(xpath)).click();
                   GenericUtilLibrary.acceptAlert(driver);
          }

}
Create New Billing Type Page of Acti time Frame work


package com.actitime.objectpage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class CreateNewBillingType extends BasePage{
          WebDriver driver;

          @FindBy(id="name")
          private WebElement billTypeName;

          @FindBy(id="billingType_setBillable")
          private WebElement billAbleRadioButton;

          @FindBy(id="billingType_setNonBillable")
          private WebElement nonbillAbleRadioButton;

          @FindBy(xpath="//input[@value='   Create Billing Type   ']")
          private WebElement createBillTypeButton;

          @FindBy(xpath="//input[@value='      Cancel      ']")
          private WebElement cancelButton;

          public CreateNewBillingType(WebDriver driver)
          {
                   super(driver);
                   this.driver = driver;
                   PageFactory.initElements(driver, this);
          }
          public void createNewBill(String billName)
          {
                   billTypeName.sendKeys(billName);

                   billAbleRadioButton.click();
                   createBillTypeButton.click();
          }
         

}



Super TestNG Class  of Acti time Frame work


package com.actitime.scripts;

import java.io.File;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.io.Zip;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

import com.actitime.util.GenericUtilLibrary;

public class SuperTestNGClass {

          WebDriver driver;

          @BeforeSuite
          public void cleanUp()
          {
                   try
                   {
                   FileUtils.deleteDirectory(new File("./test-output"));
                   }
                   catch(Exception e)
                   {
                            
                   }
          }
          @AfterSuite
          public void backUp()
          {
                   Date d = new Date();

                   String currentDate = d.toString().replace(":", "_");

                   Zip z = new Zip();

                   try
                   {
                      z.zip((new File("./test-output")), (new File("./"+currentDate+".zip")));
                   }
                   catch(Exception e)
                   {
                            
                   }
          }

            @BeforeMethod
          public void preCond()
          {
String url = GenericUtilLibrary.getExcelData("./UserFile/testdata.xlsx", "config", 0, 1);

                   driver = new FirefoxDriver();

                   driver.manage().window().maximize();

                   //Waits for 30 seconds max. or till Web Element is found.                  
                   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   driver.get(url);
                  
          }
          @AfterMethod
          public void postCond()
          {
                   //Closes only all browser windows opened by this driver instance.
                   driver.quit();
          }

}


CreateNewBillType Class  of Acti time Frame work


package com.actitime.scripts;

import org.testng.annotations.Test;

import com.actitime.objectpage.BillingTypes;
import com.actitime.objectpage.CreateNewBillingType;

import com.actitime.objectpage.LoginPage;

public class At_CreateNewBillType extends SuperTestNGClass{
          @Test
          public void testCreateBillType()
          {
                   LoginPage lp = new LoginPage(driver);
                   lp.login("admin", "manager");

                   BillingTypes btp = new BillingTypes(driver);
                   btp.gotoBillingTypes();
                   btp.gotoCreateNewBillTypePage();

                   CreateNewBillingType cnbt = new CreateNewBillingType(driver);
                    cnbt.createNewBill("contract1");

                   String expRes = "Billing type has been successfully created.";
                   btp.verfiySuccesMsg(expRes);

                   btp.clickOnLogout();
                  
                  
          }

}


Delete Bill Type Class  of Acti time Frame work


package com.actitime.scripts;

import org.testng.annotations.Test;

import com.actitime.objectpage.BillingTypes;
import com.actitime.objectpage.LoginPage;

public class At_DeleteBill extends SuperTestNGClass{
          @Test
          public void testDeleteBill()
          {
                  
                   LoginPage lp = new LoginPage(driver);
                    lp.login("admin", "manager");

                   BillingTypes bt = new BillingTypes(driver);
                   bt.gotoBillingTypes();
                   bt.deleteBillType("contract");

                   bt.verfiySuccesMsg("Billing type has been successfully deleted.");

                   bt.clickOnLogout();
          }

}