automation test woorks well except when it couters eror, the excution of script will stop.
Is there a way to tell automation replay go to next function test without stop?
Your comment welcome
You can use the try catch statement provided by JavaScript.
For example.
function myTest () {
// do testing here
}
try {
myTest();
} catch (err) {
UIALogger.logMessage("ERROR: " + err);
// go to next testing...
}
Related
I want to be able to avoid a specflow scenario execution, or quit at the first step, depending on external configuration. Is that possible?
Like
Background:
Given we have a satisfied precondition
the precondition fails and the scenario stops without returning an error
IUnitTestRuntimeProvider interface should be injected
https://docs.specflow.org/projects/specflow/en/latest/Execution/SkippingScenarios.html
however, I then run into the fact that this interface can not be resolved
https://github.com/SpecFlowOSS/SpecFlow/issues/2076
the resolution of this does not work for me in a .Net 5, XUnit project, with Visual Studio 2019
If a step does not throw an exception, then the step passes. You could refactor that particular step into a private method, then simply wrap the method call in a try-catch inside an if statement:
[Given(#"we have a satisfied precondition")]
public void GivenWeHaveASatisfiedPrecondition()
{
if (/* check your external config here */)
{
try
{
PerformPrecondition();
}
catch (Exception ex)
{
Console.WriteLine("Step failed, but continuing scenario." + Environment.NewLine + Environment.NewLine + ex);
}
}
else
{
// Failure in this step should fail the scenario.
PerformPrecondition();
}
}
private void PerformPrecondition()
{
// Your step logic goes here
}
You haven't specified where your external configuration is at, but there are a thousand ways to do this.
I have another question about Pipelines this time about error handling using try/catch. What I am doing is using the "try" outside the stage so I can easily tell which stages failed and which passed. And this has worked brilliantly.
I need to now worry about logging and success/failure for all our logs. So here is my dilemma: I want to it build (try) then error check (catch) then output SUCCESS if it has built and checked for errors. If the error check (catch) catches errors, then output to log FAILURE. I am sure the solution is staring at me in the face and I am missing it. This is what I have (simple version) that is not very elegant:
env.STAGE_STATUS = ""
try {
stage ('Build Setup') {
echo "Did this work?"
}
} catch (errors) {
echo "This did not work!"
env.STAGE_STATUS = "FAIL"
}
if ("%STAGE_STATUS%" != "FAIL") {
echo "This did work!"
}
As always thanks for any assistance.
Using try/catch/finally inside stage looks more elegant and at least more readable. Secondary if you want to fail build (which is basically same cause in your sample you are failing stage, which is also failing the job itself) use following var currentBuild.result, which is btw globally accessible. Saying so, I would go for this code:
stage('Build Setup') {
try {
echo 'Did this work?'
} catch {
echo 'This did not work!'
currentBuild.result = 'FAILED'
} finally {
if (currentBuild.result != 'FAILED') {
echo 'This did work!'
}
}
}
I want to test a function that calls exit.
Basicly, I have a console application, that asks the user if he is sure that he wants a directory to be overwritten. When the users answers "No", the directory won't be overwritten, and the program should exit.
promptToDeleteRepo() {
bool okToDelete = ...
if(okToDelete) {
deleteRepo();
} else {
exit(0);
}
}
So I want to test that if the user answers "No", that the program really exits. But if I test this, my test runner exits.
In python I seem to be able to do something like:
with pytest.raises(SystemExit):
promptToDeleteRepo();
Is there something like this in Dart?
You can inject a custom exit function during the tests.
import 'dart:io' as io;
typedef ExitFn(int code);
ExitFn exit = io.exit;
promptToDeleteRepo() {
bool okToDelete = ...
if(okToDelete) {
deleteRepo();
} else {
exit(0);
}
}
and in your test :
int exitCodeUsed;
mylib.exit = (int code) {exitCodeUsed = code};
mylib.promptToDeleteRepo();
A better solution whould have to use zones but there doesn't seem to be possible to handle exit. It could be worth to file an issue.
One option that comes to my mind is to run the code you want to test in a new process Process.run() or Process.start() and check the exit code at the end. You can use stdin/stdout to communicate with the process (send keyboard input, read output)
I'm writing a yeoman generator and want to check some prerequisites, for example a git being installed. I can easily check this using .exec, but how do i gracefully abort generator and report error to user? I searched docs, but it seems that i'm missing some obvious way to do it. Any hints?
Throwing exception will of course abort generator, but is it a best way? Maybe something more user friendly? Not all yeoman users are able to read js exceptions.
The current state of error handling in the popular generators is quite diverse:
in the most cases they just log the error and return from the action and let the subsequnt actions run and return 0 status code:
generator-karma's setupTravis method:
if (err) {
this.log.error('Could not open package.json for reading.', err);
done();
return;
}
or set a custom abort property on error and skip further actions with cheking on the abort property but still return 0 status code:
generator-jhipster's CloudFoundryGenerator:
CloudFoundryGenerator.prototype.checkInstallation = function checkInstallation() {
if(this.abort) return;
var done = this.async();
exec('cf --version', function (err) {
if (err) {
this.log.error('cloudfoundry\'s cf command line interface is not available. ' +
'You can install it via https://github.com/cloudfoundry/cli/releases');
this.abort = true;
}
done();
}.bind(this));
};
or manually end the process with process.exit:
generator-mobile's configuringmethod:
if (err) {
self.log.error(err);
process.exit(1);
}
However none of these methods provide a good way to signal to the environment that something went wrong except the last one but directly calling process.exit is a design smell.
Throwing an exception is also an option but this presents also the stackstrace to the user which is not always a good idea.
The best option would be use the Environment.error method, which has some nice advantages:
the Environment is exposed thorough the env property of the yeoman.generators.Base
an error event is emitted which is handled by the yo cli code
the execution will result in a non zero (error) status code which is override-able
by default yo will display only the message and no stacktrace
the stacktrace can be optionally displayed with providing the --debug built-in option when re-running the generator.
With using this technique your action method would look like this:
module.exports = generators.Base.extend({
method1: function () {
console.log('method 1 just ran');
this.env.error("something bad is happened");
console.log('this won't be executed');
},
method2: function () {
console.log('this won't be executed');
}
});
Say, I have the following code that I run as a .JS file using the Windows Script Host:
try
{
ProduceAnError();
}
catch(e)
{
//How to get an error line here?
}
Is there any way to know the error line where an error (exception) occurred?
Sorry for my other reply. It wasn't very helpful :P
I believe what you are looking for is the ReferenceError's stack property. You can access it with the argument that you pass to the catch:
try {
someUndefinedFunction("test");
} catch(e) {
console.log(e.stack)
}
example output:
ReferenceError: someUndefinedFunction is not defined
at message (http://example.com/example.html:4:3)
at <error: TypeError: Accessing selectionEnd on an input element that cannot have a selection.>
at HTMLInputElement.onclick (http://example.com/example.html:25:4)
There isn't really a way to get a stack or even catch the line number programmatically. One can only see the line number when throwing an error. At best you can get the error code and description or you can make your own errors.
Here's the documentation.
And an example
try {
produceAnError();
} catch(e) {
WScript.echo((e.number>>16 & 0x1FFF)); // Prints Facility Code
WScript.echo((e.number & 0xFFFF)); // Prints Error Code
WScript.echo(e.description); // Prints Description
throw e;
}