WrapperMethods:- Selenium functional reusable methods

Class Name:- Wrappermethods.java

public class WrapperMethods {
WebDriver driver;
int i=0;
private String testCaseName;
private int dataSet;

public WrapperMethods(String testCaseName, int dataSet){
    this.testCaseName = testCaseName;
    this.dataSet = dataSet;
    System.setProperty("atu.reporter.config", "atu.properties");
}

public WebDriver invokeApplication(String browser, String url,String desc, String expResult, String actResult) throws IOException{

    ExcelReporter.createReportHeader(testCaseName);
    try {
        if(browser.toLowerCase().equals("ie")){
            System.setProperty("webdriver.ie.driver", "..\\drivers\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }
        else if(browser.toLowerCase().equals("chrome")){
            System.setProperty("webdriver.chrome.driver", "..\\drivers\\ChromeDriver.exe");
            driver = new ChromeDriver();
        }
        else{
            driver = new FirefoxDriver();
        }

        ATUReports.setWebDriver(driver);
        ATUReports.indexPageDescription = "TestLeaf Demo Project";

        driver.get(url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        TC001_Login.reportStep(desc,url,"SUCCESS", expResult, actResult);
    } catch (WebDriverException exception) {
        TC001_Login.reportStep(desc,url,"FAILURE",expResult, actResult);
    }

    return driver;
}

public void enterValueById(String id, String value,String desc, String expResult, String actResult) throws IOException{
    try {
        driver.findElement(By.id(id)).clear();
        WebElement element=driver.findElement(By.id(id));
        element.sendKeys(value);
        // ATUReports.add("Enter the URL", "Kalyann", "Kumar", "Sample", LogAs.PASSED, new CaptureScreen(element));
        TC001_Login.reportStep(desc,value,"SUCCESS", expResult, actResult);

    } catch (NoSuchElementException exc) {
        TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
    } catch (WebDriverException e) {
        TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
    } finally{         
        takeSnapshot();
    }

}

public void enterValueByName(String name, String value,String desc, String expResult, String actResult) throws IOException{
    try {
        driver.findElement(By.name(name)).clear();
        driver.findElement(By.name(name)).sendKeys(value);
        TC001_Login.reportStep(desc,value,"SUCCESS",expResult, actResult);

    } catch (NoSuchElementException exc) {
        TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);

    } catch (WebDriverException exception){
        TC001_Login.reportStep(desc,value,"FAILURE",expResult, actResult);
    } finally{
        takeSnapshot();
    }
}

public void selectDropdownValueById(String id, int index) throws IOException{
        try {
            WebElement element = driver.findElement(By.id(id));
            Select dropDownElement = new Select(element);
            dropDownElement.selectByIndex(index);
            ExcelReporter.reportStep("Element with id :"+id+" is found and index :"+index+" selected successfully..","SUCCESS");
        } catch (NoSuchElementException exception) {
            ExcelReporter.reportStep("Element with id :"+id+"could not found..","FAILURE");
        } catch (WebDriverException e){
            ExcelReporter.reportStep("Driver could not found !!!","FAILURE");
        } finally{
            takeSnapshot();
        }
}

public void linkClickByText(String text,String desc, String expResult, String actResult) throws IOException{

        try {
            driver.findElement(By.linkText(text)).click();
            TC001_Login.reportStep(desc,null,"SUCCESS", expResult, actResult);
        } catch (NoSuchElementException e) {
            TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
        } catch (WebDriverException exe){
            TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
        } finally{
            takeSnapshot();
        }
}

public void clickByCSS(String css,String desc, String expResult, String actResult) throws IOException{

    try {
        driver.findElement(By.cssSelector(css)).click();
        TC001_Login.reportStep(desc,null,"SUCCESS", expResult, actResult);
    } catch (NoSuchElementException e) {
        TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
    } catch (WebDriverException exe){
        TC001_Login.reportStep(desc,null,"FAILURE", expResult, actResult);
    } finally{
        takeSnapshot();
    }


}

public void closeApplication(String desc, String expResult, String actResult) throws IOException{

    try {
        driver.quit();
        TC001_Login.reportStep(desc,null,"SUCCESS",expResult, actResult);
    } catch (WebDriverException exe){
        TC001_Login.reportStep(desc,null,"FAILURE",expResult, actResult);
    }  

    ExcelReporter.flushWorkbook(testCaseName+"-Run"+dataSet);


}
public void takeSnapshot() throws IOException{

    try {
        File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src, new File("..\\reports\\snaps\\snapshot"+ i +".png"));
        i++;
    } catch (IOException ioe) {
        // TODO Auto-generated catch block
        ExcelReporter.reportStep("Unable to copy the file !!!!","FAILURE");
    }
}
}

Comments

Popular posts from this blog

Online Tricentis Tosca Automation Training with Real Time Scenarios

Online Selenium Training With Real Time Scenario

How to move application from development server to production server