I'm using Node.js to make a build system in Sublime Text 2 to build JavaScript code. I know how to setup basic build systems in Sublime, but this one is really giving me problems.
JavaScript example code:
var end = 10;
for (var i = 0; i < end; i++) {
console.log("hello world!");
};
1st try:
{
"cmd": ["node", "$file"]
}
returned this in the console: [Finished in 0.1s]
2nd try:
{
"cmd": ["node", "$file", "$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector": "#.js"
}
returns the same thing.
Any help would be much appreciated (I've spend over 2 hours searching google).
EDIT: Fixed For to for : Same result.
Here's a Javascript build system I use on my Mac with Sublime Text. It uses the Webkit engine (SquirrelFish) which comes with OS X, and therefore doesn't require anything special be installed (not even Node.js):
{
"cmd": ["/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc", "$file"],
"selector": "source.js"
}
Tips:
Use "print(foo)" from within your script to return results to the Sublime console
Save your .js file to disk at least once
If ".js" files are not automatically selected, ensure you have no other build systems for ".js" in "~/Library/Application Support/Sublime Text 2/Packages/". Alternately you can manually select the appropriate system from "Tools > Build System"
Hope this helps.
In Sublime Text 2 you must save your .js file before you build, or the result above will happen.
Simple fix is to save the file (obviously)!
Related
I am trying to make Jsprettier work in Sublime, but the formats I set up can't be applied, because I keep getting an error when I want to save a document (jsprettier: format failed). When I open the console to see what the error is it says: The system cannot find the path specified. (CR).
The settings in the user settings tab of jsprettier are the following:
{
"prettier_cli_path": "/c/Users/Adri/AppData/Roaming/npm/prettier",
"node_path": "/c/Program Files/nodejs/node",
"auto_format_on_save": true,
"prettier_options": {
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"parser": "flow",
"semi": true,
"tabs": false
}
}
Thank you
In your terminal run the following:
which node
which prettier
Copy the result of the output for each step and in SublimeText3 proceed to:
Preferences --> PackageSettings --> JsPrettier --> Settings-User
In the user settings build your file as such:
{
"prettier_cli_path": "output from terminal here",
"node_path": "output from terminal here",
"auto_format_on_save": true,
}
I found that when JsPrettier updates it erased these settings on me and needed to re-enter them...
Hope that helps.
tested on: Sublime Text 3.2.2, MacBook Pro
Install JsPrettier globally using npm:
npm install --global prettier
Install JsPrettier from Package Control by:
Sublime Text-->Preferences-->Package Control-->type and enter: Install Package-->type and enter: JsPrettier
restart Sublime Text
In case this helps anyone:
While this installed easily on one computer, I struggled with it on a second one.
After being required to set the node and prettier_cli path, I got node errors.
What worked in the end was setting the prettier_cli_path to prettier.js within the prettier node_modules directory as such:
C:/Users/Me/AppData/Roaming/npm/node_modules/prettier/bin-prettier.js
Problem solved!
I just took the first 2 lines out (paths) and now it works!
In my case, I had to fully restart Sublime 3 after it has installed a new Sublime version.
If someone is having trouble with this even now, it may help to know that Prettier requires node >= v10.
To get this to work, you may have to set your node version as >= 10, I personally prefer 12 or 13.
You can do this either by using nvm alias default 12 fornvm or n 12 for n
Now, update the user settings file to make sure prettier has the right paths to prettier and node. Find the paths to node and prettier using which
which prettier
which node
Add the paths and flags to auto format on save as desired
{
"prettier_cli_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/prettier",
"node_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/node",
"auto_format_on_save": true,
"format_on_save_extensions": ["tsx", "json"]
}
Now reload sublime
You're running an outdated version of Prettier. Please update again to the latest version, which is v1.6.1 (as of Sept. 13, 2017).
If you want to use v1.5.x on purpose, you can add the --no-config option to the additional_cli_args setting,
and tell Prettier not to attempt to find config files.
json
{
"additional_cli_args": {
"--no-config": ""
}
}
I want to know how I can verify if a file was downloaded using Selenium Webdriver after I click the download button.
Your question doesn't say whether you want to confirm it locally or remotely(like browserstack) . If it is remotely then my answer will be "NO" as you can see that the file is getting downloaded but you can not access the folder. So you wont be able to assert that the file has been downloaded.
If you want to achieve this locally(in Chrome) then the answer is "YES", you can do it something like this:
In wdio.conf.js(To know where it is getting downloaded)
var path = require('path');
const pathToDownload = path.resolve('chromeDownloads');
// chromeDownloads above is the name of the folder in the root directory
exports.config = {
capabilities: [{
maxInstances: 1,
browserName: 'chrome',
os: 'Windows',
chromeOptions: {
args: [
'user-data-dir=./chrome/user-data',
],
prefs: {
"download.default_directory": pathToDownload,
}
}
}],
And your spec file(To check if the file is downloaded or not ?)
const fsExtra = require('fs-extra');
const pathToChromeDownloads = './chromeDownloads';
describe('User can download and verify a file', () =>{
before(() => {
// Clean up the chromeDownloads folder and create a fresh one
fsExtra.removeSync(pathToChromeDownloads);
fsExtra.mkdirsSync(pathToChromeDownloads);
});
it('Download the file', () =>{
// Code to download
});
it('Verify the file is downloaded', () =>{
// Code to verify
// Get the name of file and assert it with the expected name
});
});
more about fs-extra : https://www.npmjs.com/package/fs-extra
Hope this helps.
TL;DR: Unless your web-app has some kind of visual/GUI trigger once the download finishes (some text, an image/icon-font, push-notification, etc.), then the answer is a resounding NO.
Webdriver can't go outside the scope of your browser, but your underlying framework can. Especially if you're using NodeJS. :)
Off the top of my head I can think of a few ways I've been able to do this in the past. Choose as applicable:
1. Verify if the file has been downloaded using Node's File System (aka fs)
Since you're running WebdriverIO, under a NodeJS environment, then you can make use its powerful lib tool-suite. I would use fs.exists, or fs.existsSync to verify if the file is in the expected folder.
If you want to be diligent, then also use fs.statSync in conjunction with fs.exists & poll the file until it has the expected size (e.g.: > 2560 bytes)
There are multiple examples online that can help you put together such a script. Use the fs documentation, but other resources as well. Lastly, you can add said script inside your it/describe statement (I remember your were using Mocha).
2. Use child_process's exec command to launch third-party scripts
Though this method requires more work to setup, I find it more relevant on the long run.
!!! Caution: Apart from launching the script, you need to write a script in a third-party framework.
Using an AutoIT script;
Using a Sikuli script;
Using a TestComplete (not linking it, I don't like it that much), or [insert GUI verification script here] script;
Note: All the above frameworks can generate an .exe file that you can trigger from your WebdriverIO test-cases in order to check if your file has been downloaded, or not.
Steps to take:
create one of the stand-alone scripts like mentioned above;
place the script's .exe file inside your project in a known folder;
use child_process.exec to launch the script and assert its result after it finishes its execution;
Example:
exec = require('child_process').exec;
// Make sure you also remove the .exe from scriptName
var yourScript = pathToScript + scriptName;
var child = exec(yourScript);
child.on('close', function (code, signal) {
if (code!==0) {
callback.fail(online.online[module][code]);
} else {
callback();
}
});
Finally: I'm sure there are other ways to do it. But, your main take-away from such a vague question should be: YES, you can verify if the file has been downloaded if you absolutely must, expecially if this test-case is CRITICAL to your regression-run.
With Sublime Text, is it possible to use different syntax for a same file extension depending on the currently opened project ?
Example :
Project A : file.js contains classic Javascript
Project B : file.js contains JSX
How can I obtain JavaScript syntax for project A and Babel syntax for Project B?
Just for background (which you likely already know), Sublime Text applies a syntax via the extension of the file, and overriding that requires you to use View > Syntax > Open all with current extension as... from the menu. This creates a syntax specific override which appears in a specific file name and is thus not directly overrideable on a per project basis.
That said, it is possible to swap the syntax on your own (e.g. via the command palette) which opens the possibility of a plugin being able to do this for you. There may be an existing plugin that does this in PackageControl, but for reference purposes, here is an example based on something I'm using for a similar purpose.
The following assumes that you're using the Babel plugin to get your syntax highlighting, since you mention Babel. This would need to be modified if this is not the package you're using. This could also be modified to similarly do a swap for a different type of file if desired.
To use this, select Tools > Developer > New Plugin from the menu and replace the entire contents of the sample file with the code below, and then save it as a python file in the directory that Sublime suggests (which should be in Packages\User). I named mine js_syntax_swap.py, but the name doesn't matter as long as the extension is .py:
import sublime, sublime_plugin
# Standard Sublime JavaScript syntax file is:
# 'Packages/JavaScript/JavaScript.sublime-syntax'
#
# The Babel package syntax is:
# 'Packages/Babel/JavaScript (Babel).sublime-syntax'
class JavaScriptSyntaxSwap (sublime_plugin.EventListener):
def modify_syntax (self, view):
if view.window () == None:
return
swapSyntax = view.settings ().get ('using_babel_js', False)
curSyntax = view.settings ().get ('syntax')
variables = view.window ().extract_variables ()
extension = variables['file_extension']
if swapSyntax is True and extension == 'js' and "Babel" not in curSyntax:
view.set_syntax_file ('Packages/Babel/JavaScript (Babel).sublime-syntax')
def on_load (self, view):
self.modify_syntax (view)
def on_post_save (self, view):
self.modify_syntax (view)
With this in place, if you choose Project > Edit Project from the menu, you can include a using_babel_js setting to enable the plugin for JavaScript files in that project. An example might be:
{
"folders":
[
{
"path": "."
}
],
"settings":
{
"using_babel_js": true
}
}
What this is doing is checking every time you load or save a file to see if it should swap the syntax from the default to the Babel JSX syntax, which it does only for files with the extension .js that are not already using Babel as the syntax.
I'm trying to execute node-dev in a sublime text 3 build system. node-dev is in my path:
Yet when I run this build script:
{
"cmd": ["node-dev", "$file"],
"selector": "*.js"
}
I get this error, which also shows that npm is in my path.
yet when I run with the same build script using node instead of node-dev it executes just fine.
I've also tried to include the "path" variable pointing at the node-dev bin folder, which didn't help at all.
Any help would be appreciated. Thanks.
Following worked for me in Sublime Text 3 on Windows
Tools -> Build System -> New Build System...
Enter the below text in the new file
Save the file as "nodejs.sublime-build"
{
"shell_cmd": "node ${file}",
"selector" : "source.js"
}
Prerequisite is to have node.js installed
Sublime text docs:
https://www.sublimetext.com/docs/build_systems.html
shell
Optional. If true, cmd will be run through the shell (cmd.exe, bash…)
Try to add "shell : true
{
"cmd": ["node-dev", "$file"],
"selector": "source.js",
"windows" : {
"shell": true
}
}
The command is incorrect for Sublime Text 3 :)
This is one example of running node as build system:
{
"shell_cmd": "taskkill /F /IM node.exe & node ${file}"
}
Please note that the array-version doesn't work like in Sublime Text 2.
For macOS, this worked for me on Sublime Text 3:
{
"cmd": ["node","$file","$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector":"source.js"
}
Selector Note
My selector setting was:
"selector":"*.js"
and OdatNurd advised that:
The reason is that the selector is not correct; it doesn't match file
names, it matches syntax scopes (i.e. it's based on the syntax in use
regardless of file extension); changing it to source.js from *.js
should get it working.
If you are a windows user.
Try applying the following snippet
{
"selector": "source.js",
"cmd": ["C:\\Program Files\\nodejs\\node", "<", "$file"],
"windows": {
"shell": true
}
}
Save this as node.sublime-build file.
For more info you can refer to http://docs.sublimetext.info/en/latest/ for more.
As a follow-up of this question, I am trying MozMill for testing standalone XUL applications (not a firefox extension). However, I did not "get it" yet - more specifically, how to test a XULRunner-based application.
Consider this app, for example. For now, I can run it from command line, more or less this way:
$ /path/to/xulrunner /full/path/to/mozmillexample/application.ini
I would like to write Mozmill scripts to test it. For example, I would like to write a test such as this one, which has as "taste" of unit testing:
Components.utils.import("chrome://mozmillexample/content/example.js", example);
var setupModule = function(module) {
module.controller = mozmill.getBrowserController(); // Or what?
}
var testEquals = function() {
jumlib.assertEqual(example.exHello(), "Hello, World!", "Should be equal");
}
I would like to write some functional tests, too:
Components.utils.import("chrome://mozmillexample/content/example.js", example);
var setupModule = function(module) {
module.controller = mozmill.getBrowserController(); // Or what?
}
var testAlerts = function() {
var button = findElement.Elem(document.getElementById('button'));
button.click();
controller.window.assertAlert(); // I bet it does not exist but it gives the idea...
}
Unfortunately, however, I did not find any documentation about testing standalone apps, at least none explaining the basic steps. So I ask: is it possible to write tests like these ones? How could I do it, if it is possible?
I got it to work with the MozMill extension; unpack the extension and edit the following files:
add this to install.rdf at the right place:
<em:targetApplication>
<Description>
<em:id>mozmill#example.com</em:id>
<em:minVersion>0.9</em:minVersion>
<em:maxVersion>1.1</em:maxVersion>
</Description>
</em:targetApplication>`
create a folder "extensions" in the application's root (where application.ini and the "chrome" and the "defaults" folder are); paste the unpacked mozmill extension there.
Enable the extension manager as described in MDC
embed the MozMill Extension code in your XULRunner app: <script src="chrome://mozmill/content/overlay.js"/>
Enable the extension by adding or modifying %appdata%\Adam Brandizzi\MozMill Example\Profiles\123455.default\extensions.ini:
[ExtensionDirs]
Extension0=C:\Users\John\Desktop\myXULApp\extensions\mozmill
Use the following JS code to open MozMill: MozMill.onMenuItemCommand(event);