How to use setTimer and teswaitXXXX function in test module with CAPL - capl

I'm trying to make automated test cases with CAPL in test module.
First problem I faced is that I can't use setTimer in CAPL.
As I know, setTimer is activated in [on Start] but It can't be used in test moduel.
When I try to call setTimer in testcase, It was not called at all.
my code like this, I want to make timer cyclic every 6 sec.
on timer Timer_1{
setTimer(Timer_1,6000);
}
testcase TestCase_1()
{
setTimer(Timer_1,6000);
}
void MainTest()
{
TestCase_1();
}
What is the problem in this code and how to use setTiemr in test module ?
Second question is that how to use testwaitXXXX function in CAP.
enter image description here
For example, I want to use those kind of fuction to wait untill specific variable is changed.
But I can't get information about 1st parameter which is "valueHanlde * coValue".
Is that a kind of handler ?
How can I use it ? please give me example code to use it.
I want to use it for checking flag value.
For example,
int flag = 0;
testWaitForeChangeFlag(flag,100000);
Thanks
BR
I've tried to run setTimer and testWaitForXXX fuctions in test module. But it was failed.

variables
{
msTimer Timer_1;
}
void mainTest()
{
Testcase_1();
}
testcase Testcase_1()
{
write("TC called");
setTimerCyclic(Timer_1,60);
testWaitForTimeout(6000);
}
on timer Timer_1
{
write("Your data");
}
The problem might be that your test case is ending before the timer is even being called. Introduce a delay with testwaitfortimeout and re-run the above code

Related

Calling launch without paranthesis in kotlin coroutines

I understand that launch is an extension function on CoroutineScope. But then I see it being used like this:
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch { // launch a new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
My understaning is that in kotlin one can define an infix function and then call it without any paranthesis. But from the documenation, I don't think launch is an infix function (in fact it has more than one parameter, so it can not be infix). It is also not a keyword in language. Then how is it called without any paranthesis?
The first two parameters are default parameters and the third one is High order function. When the last parameter is High order function then you can move Lamba out of parenthesis.
Suppose you have fun:
fun post(s:String="default", block:()->Unit){}
You call it in these ways:
post("1",{
})
You will get a suggestion Lamda should be moved out of parentheses
After moving out of parentheses:
post("1"){
}
Now you can remove the first parameter since it is default parameter
post {
}
https://kotlinlang.org/docs/reference/lambdas.html

Creating an async method which throws an exception after a specified amount of time unless a certain condition is met outside of that function

I am currently working on a Ruby script which is supposed to perform different tasks on a pretty long list of hosts. I am using the net-ssh gem for connectivity with those hosts. The thing is, there seem to exist some conditions under which net-ssh times out without throwing an exception. As of know, the script was only once able to finish a run. Most of the time, the scripts just hangs at some point without ever throwing an exception or doing anything.
I thought about running all tasks that may timeout in different threads, passing them a pointer to some variable they can change when the tasks finished successfully, and then check that variable for a given amount of time. If the task has not finished by then, throw an exception in the main thread that I can catch somewhere.
This is the first time I am writing something in Ruby. To give a clear demonstration of what I want to accomplish, this is what I'd do in C++:
void perform_long_running_task(bool* finished);
void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func);
int seconds_to_wait {5};
int seconds_task_takes{6};
int main() {
start_task_and_throw_on_timeout(seconds_to_wait, &perform_long_running_task);
// do other stuff
return 0;
}
void perform_long_running_task(bool* finished){
// Do something that may possible timeout..
std::this_thread::sleep_for(std::chrono::seconds(seconds_task_takes));
// Finished..
*finished = true;
}
void start_task_and_throw_on_timeout(int secs, std::function<void(bool*)> func){
bool finished {false};
std::thread task(func, &finished);
while (secs > 0){
std::this_thread::sleep_for(std::chrono::seconds(1));
secs--;
if (finished){
task.join();
return;
}
}
throw std::exception();
}
Here, when 'seconds_task_takes' is bigger than 'seconds_to_wait', an exception is thrown in the main thread. If the task finishes in time, everything goes on smoothly.
However, I need to write my piece of software in a dynamic scripting language that can run anywhere and needs not to be compiled. I would be super glad for any advice about how I could write something like the code above in Ruby.
Thanks alot in advance :)
edit: in the example ,I added a std::function parameter to start_task_and_throw_timeout so it's reusable for all similar functions
I think module timeout has everything you need to do. It allows you to run the block for a while and raise an exception if it was not fast enough.
Here is a code example:
require "timeout"
def run(name)
puts "Running the job #{name}"
sleep(10)
end
begin
Timeout::timeout(5) { run("hard") }
rescue Timeout::Error
puts "Failed!"
end
You can play with it here: https://repl.it/repls/CraftyUnluckyCore. The documentation for the module lives here: https://ruby-doc.org/stdlib-2.5.1/libdoc/timeout/rdoc/Timeout.html. Notice that you can customize not only the timeout, but also error class and message, so different jobs may have different kinds of errors.

VS Code Coverage won't recognize only possible Expression Lambda Path

I have following Extension Method which is just a negation of Linq.Any()
These two UnitTests do test it completely
[TestMethod]
public void EnumerableExtensions_None_WithMatch()
{
Assert.IsTrue(_animals.None(t => t.Name == "Pony"));
}
[TestMethod]
public void EnumerableExtensions_None()
{
var emtpyList = new List<Animal>(); { };
Assert.IsTrue(emtpyList.None());
}
As you can see in the picture, when I run a Code Coverage Analysis, the delegate body is not covered (white selection), because of the deferred execution.
This question comes close to the problem:
Code Coverage on Lambda Expressions
But does not quite solve it: Since the List must stay empty, it's impossible to actually step into that piece of code.
I am tempted to mark the segment with [ExcludeFromCodeCoverage] ...
How would you write the UnitTest?
You need to test that None() returns false when given a non-empty list of Animal. As it is, you never execute your default lambda expression.
You might even find a bug...
This is the correct way to write the Test. Even found a bug!
public void EnumerableExtensions_None()
{
// _animals HAS entries
Assert.IsFalse(_animals.None());
}
Code Coverage 100%!

how can we implement a keyword driven automation framework in testcomplete

I am trying to implement a keyword driven framework using test complete, and can anyone help me as to how to go about it , and what approach should i take to achieve this .
Here's what I did:
1-Create small "helper" scripts with functions you use all the time
2-One test case equals one keyword test
3-Call the small scripts in order to go through all your test case steps
This way, if you need to change something in the future, instead of going through all the tests to make that change, you can just change the helper script.
What I mean by helper scripts? Here's the example for a login page ('login.sj' file).
I just made this in 5 minutes, if there's any error I'm sorry...
var passTxtBx;
function login(username, password)
{
setUsername(username);
setPassword(password);
passTxtBx = Sys.Browser().Page().Find("objectIdentifier", "passwordTxtBx", 50, true);
passTxtBx.keys("[Enter]");
if(checkWarning)
Log.Error("Login Error")
}
function setUsername(username)
{
Sys.Browser().Page().Find("objectIdentifier", "usernameTxtBx", 50, true).setText(username);
}
function setPassword(password)
{
passTxtBx.setText(pasword);
}
function checkWarning()
{
if(Sys.Browser().Page().Find("objectIdentifier", "warning", 50, true).Exists)
return true;
else
return false;
}
Create a list of common actions in your application, ie. login, logout, set date.
Create a library script in your project.
Write small helper functions for the list of actions you defined in step 1 and place them in your library script created in step 2.
Write a test script that imports the library script and calls the helper functions in the order you want to drive your application.
I extended this by writing a function that read 'keywords' and their arguments in from a spreadsheet using the DDT Object in TestComplete and called the corresponding helper function.

failing a XCTestCase with assert without the test continuing to run but without stopping other tests

I'm trying to test my application using the XCTest framework.
I want my single test case to fail if some logical condition holds (using an assertion).
I don't want the rest of the code in the test case to run, because this might lead to problems (access to null pointers, for example)
I also want the rest of the test case to run normally, and just the failed test to be marked as failed.
I've noticed XCTestCase has a property called continueAfterFailure.
However, setting it to YES caused the failed test to continue executing lines after the assertion, and setting it to NO caused the rest of the tests not to run at all.
Is there a solution to this issue?
Pascal's answer gave me the idea to achieve this properly. XCTool now behaves like OCUnit when an assertion fails: the execution of the test case is aborted immediately, tearDown invoked and the next test case is run.
Simply override the method invokeTest in your base class (the one that inherits from the XCTestCase class):
- (void)invokeTest
{
self.continueAfterFailure = NO;
#try
{
[super invokeTest];
}
#finally
{
self.continueAfterFailure = YES;
}
}
That's it!
The easiest way is to add:
continueAfterFailure = false
into setUp() method. So it will look like this:
Swift
override func setUp() {
super.setUp()
continueAfterFailure = false
}
Objective-C
- (void)setUp {
[super setUp];
[self setContinueAfterFailure:NO];
}
One option would be to check the condition normally, then fail and return from the test if it is false.
Something like this:
if (!condition) {
XCFail(#"o noes");
return;
}
You could wrap this up in a helper macro to preserve readability.
BDD test libraries like Kiwi are more elegant for this sort of thing, as they make it easier to share setup between many tests which leads to fewer assertions per test.
I am able to use continueAfterFailure and let the other tests run by using this pattern:
self.continueAfterFailure = NO;
#try
{
// Perform test code here
}
#finally
{
self.continueAfterFailure = YES;
}
In Swift projects, I use a helper function (defined in a shared superclass of all my tests which itself extends XCTestCase):
/// Like `XCTFail(...)` but aborts the test.
func XCTAbortTest(_ message: String,
file: StaticString = #file, line: UInt = #line
) -> Never {
self.continueAfterFailure = false
XCTFail(message, file: file, line: line)
fatalError("never reached")
}
As the comment suggests, the call to fatalError is never actually executed; XCTFail aborts the test in an orderly fashion (tearDown is called, next test runs, etc.). The call is only there to trick the compiler into accepting Never as return type since XCTFail returns Void (it does return if continueAfterFailure == true).
Note that self.continueAfterFailure is reset to the default true for every test method. You can also make that explicit in setUp().

Resources