How to save an image to desktop using TestComplete? - jscript

I'm writing tests in JScript in TestComplete. I need to make a screenshot of a web page element, and save it to my desktop as a PNG file.
I tried this code:
var MyPicture = WebPage.SomeLocation.Picture();
MyPicture.SaveToFile("C:\Desktop");
which doesn't seem to be working, and I can't seem to figure out why. My program doesn't crash or anything, it simply doesn't save the picture. What am I doing wrong?

SaveToFile needs a full name of the image to create, including a path. Remember that in JScript you must double the backslashes in paths.
To get the desktop folder path, you can use the SpecialFolders property.
var MyPicture = WebPage.SomeLocation.Picture();
var strImageName = "MyPicture.png";
// Get the Desktop folder path
var strDesktop = Sys.OleObject("WScript.Shell").SpecialFolders("Desktop");
// Build the full path to the image
var strPath = aqFileSystem.IncludeTrailingBackSlash(strDesktop) + strImageName;
MyPicture.SaveToFile(strPath);

Related

How to get acces to file in the same folder of the game (Godot engine)

So i wanted to load external file in my godot game but I can't find any answer. I want to load file (sound, image, ...) in the same folder of the game (.exe).
By the way I can always do it directly include all the file in the engine but it's more easy to modify the external file than file in the editor
You can use OS.get_executable_path() to get the path of the executable. Except, it will give you the path to the Godot executable if you are running from the editor.
You can check if your code is running from the editor with OS.has_feature("standalone").
And of course, you don't really want the path to the executable, but to the folder it is in. So we use get_base_dir.
Like this:
if OS.has_feature("standalone"):
var folder := OS.get_executable_path().get_base_dir()
Also be aware of OS.get_user_data_dir() which gives you an appropriate folder for your game depending on the operating system (there is also where the "logs" folder will be). Plus OS.get_user_data_dir() will work even when running form the editor, and it gives you a folder path directly.
Then we can either build a path to another file in the same folder:
if OS.has_feature("standalone"):
var path := OS.get_executable_path().get_base_dir()
var file_name := folder.plus_file("my_file.png")
Or we can enumerate files:
if OS.has_feature("standalone"):
var path := OS.get_executable_path().get_base_dir()
var dir = Directory.new()
if dir.open(path) == OK:
dir.list_dir_begin()
var file_name:String = dir.get_next()
while file_name != "":
if not dir.current_is_dir():
if file_name.get_extension() == "png":
print("Found file: " + file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
Now, you want to load them. Except load et.al. won't work because they are not resources in your project.
You can read the contents of a file into a buffer like this:
var file := File.new()
file.open(file_name, File.READ)
var buffer := file.get_buffer(file.get_len())
file.close()
Then from the buffer you can create an Image like this:
var my_image := Image.new()
my_image.load_png_from_buffer(buffer)
If you happen to need a Texture you can do this:
var my_texture := ImageTexture.new()
my_texture.create_from_image(my_image)
I'll also mention that you can set the flags of the Texture.
And for sound…
I will point you to GDScriptAudioImport because, sadly, it is not as straightforward.
And as you can imagine anything else you want to load from external files will different code to support it.
By the way I can always do it directly include all the file in the engine but it's more easy to modify the external file than file in the editor
You can always edit files in the project folder with any other external software and when you switch back to Godot it will detect the changes and re-import. So you don't need to modify anything within Godot.
And for any other software modifying files in the project folder should not be any harder than modifying files located elsewhere.

Give file link in place of absolute path in fs.createReadStream();

I've a code like this
const fileData = fs.createReadStream('Google Sheet Link');
The error I am getting is this.
This is probably due to fs trying to read from an absolute path, rather than directly from the URL.
The path, it is taking is "Directory to which file is located" + "Google sheet link"
I only want to open google sheet link in the createReaderStream Path.
I've hidden the google sheet path due to privacy reasons.
The createReadStream() method is an inbuilt application programming interface of fs module which allow you to open up a file/stream and read the data present in it.
fs.createReadStream( path, options )
Parameters: This method accept two parameters as mentioned above and described below:
path: This parameter holds the path of the file where to read the file. It can be string, buffer or URL.
options: It is an optional parameter that holds string or object.
Now, To avoid paths issues, it's recommended to use path.join, like:
const path = require('path');
const pathGoogleSheet = path.join(__dirname, 'Sheet name');
const readableStream = fs.createReadStream(pathGoogleSheet);
Here, you're creating a path to the google sheet referring to the current directory of the script stored in global variable called __dirname.

How can i make 'text' to 'downloadable url'?

I make chrome extension, which download some files from website.
i want write some log with txt file, because my program has some error and i want save error to txt file.
but i can't find how to do it.
someone say it's impossible.
i try to search, some questions say just 'app' can do it, but extension can't.
if i want this functions, i must change my program to web app? but i don't know anything about that...
'download file' is possible, download is write file to filesystem.
i think, 'write text file' is so easy work then download image and sound files....
why not??? what's diffrent??
i see many questions, blog and document, but i cant know clearly.
*to sum up
I want make some text to downloadable file. If i can get downloadable file and it's url, everything is perfect.
i see this extension, i think i can do it. but i don't know how to do it.
please teach me how do it!
https://chrome.google.com/webstore/detail/save-text-to-file/mkepenkbhepjelljcfiooignmpfgochi/related
This is what I understand from your question that you want to write some text into .txt file and it should be automatically downloaded for your users. For that, here I am writing code in JavaScript which will surely helps you.
var fileText = "Your content which you want to save in file";
var fileBlob = new Blob([fileText], {
type: 'text/plain'
});
var fileUrl = URL.createObjectURL(fileBlob);
var fileName = 'mytextfile.txt';
var fileOptions = {
filename: fileName,
url: fileUrl,
conflictAction: 'uniquify'
};
fileOptions.saveAs = true;
It will automatically download mytextfile.txt file with your given contents. You can implement this logic in your chrome extension as per your way.

how to draw shapes in a PDF using NodeJS

I have some existing PDF files and what I want is to highlight some content by overlaying circles or straight lines. I've looked at some NodeJS PDF libraries but couldn't find a solution (some libraries allow creating a PDF from scratch and draw into it; other libraries can modify existing PDFs, but do not support drawing).
A (Linux / OSX) command line solution (e.g. using ImageMagick or some other library) would be perfectly fine, too.
Edit I've since found out that with Image/GraphicsMagick I can in fact do sth. like gm convert -draw "rectangle 20,20 150,100" xxx.pdf[7] xxx2.pdf, but this (1) either draws on all pages or else only on a single one, but then the resulting PDF will only contain that page; (2) the output PDF will contain a bitmap image where I would prefer a PDF with text content.
Edit I've just found HummusJS which is a NodeJS library to manipulate PDF files via declarative JSON objects. Unfortunately, apart from the scrace documentation, the unwieldy API (see below), the tests fail consistently and across the board with Unable to create PDF file, make sure that output file target is available.
completely OT not sure what it is that makes people think such utterly obfuscated APIs are better than simple ones:
var settings = {modifiedFilePath:'./output/BasicJPGImagesTestPageModified.pdf'}
var pdfWriter = hummus.createWriterToModify('./TestMaterials/BasicJPGImagesTest.PDF',settings);
var pageModifier = new hummus.PDFPageModifier(pdfWriter,0);
pageModifier.startContext().getContext().writeText('Test Text', ...
...
var copyingContext = inPDFWriter.createPDFCopyingContextForModifiedFile();
var thirdPageID = copyingContext.getSourceDocumentParser().getPageObjectID(2);
var thirdPageObject = copyingContext.getSourceDocumentParser().parsePage(2).getDictionary().toJSObject();
var objectsContext = inPDFWriter.getObjectsContext();
objectsContext.startModifiedIndirectObject(thirdPageID);
var modifiedPageObject = inPDFWriter.getObjectsContext().startDictionary();
A couple of helpers with HummusJS, to assist with what you are trying to do:
Adding content to existing pages - https://github.com/galkahana/HummusJS/wiki/Modification#adding-content-to-existing-pages
Draw shapes - https://github.com/galkahana/HummusJS/wiki/Show-primitives
using both, this is how to add a circle at 'centerx,centery' with 'radius' and border width of 1, to the first page of 'myFile.pdf'. The end result, in this case will be placed in 'modifiedCopy.pdf':
var pdfWriter = hummus.createWriterToModify(
'myfile.pdf',
{modifiedFilePath:'modifiedCopy.pdf'});
var pageModifier = new hummus.PDFPageModifier(pdfWriter,0);
var cxt = pageModifier.startContext().getContext();
cxt.drawCircle(
centerX,
centerY,
radius,
{
type:stroke,
width:1,
color:'black'
});
pageModifier.endContext().writePage();
pdfWriter.end();
General documentation - https://github.com/galkahana/HummusJS/wiki
If the tests fail, check that an "output" folder exists next to the script being executed, and that there are permissions to write there.

How to get file contents from absolute path in Node.js in this code?

I have the following code in NodeJs,
var fs = require('fs');
var data = fs.readFileSync('client/assets/images/car-img.jpg');
This client/assets/images/car-img.jpg is the location of image in system i.e relative path. I want to get file contents from absolute path like this. Consider, getting Google image from this absolute path,
var data = fs.readFileSync('https://www.google.co.in/images/srpr/logo11w.png');
How should I read data using readFileSync? Any help appreciated.

Resources