Opening Eclipse editor from python with Py4J - python

I'm trying open a file in Eclipse editor from my python program.
Here is example how to do this with Java:
import java.io.File;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
File fileToOpen = new File("externalfile.xml");
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}
I'm trying to use this API from python with use of Py4j.
from py4j.java_gateway import JavaGateway, java_import
gateway = JavaGateway()
jvm = gateway.jvm
java_import(jvm, 'org.eclipse.core.filesystem.EFS')
java_import(jvm, 'org.eclipse.ui.PlatformUI')
fileToOpen = jvm.java.io.File('c:/test.txt')
fileStore = jvm.org.eclipse.core.filesystem.EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
This works fine. But I have stacked with getting page.
page = jvm.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
I'm getting None here. Looks like Rudolf Widmann have posted answer for this question here. So the java solution will be:
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
});
But how can I do this from python? How to implement it with Py4j?

Did you try to implement the Runnable interface in Python?
class PythonRunner(object):
def __init__(self, gateway):
self.gateway = gateway
self.iw
def run(self):
self.iw = gateway.jvm.org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow()
class Java:
implements = ['java.lang.Runnable']
gateway = JavaGateway(start_callback_server=True)
runner = PythonRunner(gateway)
gateway.jvm.org.eclipse.swt.widgets.Display.getDefault().asyncExec(runner)
# Replace with a synchronization primitive
time.sleep(2)
page = runner.iw.getActivePage()

Related

PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library

Ok I'm fairly new to swift and am trying to create an app with a button that uses python. I have seem code looking like
//
// ContentView.swift
// Shared
//
// Created by Ulto4 on 10/17/21.
//
import SwiftUI
import PythonKit
struct ContentView: View {
#State private var showDetails = false
#State var result : String = " "
var body: some View {
HStack{
Text("Hello, world!")
.padding()
Button(action : {
self.coolPerson()
}, label: {
Text("Respones")
})
Text("\(result)")
}
}
func coolPerson(){
let sys = Python.import("sys")
sys.path.append("/Users/ulto4/Documents/vsc")
let example = Python.import("ahhhhh")
let response = example.hi()
result = response.description
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}
The code in the python file is
def hello():
return "cool"
However, when I click the button I get this error
2021-10-17 17:53:16.943097-0700 GAAIN[27059:2277939] PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
(lldb)
I also clicked the .xcodeproj and deleted the Apple Sandbox. But it still doesn't work. Since I'm fairly new I don't know how to do this. Any help would be appreciated.
EDIT: As per the comments, IOS doesn't support PythonKIT

How do you record user's audio in Streamlit shearing?

I'm looking to deploy a small data collection app that allows users to record themselves saying some phrases.
Building this with streamlit.
I can get it to work locally, but can't seem to find a solution that works in the Streamlit-Shearing service.
Any ideas?
I do not fully understand your question I think this might help.
import streamlit as st
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events
stt_button = Button(label="Speak", width=100)
stt_button.js_on_event("button_click", CustomJS(code="""
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function (e) {
var value = "";
for (var i = e.resultIndex; i < e.results.length; ++i) {
if (e.results[i].isFinal) {
value += e.results[i][0].transcript;
}
}
if ( value != "") {
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
}
}
recognition.start();
"""))
result = streamlit_bokeh_events(
stt_button,
events="GET_TEXT",
key="listen",
refresh_on_update=False,
override_height=75,
debounce_time=0)
if result:
if "GET_TEXT" in result:
st.write(result.get("GET_TEXT"))`
I have been working on implementing a custom component that enables recording audio from the client’s microphone in apps that are deployed on the web (Streamlit-Audio-Recorder). This version of the component supports downloading the recorded audio but not directly returning audio data to streamlit/Python.

Python Kivy/Pyjnius android NotificationListenerService

I want to create an application for android with kivy in python, which Listens for notification.
I created a notification_listener.py:
from kivy import platform
if platform == "android":
from jnius import autoclass, cast, PythonJavaClass, java_method
from android.runnable import run_on_ui_thread
PythonActivity = autoclass("org.kivy.android.PythonActivity")
Activity = autoclass("android.app.Activity")
Context = autoclass("android.content.Context")
NotificationListenerService = autoclass("android.service.notification.NotificationListenerService")
StatusBarNotification = autoclass("android.service.notification.StatusBarNotification")
Log = autoclass("android.util.Log")
Toast = autoclass("android.widget.Toast")
String = autoclass("java.lang.string")
CharSequence = autoclass("java.lang.CharSequence")
activity = PythonActivity.mActivity
currentActivity = cast(Activity, activity)
context = cast(Context, currentActivity.getApplicationContext())
class NotificationListener(PythonJavaClass):
__javaclass__ = "android.service.notification.NotificationListenerService"
__javacontext__ = "app"
#java_method("()V")
def onCreate(self):
super(NotificationListener, self).onCreate()
text = cast(CharSequence, String("Listener started..."))
toast = Toast.makeText(context, text, Toast.LENGTH_LONG)
toast.show()
#java_method("(Landroid/service/notification/StatusBarNotification)V")
def onNotificationPosted(self, sbn):
notification = cast(StatusBarNotification, sbn)
extras = notification.getNotification().extras
tag = String("Notification recived")
msg_title = String("title: %s" % (extras.getString("android.title")))
msg_text = String("text: %s" % (extras.getString("android.text")))
Log.v(tag, msg_title)
Log.v(tag, msg_text)
text = cast(CharSequence, String("Notification recieved..."))
toast = Toast.makeText(context, text, Toast.LENGTH_LONG)
toast.show()
but how do I have to add the to the AndroidManifest.xml?
if I would do it in Java, the following code would be correct but its a python file, so how do I have to implement it? 🤔
<service name=".NotificationListener"
android:label="notification_listener"
android:permissions="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Recently I made it work, so I am writing here about my experience.
Buildozer does not provide any options for registering a service with permissions and an intent-filter. However, you can customize AndroidManifest.xml from the template file which is located in your .buildozer directory.
/your/project/home/.buildozer/android/platform/build-armeabi-v7a/dists/yourapp__armeabi-v7a/templates/AndroidManifest.tmpl.xml
You can insert the xml block between "end-if" and "if" so that it won't be changed by the buildozer.spec settings.
Next thing you need to make is a java class file. Unfortunately, python class cannot be bound to NotificationListeners, so you need to manually copy a java class that receives StatusBarNotifications.
The path is going to be:
/your/project/home/.buildozer/android/platform/build-armeabi-v7a/dists/yourapp__armeabi-v7a/src/main/java/your/app/domain/NotificationListener.java
With the java class, you can broadcast the StatusBarNotification to the python side,getApplicationContext().sendBroadcast(intent);, and p4a's android.broadcast.BroadcastReceiver will receive the object on main.py.
Java class
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
try {
Bundle bundle = new Bundle();
bundle.putParcelable("sbn",sbn);
Intent intent = new Intent("action_name_matches_with_java_side");
intent.putExtra("sbn",sbn);
getApplicationContext().sendBroadcast(intent);
} catch(Exception e) {
Log.v("KIVYAPP","Notification broadcasting prohibited");
}
}
main.py
class YourApp(App):
def build(self):
...
self.br = BroadcastReceiver(self.onReceive,actions=["action_name_matches_with_java_side"])
self.br.start()
...
def onReceive(self, context, intent):
bundle = cast(Bundle, intent.getExtras())
sbn = cast(StatusBarNotification,bundle.getParcelable("sbn"))
...

IronPython | Redirect Output To Textbox

Ok so, let's say I am executing a script with IronPython in C#:
ScriptEngine engine = Python.CreateEngine();
string Script = textBox1.Text();
engine.Execute(Script);
Now let's say the script was:
print("Hello")
Output in VS Console: Hello
What I am trying to achieve here is to redirect that output to a TextBox in C#. I have tried the following method which didn't work:
class TextBoxWriter : TextWriter
{
private RichTextBox _textBox;
public TextBoxWriter(RichTextBox textbox)
{
_textBox = textbox;
}
public override void Write(char value)
{
base.Write(value);
_textBox.AppendText(value.ToString());
}
public override System.Text.Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
Form1_Load:
engine.Runtime.IO.RedirectToConsole();
TextWriter _writer = TextWriter.Synchronized(new TextBoxWriter(textBox1));
Console.SetOut(_writer);
I have just that in my code and its functional for me.
engine.Runtime.IO.RedirectToConsole();
Console.SetOut(new TextBoxWriter(textBox1));

save_screenshot saves a 0kb file [duplicate]

Is it possible to take a screenshot using Selenium WebDriver?
(Note: Not Selenium Remote Control)
Java
Yes, it is possible. The following example is in Java:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Python
Each WebDriver has a .save_screenshot(filename) method. So for Firefox, it can be used like this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
Confusingly, a .get_screenshot_as_file(filename) method also exists that does the same thing.
There are also methods for: .get_screenshot_as_base64() (for embedding in HTML) and .get_screenshot_as_png()(for retrieving binary data).
And note that WebElements have a .screenshot() method that works similarly, but only captures the selected element.
C#
public void TakeScreenshot()
{
try
{
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(#"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
JavaScript (Selenium-Webdriver)
driver.takeScreenshot().then(function(data){
var base64Data = data.replace(/^data:image\/png;base64,/,"")
fs.writeFile("out.png", base64Data, 'base64', function(err) {
if(err) console.log(err);
});
});
Ruby
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :ie
driver.get "https://www.google.com"
driver.save_screenshot("./screen.png")
More file types and options are available and you can see them in file takes_screenshot.rb.
Java
I got this issue resolved. You can augment the RemoteWebDriver to give it all of the interfaces its proxied driver implements:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((TakesScreenshot)augmentedDriver).getScreenshotAs(...); // It works this way
PHP (PHPUnit)
It uses PHPUnit_Selenium extension version 1.2.7:
class MyTestClass extends PHPUnit_Extensions_Selenium2TestCase {
...
public function screenshot($filepath) {
$filedata = $this->currentScreenshot();
file_put_contents($filepath, $filedata);
}
public function testSomething() {
$this->screenshot('/path/to/screenshot.png');
}
...
}
C#
public Bitmap TakeScreenshot(By by) {
// 1. Make screenshot of all screen
var screenshotDriver = _selenium as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
IWebElement element = FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
return bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
}
Java
public String captureScreen() {
String path;
try {
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
path = "./target/screenshots/" + source.getName();
FileUtils.copyFile(source, new File(path));
}
catch(IOException e) {
path = "Failed to capture screenshot: " + e.getMessage();
}
return path;
}
Jython
import org.openqa.selenium.OutputType as OutputType
import org.apache.commons.io.FileUtils as FileUtils
import java.io.File as File
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver
self.driver = FirefoxDriver()
tempfile = self.driver.getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(tempfile, File("C:\\screenshot.png"))
Java (Robot Framework)
I used this method for taking a screenshot.
void takeScreenShotMethod(){
try{
Thread.sleep(10000)
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/screenshot.jpg"));
}
catch(Exception e){
e.printStackTrace();
}
}
You may use this method wherever required.
Java
Seems to be missing here - taking screenshot of a specific element in Java:
public void takeScreenshotElement(WebElement element) throws IOException {
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height);
Point location = element.getLocation();
BufferedImage bufferedImage = ImageIO.read(screenshot);
BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
ImageIO.write(destImage, "png", screenshot);
File file = new File("//path//to");
FileUtils.copyFile(screenshot, file);
}
C#
using System;
using OpenQA.Selenium.PhantomJS;
using System.Drawing.Imaging;
namespace example.com
{
class Program
{
public static PhantomJSDriver driver;
public static void Main(string[] args)
{
driver = new PhantomJSDriver();
driver.Manage().Window.Size = new System.Drawing.Size(1280, 1024);
driver.Navigate().GoToUrl("http://www.example.com/");
driver.GetScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);
driver.Quit();
}
}
}
It requires NuGet packages:
PhantomJS 2.0.0
Selenium.Support 2.48.2
Selenium.WebDriver 2.48.2
It was Tested with .NET Framework v4.5.2.
PowerShell
Set-Location PATH:\to\selenium
Add-Type -Path "Selenium.WebDriverBackedSelenium.dll"
Add-Type -Path "ThoughtWorks.Selenium.Core.dll"
Add-Type -Path "WebDriver.dll"
Add-Type -Path "WebDriver.Support.dll"
$driver = New-Object OpenQA.Selenium.PhantomJS.PhantomJSDriver
$driver.Navigate().GoToUrl("https://www.google.co.uk/")
# Take a screenshot and save it to filename
$filename = Join-Path (Get-Location).Path "01_GoogleLandingPage.png"
$screenshot = $driver.GetScreenshot()
$screenshot.SaveAsFile($filename, [System.Drawing.Imaging.ImageFormat]::Png)
Other drivers...
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
$driver = New-Object OpenQA.Selenium.IE.InternetExplorerDriver
$driver = New-Object OpenQA.Selenium.Opera.OperaDriver
C#
public static void TakeScreenshot(IWebDriver driver, String filename)
{
// Take a screenshot and save it to filename
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(filename, ImageFormat.Png);
}
Java
I could not get the accepted answer to work, but as per the current WebDriver documentation, the following worked fine for me with Java 7 on OS X v10.9 (Mavericks):
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}
There are multiple methods through Selenium's Java and Python client to take a screenshot using Selenium WebDriver.
Java Methods
The following are the different Java methods to take a screenshot:
Using getScreenshotAs() from the TakesScreenshot interface:
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Firefox_takesScreenshot {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
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"));
driver.quit();
}
}
Screenshot:
If the webpage is jQuery enabled, you can use ashot from the pazone/ashot library:
Code block:
package screenShot;
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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_Firefox {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
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/firefoxScreenshot.png"));
driver.quit();
}
}
Screenshot:
Using selenium-shutterbug from assertthat/selenium-shutterbug library:
Code block:
package screenShot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.assertthat.selenium_shutterbug.core.Shutterbug;
import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy;
public class selenium_shutterbug_fullpage_firefox {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save("./Screenshots/");
driver.quit();
}
}
Screenshot:
Python Methods
The following are the different Python methods to take a screenshot:
Using save_screenshot() method:
Code block:
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://google.com")
driver.save_screenshot('./Screenshots/save_screenshot_method.png')
driver.quit()
Screenshot:
Using the get_screenshot_as_file() method:
Code block:
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://google.com")
driver.get_screenshot_as_file('./Screenshots/get_screenshot_as_file_method.png')
driver.quit()
Screenshot:
Using get_screenshot_as_png() method:
Code block:
from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://google.com")
screenPnG = driver.get_screenshot_as_png()
# Crop it back to the window size (it may be taller)
box = (0, 0, 1366, 728)
im = Image.open(BytesIO(screenPnG))
region = im.crop(box)
region.save('./Screenshots/get_screenshot_as_png_method.png', 'PNG', optimize=True, quality=95)
driver.quit()
Screenshot:
Ruby (Cucumber)
After do |scenario|
if(scenario.failed?)
puts "after step is executed"
end
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
Given /^snapshot$/ do
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
Ruby
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M_%S')
file_path = File.expand_path(File.dirname(__FILE__) + 'screens_shot')+'/'+time +'.png'
#driver.save_screenshot(file_path)
page.driver.browser.save_screenshot file_path
PHP
public function takescreenshot($event)
{
$errorFolder = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "ErrorScreenshot";
if(!file_exists($errorFolder)){
mkdir($errorFolder);
}
if (4 === $event->getResult()) {
$driver = $this->getSession()->getDriver();
$screenshot = $driver->getWebDriverSession()->screenshot();
file_put_contents($errorFolder . DIRECTORY_SEPARATOR . 'Error_' . time() . '.png', base64_decode($screenshot));
}
}
Java
Using RemoteWebDriver, after augmenting the Node with screenshot capability, I would store the screenshot like so:
void takeScreenShotMethod(){
try{
Thread.sleep(10000);
long id = Thread.currentThread().getId();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
+ id + "/screenshot.jpg"));
}
catch( Exception e ) {
e.printStackTrace();
}
}
You may use this method wherever required. Then, I assume you can customize the style sheet of maven-surefire-report-plugin at surefire-reports/html/custom.css so that your reports include the link to the correct screenshot for each test?
Java
String yourfilepath = "E:\\username\\Selenium_Workspace\\foldername";
// Take a snapshort
File snapshort_file = ((TakesScreenshot) mWebDriver)
.getScreenshotAs(OutputType.FILE);
// Copy the file into folder
FileUtils.copyFile(snapshort_file, new File(yourfilepath));
Java
public void captureScreenShot(String obj) throws IOException {
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("Screenshots\\" + obj + "" + GetTimeStampValue() + ".png"));
}
public String GetTimeStampValue()throws IOException{
Calendar cal = Calendar.getInstance();
Date time = cal.getTime();
String timestamp = time.toString();
System.out.println(timestamp);
String systime = timestamp.replace(":", "-");
System.out.println(systime);
return systime;
}
Using these two methods you can take a screen shot with the date and time as well.
Selenese
captureEntirePageScreenshot | /path/to/filename.png | background=#ccffdd
Python
def test_url(self):
self.driver.get("https://www.google.com/")
self.driver.save_screenshot("test.jpg")
It will save a screenshot in the same directory the where script is saved.
You can give a try to AShot API. It is on GitHub.
Examples of tests.
C#
You can use the following code snippet/function to take screenshot with Selenium:
public void TakeScreenshot(IWebDriver driver, string path = #"output")
{
var cantakescreenshot = (driver as ITakesScreenshot) != null;
if (!cantakescreenshot)
return;
var filename = string.Empty + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
filename = path + #"\" + filename + ".png";
var ss = ((ITakesScreenshot)driver).GetScreenshot();
var screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
ss.SaveAsFile(filename, ImageFormat.Png);
}
Java
A method to capture a screenshot for the failures in Selenium with TestName and Timestamp appended.
public class Screenshot{
final static String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static String imgname = null;
/*
* Method to Capture Screenshot for the failures in Selenium with TestName and Timestamp appended.
*/
public static void getSnapShot(WebDriver wb, String testcaseName) throws Exception {
try {
String imgpath = System.getProperty("user.dir").concat("\\Screenshot\\"+testcaseName);
File f = new File(imgpath);
if(!f.exists()) {
f.mkdir();
}
Date d = new Date();
SimpleDateFormat sd = new SimpleDateFormat("dd_MM_yy_HH_mm_ss_a");
String timestamp = sd.format(d);
imgname = imgpath + "\\" + timestamp + ".png";
// Snapshot code
TakesScreenshot snpobj = ((TakesScreenshot)wb);
File srcfile = snpobj.getScreenshotAs(OutputType.FILE);
File destFile = new File(imgname);
FileUtils.copyFile(srcfile, destFile);
}
catch(Exception e) {
e.getMessage();
}
}
C# (Ranorex API)
public static void ClickButton()
{
try
{
// code
}
catch (Exception e)
{
TestReport.Setup(ReportLevel.Debug, "myReport.rxlog", true);
Report.Screenshot();
throw (e);
}
}
You can create a webdriverbacked selenium object using the Webdriverclass object, and then you can take a screenshot.

Categories