Not getting expected screenshots | Selenium Forum
B
barik.arun Posted on 09/09/2020

Hi, I copied the code from the downloaded code and using it. I am getting a different result than expected. This is the code for Screenshot and Test Base . Also attached is the screenshots. Please do let me know if I am doing wrong anywhere.

/***********ScreenShot code************************/

package RadioButtonsAndScreenshots;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

 


import base.TestBase;

public class TakeScreenshots extends TestBase{
public WebDriver driver=null;
@Test
public void screenshot() {
System.setProperty("webdriver.chrome.driver", "C:\\Tools\\TestingTools\\drivers\\chromedriver.exe");

driver=launchBrowser("chrome");
driver.get("https://www.chabadpotomac.com/templates/articlecco_cdo/aid/1094542/jewish/HS-Registration-Form-New-Student.htm");

takeScreenShot("C:\\Tools\\TestingTools\\sample.jpg");
WebElement e = driver.findElement(By.xpath("//*[@id='menu']"));


getElementScreenshot(e, "C:\\Tools\\TestingTools\\img.jpg");

}

public void takeScreenShot(String filePath){
// take screenshot- java object
// save screenshot in path
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// get the dynamic folder name
FileUtils.copyFile(srcFile, new File(filePath));
//test.addScreenCaptureFromPath("path of image", "xxxx");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


public void getElementScreenshot(WebElement ele, String filePath)
{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg;
try {
fullImg = ImageIO.read(screenshot);
// Get the location of element on the page , 100,150
Point point = ele.getLocation();
System.out.println("x co-ordinates " + ele.getLocation().getX());
System.out.println("y co-ordinate "+ ele.getLocation().getY());
// Get width and height of the element -50,100
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);

// Copy the element screenshot to disk
File screenshotLocation = new File(filePath);
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}


}

 

 

/*************************TestBase Code**************************************/

package base;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.testng.annotations.Test;

public class TestBase {
WebDriver driver;

@Test
public WebDriver launchBrowser(String browser) {



if(browser.equals("mozilla")) {

System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "logs\\firefox.log");
FirefoxOptions options=new FirefoxOptions();
ProfilesIni prof=new ProfilesIni();
FirefoxProfile fireprof=new FirefoxProfile();
//manage webnotification
fireprof.setPreference("dom.webnotifications.enabled", false);

//manage ssl
//fireprof.setAcceptUntrustedCertificates(true);
//fireprof.setAssumeUntrustedCertificateIssuer(false);

// proxy setting
//fireprof.setPreference("network.proxy.type", 1);
//fireprof.setPreference("network.proxy.socks", "83.44.39.12");
//fireprof.setPreference("network.proxy.socks_port", 1823);
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
options.setProfile(fireprof);

driver=new FirefoxDriver(options);
}
if(browser.equals("chrome")) {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "logs\\chrome.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
ChromeOptions opt=new ChromeOptions();
opt.addArguments("--disable-notification");
opt.addArguments("--start-maximized");
opt.setCapability("pageLoadStrategy", "normal");

driver=new ChromeDriver(opt);
}
if(browser.equals("ie")) {
System.setProperty(InternetExplorerDriverService.IE_DRIVER_LOGLEVEL_PROPERTY, "INFO");
System.setProperty(InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY, "logs\\ie.log");

driver=new InternetExplorerDriver(); }






if(browser.equals("edge")) {
System.setProperty(EdgeDriverService.EDGE_DRIVER_LOG_PROPERTY,"logs\\edge.log");
System.setProperty(EdgeDriverService.EDGE_DRIVER_SILENT_OUTPUT_PROPERTY,"true");
System.setProperty("webdriver.edge.driver", "C:\\Tools\\TestingTools\\drivers\\msedgedriver.exe");

EdgeOptions options = new EdgeOptions();
//options.setBinary(new File(""));
options.addArguments("--disable-notifications");
options.addArguments("--start-maximized");
//options.addArguments("--proxy-server=http://83.209.94.87:8123");
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
options.addArguments("ignore-certificate-errors");


// NOTIFICATIONS and PROFILE


driver = new EdgeDriver(options);// make sure previous session is
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;

}

}


A
Ashish Replied on 11/09/2020

This can be browser issue, Please try updating the browser version as well as driver version and try again.


B
barik.arun Replied on 11/09/2020

Hi, Still getting the same result after updating the chrome driver. The version of the driver and browser I am using is

chrome browser-85.0.4183.102

chrome driver-85.0.4183.87


A
Ashish 5 days ago

Please export your project in a Compressed ZIP file and share it with us.


Related Posts