TestCafe Studio: How do I import ES modules so I can use them in a TestCafe Script? - testing

I want to use imported functions in TestCafe Scripts (which are basically copied into the test method). To me these scripts would be great to create reusable code snippets.
But I did not find a place where I could import a module which is then added to the import statements in the header of the test file.
An example:
I have a test-function I use for visual regression tests. It basically takes a screenshot of the page and compares it to a screenshot stored on disk. Since this also includes reading and writing files from/to disk using nodes fs API and calling packages like graphicsmagik, it quickly becomes a huge script. I might be able to copy it into a a TestCafe Script block, but it is not reusable and hardly scales.
Is there something I missed?
Of course I could create some command-line task that adds the import line to the top of all generated js files. But the test would only work after someone made them js files and ran my script. It would not be possible to run these tests directly from TestCafe Studio.

You can put your code to a separate module and import it using the Run TestCafe Script:

Related

is it possible to define events or capl inbuilt test module functions in includes.cin file and import it in test modules?

I am currently working on capl browser (Vector Cannoe) test modules. I have a requirement where I am performing same set of actions such as testWaitForSignalChange(), testWaitForSignalMatch() as a prerequisite on every Testcase, inside different test modules. is it possible to define these functions in a includes file and import it when required?
Also, is it possible to import testcases from test module to another in Capl?`

Loading fsx files dynamically in an FSX script

We are sharing a build script for FAKE across a set of projects. We want to keep this one build script the same but make it possible to extend with other targets. One way I could think of doing this is by loading .fsx files if they fit a specific naming pattern like al files that matches build-*.fsx however I can't seem to think of a way to load these files dynamically. Any suggestions on how to do this or how to accomplish the desired result are all good as answers
if I could I would have done something like
#load "build-*.fsx"
It's not completely clear to me why you want to do this but maybe this will help. Refer to a single script in each project:
#load "load-build-scripts.fsx"
And then in single load-build-scripts.fsx:
#load "build-1.fsx"
#load "build-2.fsx"
#load "build-3.fsx"
...
This second file you will need to change whenever you add a new script.
It's not generally recommended to do this. Because now if these separate scripts refer to each other then some scripts will be loaded more than once. Scripts aren't really meant to be used for cases this complex.
Another option is to use FAKE as a console project instead of using scripts and the fake-cli tool. Then you can use normal .NET project dependencies.

How to test the main package in Golang from a "test" package?

I have a simple program written in Golang. It's an API. So inside the project folder, there's a folder named cmd containing my main package (used to initialise the app and defines the endpoints for the API). There's also a folder named after my program, containing multiple files from a package also named after my program. This package serves as the model to do all the necessary queries and contains all the types I have defined.
I also created a folder called test. It contains all my test files under the package named test. The problem is that to run the tests, I have to access my main package ! Is there a way to do that in Golang ? I tried simply using import "cmd/main" but of course it doesn't work.
I also had an idea. Perhaps I could move all my initialising functions (in the cmd folder) to the package named after my program. This way I could do a regular import in test. And I create, inside of cmd, a main.go in the main package that serves as the entry point for the compiler.
I'm new to Go so I'm not really confident. Do you think it's the right way ?
Thanks.
EDIT : Apparently some people think this question is a duplicate, but it's not. Here's the explanation I gave in on of the comments :
I read this post before posting, but it didn't answer my question
because in that post the person has his tests in the main package. The
reason why I asked my question is because I don't want to have my
tests in the main package. I'd rather have them all in a test folder
inside the same package.
What you want to do is not not possible in GO (assuming you want to test private functions).
because I don't want to have my tests in the main package. I'd rather
have them all in a test folder inside the same package.
Your code belongs to different package if you move it into different folder.
This is how GO defines packages https://golang.org/doc/code.html#Organization:
Each package consists of one or more Go source files in a single
directory.
This is how your code structured:
main
| -- main.go (package main)
+ -- test
| -- main_test.go (package test)
It is idiomatic to keep tests in the same folder with code. It is normal if language or framework set some rules that developer has to follow. GO is pretty strict about that.
This is how you can organize your code:
main
| -- main.go (package main)
| -- main_test.go (package main_test)
| -- main_private_test.go (package main)
Often it makes sense to test code against its pubic interfaces. The best way to do that that, is to put tests into different package. GO convention is to keep tests in the same folder what leads to using the same package name. There is a workaround for that issue. You can add _test (package main_test) prefix to package name for your tests.
If, that is not possible to test your code using public interfaces, you can add another file with tests and use package main in that file.

How to exclude generated code from coverage statistics

I have thrift generated code in my project? How do I stop this from affecting my coverage stats? They're dismal.
This help message from go test seems to suggest you can filter the packages you're testing:
-coverpkg pkg1,pkg2,pkg3
Apply coverage analysis in each test to the given list of packages.
The default is for each test to analyze only the package being tested.
Packages are specified as import paths.
Sets -cover.
Another simpler option, and this is what I do, is to import the generated code as a library package that sits outside your code tree, and thus the cover tool ignores it in its stats.
e.g. if your app is github.com/Fuser97381/myproj, put the generated code in github.com/Fuser97381/protocols. Then your code looks like this:
package main
import (
"github.com/Fuser97381/protocols/myproto"
"git.apache.org/thrift.git/lib/go/thrift"
)
...

how to setup tests for mocha-phantomjs

Every tutorial I have seen for mocha-phantomjs shows having a test harness html file, and a separate javascript file that gets included.
Is this the correct way to do this for each test? I want to create a separate test for each page in my website, but it seems like overkill/crazy to duplicate an html file for every test case.
Granged, this is my first time trying to use mocha-phantomjs, but still, it seems really odd to create an html file and a js for every test case.
What is the standard for doing this sort of thing? I have been googling for about an hour now and can't find any good examples.
I know it seems weird, but... yes.
You need fixture (or harness) files in the "/test" directory. By default, Mocha looks in this directory for filenames with a .html extension, starting with test.html.
Make sure to include the script (and css) tags for 1) mocha, 2) chai (or whatever other assertion library you want), 3) and your specific test suites.
Personally, I've found it helps to use it with a modular bootloader like RequireJS. That way all your fixture files can point to a single configuration file: less maintenance.