Forum Discussion

PaulF's avatar
PaulF
Occasional Contributor
4 years ago
Solved

dynamic function names

Hi Everyone,

 

TestComplete 14, project in JS

 

We're currently testing an app on Android and are trying to get ourcode dynamic, so that the Device can become some sort of global variable (execution environement related) that would have impacts on the way we'll execute the tests.

Unfortunately, we're not always able to do so, using testComplete.

 

Let me give you a clear example.

In some of our tests, we're using the Regions to compare screenshots taken in advance, with the actual screen while the test is executing.

Code :

function loginImageComparison(){Regions.loginF8331.Check(Aliases.Device.Android.login)};

This works very well with 1 Device. But when you change for a different one, you're likely to get the following

Output error : "The images are not equal. The size of the images is different."

 

 

All Right, we can take 1 image per Device, called something like "myImageDeviceName" and have the end of the "Devicename" part vary in our script.

 

So we scripted the following code :

function loginImageComparison(){
var loginScreenName, loginScreenImage;
loginScreenName = "login" + Mobile.Device().DeviceName;
loginScreenImage = Regions.loginScreenName;
loginScreenImage.Check(Aliases.Device.Android.login)

};

Output error : TypeError: Cannot read property 'Check' of undefined

loginScreenName, as a string, is not computed by testComplete. LoginScreenName is undefined.

 

So we tried the following, to make sure :

function loginImageComparison(){
var loginScreenName, loginScreenImage;
loginScreenName = "login" + Mobile.Device().DeviceName;
loginScreenImage = "Regions." + loginScreenName;
loginScreenImage.Check(Aliases.Device.Android.login);
};

Output error : TypeError: imageReference.Check is not a function

Without surprise, loginScreenImage, as a string, is not a function.

 

So is there a way to "variabilize" methods of testComplete ?

This could be the same for nameMapping for instance : Aliases.objectDevice with the "Device" used as a variable.

 

Regards,

Paul

  • Try the so-devil-and-must-be-hidden-because-too-powerful function eval() ...

     

    loginScreenImage = eval("Regions." + loginScreenName);

3 Replies

  • BenoitB's avatar
    BenoitB
    Community Hero

    Try the so-devil-and-must-be-hidden-because-too-powerful function eval() ...

     

    loginScreenImage = eval("Regions." + loginScreenName);

    • PaulF's avatar
      PaulF
      Occasional Contributor

      😄
      I guess, for "real" developpers, that was a non problem.

      Thank you for the tip.

      Cheers

      • BenoitB's avatar
        BenoitB
        Community Hero

        In fact eval() is so powerful that a bunch of real dev avoid it .. but well used it's a magic wand !

        And yes, this come from pure dev world 😉