Code not executing and not getting screenshot | Selenium Forum
R
Rupa Posted on 07/05/2020
package com.qtpselenium.hybrid.keywords;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.qtpselenium.hybrid.util.ExtentManager;

public class GenericKeywords {
	public Properties prop;
	public Properties envProp;
	public String objectKey;
	public String dataKey;
	public Hashtable<String, String> data;
	public WebDriver driver = null;
	public ExtentTest test;

	/************************* Keyword *************************/
	
	public void openBrowser() {
		String browserName = dataKey;
		test.log(Status.INFO, "Opening Browser" + browserName);
		if (browserName.equals("Chrome")) {
			driver = new ChromeDriver();
		} else if (browserName.equals("Firefox")) {
			driver = new FirefoxDriver();
		}
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

	}

	public void navigate()  {

		String navigatingURL = envProp.getProperty("url");

		test.log(Status.INFO, "Navigating to:" + navigatingURL);
		driver.get(navigatingURL);
	}

	public void closeBrowser()  {

		test.log(Status.INFO, "Closing Browser");
		driver.quit();
	}

	public void click() throws Exception {
		String elementLocator = prop.getProperty(objectKey);
		test.log(Status.INFO, "Clicking:" + elementLocator);
		try{
			getObject().click();
		}catch(Exception e){
			reportFailure(e.getMessage());
		}
	}

	public void type() throws Exception {
		String elementLocator = prop.getProperty(objectKey);
		test.log(Status.INFO, "Typing" + data.get(dataKey) + " inside " + elementLocator);
		try{
			getObject().sendKeys(data.get(dataKey));
		}catch(Exception e){
			reportFailure(e.getMessage());
		}
	}
	
	
	/************************* Utility *************************/
	
	public WebElement getObject(){
		WebElement retrievedElement=null;
		if(objectKey.endsWith("_xpath")){
				retrievedElement=driver.findElement(By.xpath(prop.getProperty(objectKey)));
		}else if (objectKey.endsWith("_cssSelector")) {
			
			


retrievedElement=driver.findElement(By.cssSelector(prop.getProperty(objectKey)));
			
		}else if (objectKey.endsWith("_id")) {
			retrievedElement=driver.findElement(By.id(prop.getProperty(objectKey)));
						
		}else if (objectKey.endsWith("_name")){
			retrievedElement=driver.findElement(By.name(prop.getProperty(objectKey)));
		}
	
		WebDriverWait wait=new WebDriverWait(driver, 15);
		wait.until(ExpectedConditions.visibilityOf(retrievedElement));
		wait.until(ExpectedConditions.elementToBeClickable(retrievedElement));
		return retrievedElement;
	}
	
	public void reportFailure(String errorMessage) throws Exception{
		takeScreenshot();
		test.log(Status.FAIL, errorMessage);
		Assert.fail(errorMessage);
	}

	public void takeScreenshot() throws Exception {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy_MM_dd_z_HH_mm_ss");
		String screenshotFileName=sdf.format(new Date()+".png");
		File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		FileUtils.copyFile(srcFile,new File(ExtentManager.screenshotsFolderPath+screenshotFileName));
		test.log(Status.INFO, "Screenshot File----------------------->"+test.addScreenCaptureFromPath(ExtentManager.screenshotsFolderPath+screenshotFileName,"Screenshot"));
	
	}

	public void setTest(ExtentTest test) {
		this.test = test;
	}

	public void setData(Hashtable<String, String> data) {
		this.data = data;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public void setEnvProp(Properties envProp) {
		this.envProp = envProp;
	}

	public void setObjectKey(String objectKey) {
		this.objectKey = objectKey;
	}

	public void setDataKey(String dataKey) {
		this.dataKey = dataKey;
	}

}

S
Support Team Replied on 07/05/2020

DOes it throw any error?


R
Rupa Replied on 07/05/2020

 

No error msg


S
Support Team Replied on 08/05/2020

Only way out of such sitautations is debugging

Put breakpoints in your framework and please debug them one by one


Related Posts