Is there any way we can control the size of the image attached in testNG report? - selenium-webdriver

I have been trying to capture Screenshots for Passed/Failed test in my testNG reports captured in Test-Output folder. I have succeeded in doing so, however I want a bit more improvement in the way things are right now.
The Screenshot is displayed in full size and covers the whole screen, I would like to make it appear smaller in report and then user can click on it to see the image maximized.
I am capturing the Screenshot with the help of Reporter.log(ScreenshotPath) in the static TakeScreenshot method which is a part of finally block in my test case.
public static void TakeScreenshotMethod() throws IOException {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File ScreenshotName = new File(UtilityClass.ScreenshotPathCustomerPortal + count +".jpg");
FileUtils.copyFile(scrFile, ScreenshotName);
String filePath = ScreenshotName.toString();
String path = "<img src=\"file://" + filePath + "\" alt=\"\"/>";
Reporter.log(path);
count++;
}
I would like to control the size of the image being displayed in my report.

You can a bit modify the <img> tag by adding a style like normal html with some property (width, height), like this:
String path = "<img src=\"file://" + filePath + "\" alt=\"\" style=\"width: 230px; height :500px;\" />";

If you are asking for this behavior in the default TestNG reports, then the answer is NO its not possible. TestNG reports are agnostic to the content that gets embedded in them. So TestNG doesn't know if an image or if a video for that matter is being embedded in it. So it also doesn't different on how to get them rendered.
If you would like to control these behaviors, you should do one of the following:
Build your own custom reporter that has this behavior by implementing the org.testng.IReporter interface and then wiring in this listener via the <listeners> tag or via #Listeners interface or via a Service Provider Interface mechanism. You can learn about wiring in listeners in general from my blog post here.
You can explore some of the reporting solutions that exist already out there such as allure reports (or) extent reports etc., and see if one of them fits your requirement and use them instead.
TestNG doesn't have any role to play in this.

Related

JBehave + TestNG + Selenium Grid - generate jbehave index.html report file for each seperate selenium node when using single jbehave story file

I have a JBehave project in which need to integrate with TestNG and Selenium grid which using this, this and this code in github (sorry since i cant past the entire code so only showing the reference) i have done the JBehave + TestNG + Selenium Grid.
But my problem is when using single Story file to execute in different Selenium node the jbehave report index.html file is shown for any one node only. I want to have separate report for each node in a single or more jbehave report index.html file. I should not run with two story files and all, how can i show separate report for each and every Selenium node in a single jbehave report html file.
I know the jbehave use freemarker for their report generation but i have no clue on how to override this and show report for each selenium nodes. Any idea please share.
Thanks in Advance.
I dont think combining Jbehave and TestNG is a better idea. Jbehave supports the stories and using testng, there would be of no use, like grouping, parameter,etc. There are existing JunitStories class which works fine with Jbehave. If your using it for organizing your test results, you can use customized Allure reporting tool for jbehave (link). You could use Jbehave + Allure + Selenium Grid architecture.
Finally tried a way and found it to be an temporary solution for it now.
Create a String variable that gets the details in Story Runner Class.
RemoteWebDriver driver = (RemoteWebDriver) DriverManager.getDriver();
String hostname = hng.getHostName(driver.getSessionId());
String browserName = driver.getCapabilities().getBrowserName();
String browserVersion = driver.getCapabilities().getVersion();
Then pass that value to the Story Embedded class as shown below.
Embedder storyEmbedder = new StoryEmbedder(driver, browserName + "v" + browserVersion);
In the Story Embedded class assign that String value as shown below
private WebDriver driver;
private static String name;
public StoryEmbedder(WebDriver driver, String hostname) {
this.driver = driver;
this.name = hostname;
}
Then in the Configuration method inside the useStoryReporterBuilder function add the following code.
.withRelativeDirectory(name) //where 'name' is the String variable refer above step.
like the return will be as follows
return new MostUsefulConfiguration()
.useStoryControls(new StoryControls().doDryRun(false).doSkipScenariosAfterFailure(false))
.useStoryLoader(
new LoadFromClasspath(embedderClass))
.useStoryParser(
new RegexStoryParser(
examplesTableFactory))
.useStoryPathResolver(new UnderscoredCamelCaseResolver())
.useStoryReporterBuilder(
new StoryReporterBuilder().withCodeLocation(CodeLocations.codeLocationFromClass(embedderClass))
.withDefaultFormats().withPathResolver(new ResolveToPackagedName())
.withViewResources(viewResources).withReporters(new MyStoryReporter())
.withFormats(Format.CONSOLE, Format.TXT, Format.HTML, Format.XML).withFailureTrace(true)
.withFailureTraceCompression(true).withCrossReference(xref).withRelativeDirectory(name)).useParameterConverters(parameterConverters)
// use '%' instead of '$' to identify parameters
.useStepPatternParser(new RegexPrefixCapturingPatternParser("%"));
Now i can have two folders based on browser and browser version.
If you guys have any better answer please do help by posting it.
Thanks in Advance.

WPF native windows 10 toasts

Using .NET WPF and Windows 10, is there a way to push a local toast notification onto the action center using c#? I've only seen people making custom dialogs for that but there must be a way to do it through the os.
You can use a NotifyIcon from System.Windows.Forms namespace like this:
class Test
{
private readonly NotifyIcon _notifyIcon;
public Test()
{
_notifyIcon = new NotifyIcon();
// Extracts your app's icon and uses it as notify icon
_notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
// Hides the icon when the notification is closed
_notifyIcon.BalloonTipClosed += (s, e) => _notifyIcon.Visible = false;
}
public void ShowNotification()
{
_notifyIcon.Visible = true;
// Shows a notification with specified message and title
_notifyIcon.ShowBalloonTip(3000, "Title", "Message", ToolTipIcon.Info);
}
}
This should work since .NET Framework 1.1. Refer to this MSDN page for parameters of ShowBalloonTip.
As I found out, the first parameter of ShowBalloonTip (in my example that would be 3000 milliseconds) is generously ignored. Comments are appreciated ;)
I know this is an old post but I thought this might help someone that stumbles on this as I did when attempting to get Toast Notifications to work on Win 10.
This seems to be good outline to follow -
Send a local toast notification from desktop C# apps
I used that link along with this great blog post- Pop a Toast Notification in WPF using Win 10 APIs
to get my WPF app working on Win10. This is a much better solution vs the "old school" notify icon because you can add buttons to complete specific actions within your toasts even after the notification has entered the action center.
Note- the first link mentions "If you are using WiX" but it's really a requirement. You must create and install your Wix setup project before you Toasts will work. As the appUserModelId for your app needs to be registered first. The second link does not mention this unless you read my comments within it.
TIP- Once your app is installed you can verify the AppUserModelId by running this command on the run line shell:appsfolder . Make sure you are in the details view, next click View , Choose Details and ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.
Here's a snipit of code that I used. One thing two note here, I did not install the "Notifications library" mentioned in step 7 of the first link because I prefer to use the raw XML.
private const String APP_ID = "YourCompanyName.YourAppName";
public static void CreateToast()
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));
// Specify the absolute path to an image
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + #"\Your Path To File\Your Image Name.png";
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;
// Change default audio if desired - ref - https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
XmlElement audio = toastXml.CreateElement("audio");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
//audio.SetAttribute("loop", "false");
// Add the audio element
toastXml.DocumentElement.AppendChild(audio);
XmlElement actions = toastXml.CreateElement("actions");
toastXml.DocumentElement.AppendChild(actions);
// Create a simple button to display on the toast
XmlElement action = toastXml.CreateElement("action");
actions.AppendChild(action);
action.SetAttribute("content", "Show details");
action.SetAttribute("arguments", "viewdetails");
// Create the toast
ToastNotification toast = new ToastNotification(toastXml);
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
UPDATE
This seems to be working fine on windows 10
https://msdn.microsoft.com/library/windows/apps/windows.ui.notifications.toastnotificationmanager.aspx
you will need to add these nugets
Install-Package WindowsAPICodePack-Core
Install-Package WindowsAPICodePack-Shell
Add reference to:
C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
And
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
And use the following code:
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
string imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("Toast Sample").Show(toast);
The original code can be found here: https://www.michaelcrump.net/pop-toast-notification-in-wpf/
I managed to gain access to the working API for windows 8 and 10 by referencing
Windows.winmd:
C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral
This exposes Windows.UI.Notifications.
You can have a look at this post for creating a COM server that is needed in order to have notifications persisted in the AC with Win32 apps https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/.
A working sample can be found at https://github.com/WindowsNotifications/desktop-toasts

How to show an image from an image inputstream in Birt?

I am using birt to develop some reports,I want to display some pie charts in the birt pdf report,I found the birt chart function is not very flexiable,so I want to use jfreechart to generate an image and show it in the report instead. Now I have a question: can we show image in birt with the image inputstream,so that the image do not need to be generated and store in some place?
I know I can generate the image into a jpg file and store it in some place ,then I can access it birt report via the location of this image file.But if I do as that ,each time when we view the birt report ,it will generate a new image file,I do not want to generate so much files.
Any good ideas?
There are two possible solutions.
As you said, you can save the charts as files.
I do this in a report where I generate dozens of control charts (German: "Regelkarten"). To tidy up after the report has finished, I add the generated file names to a list, then I delete the files in the list in the report's afterRender script like this.
var images = vars["images"];
for (var i=0; i<images.size(); i++) {
var fname = images.get(i);
if (new java.io.File(fname)['delete']()) {
log.debug("Temp. image file removed: " + fname);
} else {
log.error("Could not delete temp. image file " + fname);
}
}
Note: This is OK for generating PDF or Word, but probably it wouldn't work for HTML.
In my case, I did this because I could check the generated charts outside of BIRT by just commenting out the clean up routine.
The other option is to generate a byte[]. BIRT complains in the log file then (Rhino, something about unknown type mapping Java<->Javascript), but it works.
I never have used this with JFreeChart, but that's how we do it for generating DataMatrix ("2D-Barcode") with an ancient commercial Java library from a company called "ID Automation".
The image item references row["dataMatrix_AuftID"], which is defined as type: Java Object, expression: barcode.dataMatrix(row["AUFT_ID"],70,70,80,0.03)
The function barcode.dataMatrix in our Javascript wrapper library for the Java library calls the Java library. Its last few lines look like this, which should give you an idea for your case:
var image = new java.awt.image.BufferedImage(siz.width, siz.height, t);
// get graphic context of image
var imgGraphics = image.createGraphics();
// paint DataMatrix in graphics context of image
bc.paint(imgGraphics);
// get the image's bytes in PNG format
var baos = new java.io.ByteArrayOutputStream();
javax.imageio.ImageIO.write(image, "png", baos);
var bytes = baos.toByteArray();
return bytes;
As a side-note, for a proof of concept I showed that it is also possible to generate SVG in memory and use it with BIRT. Never used this in production, however.

Download file with JSF

everyone!
I have a trouble. I tried to save excel file in jsf web application.
I generated file by my utils and trying to get "save" window, but I failed.
Here is my code:
<div>
<h:commandButton value="Apply" actionListener="#{hornPhonesBean.generateReport}"/>
</div>
and:
public void generateReport(ActionEvent event) {
System.out.println("GENERATE REPORT FROM = " + this.dateFrom + "; TO = " + this.dateTo);
try {
XSSFWorkbook workbook = (XSSFWorkbook) HornReportGenerator.getWorkbook(null, null);
String fileName = "1.xlsx";
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
// Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
ec.responseReset();
// Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
ec.setResponseContentType("application/vnd.ms-excel");
// Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
//ec.setResponseContentLength(contentLength);
// The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream output = ec.getResponseOutputStream();
workbook.write(output);
output.flush();
output.close();
fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
System.out.println("END");
} catch (Exception e) {
e.printStackTrace();
}
}
I read suggestions here and from another forums - everyone says I shouldnt use , but I didn't use it at all.
Then I thought that the problem could be in the
<ice:form>,
where I kept the
<h:commandButton>,
and I changed to
<h:form>,
but it didn't help.
Maybe the problem in the request - it has header Faces-Request partial/ajax. But I am not sure.
Please give me some ideas - I already spent 4 hours for this crazy jsf download issue)
Maybe the problem in the request - it has header Faces-Request partial/ajax. But I am not sure.
This suggests that the request is an ajax request. You can't download files by ajax. Ajax requests are processed by JavaScript which has for obvious security reasons no facilities to programmatically pop a Save As dialogue nor to access/manipulate client's disk file system.
Your code snippet does however not show that you're using ajax. Perhaps you oversimplified it too much or you're using ICEfaces which silently auto-enables ajax on all standard JSF command components.
In any case, you need to make sure that it's not sending an ajax request.
See also:
How to provide a file download from a JSF backing bean?
ICEfaces libary in classpath prevents Save As dialog from popping up on file download

Silverlight: Business Application Needs Access To Files To Print and Move

I have the following requirement for a business application:
(All of this could be on local or server)
Allow user to select folder location
Show contents of folder
Print selected items from folder (*.pdf)
Display which files have been printed
Potentially move printed files to new location (sub-folder of printed)
How can I make this happen in Silverlight?
Kind regards,
ribald
First of all, all but the last item can be done (the way you expect). Due to security protocols, silverlight cannot access the user's drive and manipulate it. The closest you can get is accessing silverlight's application storage which will be of no help to you whatsoever in this case. I will highlight how to do the first 4 items.
Allow user to select folder location & Show contents of folder
public void OnSelectPDF(object sender)
{
//create the open file dialog
OpenFileDialog ofg = new OpenFileDialog();
//filter to show only pdf files
ofg.Filter = "PDF Files|*.pdf";
ofg.ShowDialog();
byte[] _import_file = new byte[0];
//once a file is selected proceed
if (!object.ReferenceEquals(ofg.File, null))
{
try
{
fs = ofg.File.OpenRead();
_import_file = new byte[fs.Length];
fs.Read(_import_file, 0, (int)fs.Length);
}
catch (Exception ex)
{
}
finally
{
if (!object.ReferenceEquals(fs, null))
fs.Close();
}
//do stuff with file - such as upload the file to the server
};
}
If you noticed, in my example, once the file is retrieved, i suggest uploading it to a webserver or somewhere with temporary public access. I would recommend doing this via a web service. E.g
//configure the system file (customn class)
TSystemFile objFile = new TNetworkFile().Initialize();
//get the file description from the Open File Dialog (ofg)
objFile.Description = ofg.File.Extension.Contains(".") ? ofg.File.Extension : "." + ofg.File.Extension;
objFile.FileData = _import_file;
objFile.FileName = ofg.File.Name;
//upload the file
MasterService.ToolingInterface.UploadTemporaryFileAsync(objFile);
Once this file is uploaded, on the async result, most likely returning the temporary file name and upload location, I would foward the call to some javascript method in the browser for it to use the generic "download.aspx?fileName=givenFileName" technique to force a download on the users system which would take care of both saving to a new location and printing. Which is what your are seeking.
Example of the javascript technique (remember to include System.Windows.Browser):
public void OnInvokeDownload(string _destination)
{
//call the browser method/jquery method
//(I use constants to centralize the names of the respective browser methods)
try
{
HtmlWindow window = HtmlPage.Window;
//where BM_INVOKE_DOWNLOAD is something like "invokeDownload"
window.Invoke(Constants.TBrowserMethods.BM_INVOKE_DOWNLOAD, new object[] { _destination});
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
Ensure you have the javascript method existing either in an included javaScript file or in the same hosting page as your silverlight app. E.g:
function invokeDownload(_destination) {
//some fancy jquery or just the traditional document.location change here
//open a popup window to http://www.myurl.com/downloads/download.aspx? fileName=_destination
}
The code for download.aspx is outside the scope of my answer, as it varies per need and would just lengthen this post (A LOT MORE). But from what I've given, it will "work" for what you're looking for, but maybe not in exactly the way you expected. However, remember that this is primarily due to silverlight restrictions. What this approach does is rather than forcing you to need a pluging to view pdf files in your app, it allows the user computer to play it's part by using the existing adobe pdf reader. In silverlight, most printing, at least to my knowledge is done my using what you call and "ImageVisual" which is a UIElement. To print a pdf directly from silverlight, you need to either be viewing that PDF in a silverlight control, or ask a web service to render the PDF as an image and then place that image in a control. Only then could you print directly. I presented this approach as a lot more clean and direct approach.
One note - with the temp directory, i would recommend doing a clean up by some timespan of the files on the server side everytime a file is being added. Saves you the work of running some task periodically to check the folder and remove old files. ;)

Resources