How to capture a screenshot using Krypton using Selenium Java - java

I would like to seek help if there is a possible way / code to capture a screenshot under Krypton platform that uses JAVA selenium. I'm having trouble in terms of it's standardization. Thanks!
var driver = new ChromeDriver()
driver.get("https://login.bws.birst.com/login.html/")
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"))

As per the screenshots there seems to be some discrepency with the imports/Classes. Further to keep things simpler create destination folder within the Project Scope e.g. the Screenshots directory below:
To capture a screenshot using Java you can use the following solution:
Code Block:
package screenShot;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class takesScreenshot {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://login.bws.birst.com/login.html/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Birst"));
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Mads_Cruz_screenshot.png"));
}
}
Captured Image:

Related

Taking full page screen shot of really long webpages using Ashot, Selenium Java Client, ChromeDriver

the URL i am trying to take a screenshot is https://www.smashingmagazine.com/2017/05/long-scrolling/
Attached is the screenshot obtained from Ashot. [screenshot from ashot] - https://i.stack.imgur.com/CdeYf.jpg (notice it is incomplete)
I feel like its a memory issue and not a bug. Any pointers would be appreciated.
This is the code I am using to take screenshots.
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.smashingmagazine.com/2017/05/long-scrolling/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}

How to use tor browser using selenium webdriver (java)? I have tried below code so far but getting message: 'tor failed to start'

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class torr1 {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette",
"C:\\Users\\ghorh\\Documents\\selenium-bazinga\\Drivers\\geckodriver.exe");
String torPath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://google.co.in");
}
}
I have tried the above code so far but getting message: 'tor failed to start'. Could somebody please help on what changes are required for the above code.
You are trying to use FireFox driver.
Try use TorBrowserDriver as specified in its readme: https://github.com/webfp/tor-browser-selenium
Or if you want to use firefox - use GeckoDriver that can be downloaded from the next link:
https://github.com/mozilla/geckodriver/releases/tag/v0.26.0

How disable debuger on chrome webdriver in Java?

My target application use <script type="text/javascript">debugger;</script> So my page is blocked by break point.
How disable debugger on chrome webdriver in Java?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
}
}
I think, add javascipt code (revert of debugger;) by this:
((JavascriptExecutor) driver).executeScript("...");
EDIT WITH HEADLESS MODE + SCREENSHOT:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) throws IOException {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.forceMkdir(new File(System.getProperty("user.dir") + File.separator + "downloadFiles"));
FileUtils.writeByteArrayToFile(new File(System.getProperty("user.dir") + File.separator + "downloadFiles" + File.separator + "bot.jpg"), screenshot);
}
}
The result is same, my sceenshot is white.

How to take full page screenshot using AShot library through Selenium and Java

I tried the below code for taking full page screenshot. But only the visible area is captured,
public void Fullscreen (WebDriver driver)
{
try {
final Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();
ImageIO.write(image, "PNG", new File("D:\\" + "AShot_BBC_Entire.png"));
} catch(Exception e){
System.out.println(e.getMessage());
}
}
While working with Selenium Java Client v3.14.0, ChromeDriver v2.41, Chrome v 68.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/:
Code Block:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://jquery.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}
Screenshots:
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver
Want to add answer in case when you don't know what kind of screen is used. (retina or not)
In this case you need find devicePixelRatio of browser window:
Object output = ((JavascriptExecutor) webDriver).executeScript("return window.devicePixelRatio");
String value = String.valueOf(output);
float windowDPR = Float.parseFloat(value);
Then you can use ShootingStrategy with scaling;
ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)

phantomjs not capturing screenshot from https website

I am trying to capture simple screenshot using phantomjs for https://www.google.com . Screenshot is coming as blank . I am using eclipse in windows and jars phantomjsdriver1.1 , selenium jars all 2.39 version .
When I mention site as http://google , it redirects to https and captures screenshot . But I need it to exclusively capture it for https only . Below is my code . Thanks in advance .
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.anthavio.phanbedder.Phanbedder;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Schedule {
public static void main(String[] args) {
File phantomjs = Phanbedder.unpack();
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
dcaps.setCapability("takesScreenshot", true);
String [] phantomJsArgs = {"---ignore-ssl-errors=yes"};
dcaps.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
phantomJsArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("C:\\Users\\Desktop\\fci\\sample.jpeg"),true);
} catch (IOException e) {
System.out.println("exception");
}
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
PhantomJS is a headless browser meaning; you don't get to see what is happening; everything that's done is done in background.

Categories