Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, windows and java based applications. Get your 30 day free trial.

Discuss your Sahi Pro usage patterns, best practices, problems and solutions. Help others solve their problems and seek help from the community when needed. If you need specific support on your application, please email support @ sahipro.com

How do I code the the second factor in 2 factor authentication?

davidjohnbirddavidjohnbird Members
edited November 2013 in Sahi - Open Source
Hi,

I'm trying to complete an exercise in 2 factor authentication and have run in to problems with the second factor part. Humans have no problem reading what is on screen and supplying the right values, but translating that in to an automation script has defeated me so far.

Getting the login page to work is easy, so I'll not include the code for that.

The second page consist of 3 labels, 3 textboxes and a Submit button.

The labels tell you what values from a user-defined string to type in to the textboxes by displaying the position of the relevant character in the label - for instance "Position 1", "Position 5" and "Position 7". The numeric part of the label can change each time you log in.

The problem I am having is scripting Sahi to extract the numeric part of each label so that I can use substring to get the values to put in to the textboxes. This is because Sahi uses the label contents to name the label objects - which will change each time the process is run. I eventually used the Object Spy functionality to get generic values for the label objects which stay the same but it is still not
working.

Here is the code I'm currently working with, so I hope someone can tell me what I'm doing wrong. Please note - I'm a tester and coding is something I'm learning as I go along. Thanks in advance.
var $securitynum = "12345678";
var $res = _cell("text").substring(9) - 1;
_setValue(_password("rand1"), $securitynum.substring($res,1));
var $res = _cell("text[1]").substring(9) - 1;
_setValue(_password("rand2"), $securitynum.substring($res,1));
var $res = _cell("text[2]").substring(9) - 1;
_setValue(_password("rand3"), $securitynum.substring($res,1));
_click(_submit("Submit"));

Best Answer

  • Hi davidjohnbird ,

    i guess split can also be used..
    var $label ="Position 1";
    var $s= $label.split(" ");
    _log($s[1]); will result int he number ("1")

    Or else, we can use sahi api's of referencing as in _near.

    Regards,
    Bhavitha

Answers

  • To answer my own question.
    Using a whole afternoon, a bit of brute force coding and testing a lot of perusing the Sahi online documentation and finally substituting substring with substr, I have managed to crack it. So I have a solution but I'm open to seeing if anyone comes up with a more elegant solution than mine.
  • Hi Bhavitha,

    Very elegant alternative. I will try it out once I can get back to coding rather than testing.

    Regards,

    David.
Sign In or Register to comment.