Results 1 to 4 of 4

Thread: Acessing page elements inside a .then() method

  1. #1

    Post Answered: Acessing page elements inside a .then() method

    So I am trying to fetch login credentials from a local database as an example to test the node-mysql module with sencha test.

    In the beforeAll()-Block I establish a connection, fetch the credentials using a promise and then want to enter them into the textfield's of the login page.

    But suddenly it seems that I can't access the page elements anymore as the first test cases start to fail before the login takes place

    Old code, works perfectly fine:
    Code:
        beforeAll(function () {
            
            stpo.loginPage.username()
                .focus()
                .type('username');
            
            stpo.loginPage.password()
                .focus()
                .type('password');
                   
            stpo.loginPage.loginButton()
                .focus()
                .click();
        });
    New code, does not work:

    Code:
        beforeAll(function () {
           
            const pool = mysql.createPool(//...db connection);
            
            let promise = new Promise(//...db query);
    
    
            promise.then(function(user){
                alert(user.name); // displays the correct value fetched from the datebase
                alert(user.password); // displays the correct value detched from the datebase
    
    
                stpo.loginPage.username()
                    .focus()
                    .type(user.name);
    
    
                stpo.loginPage.password()
                    .focus()
                    .type(user.password);
                    
                stpo.loginPage.loginButton()
                    .focus()
                    .click();
            }).catch(function(err){
                alert('Connection to the database has failed, please check the specified connection details...');
            });
        });
    Substituting the user.name and user.password with hard coded strings does not work either.
    Apparently the locators don't work inside the .then()-Block, why is that?

  2. I haven't used that particular node module before, but you may need to structure your "beforeAll" like this - by specifying a "done" parameter in the "beforeAll". This will cause the tests to wait until you explicitly tell it that the beforeAll is done, otherwise it will proceed to the next test without executing the contents of the promise.

    Something like this:

    Code:
    beforeAll(function (done) {
        const pool = mysql.createPool(// db connection);
        
        let promise = new Promise(// db query);
    
        promise.then(function(user){
            stpo.loginPage.username()
                .focus()
                .type(user.name);
    
            stpo.loginPage.password()
                .focus()
                .type(user.password);
                
            stpo.loginPage.loginButton()
                .focus()
                .click()
                .and(done);
        }).catch(function(err){
            // Error handling
        });
    });

  3. #2
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    Redwood City, CA
    Posts
    341
    Answers
    55

    Default

    What is the error when running the tests? Also, note that "alert" won't work in your code as a WebDriver scenario runs separately from the browser, so "alert" will be undefined, which would also cause an error.

    If you want to debug it in order to find out the returned values, add a "debugger;" line inside of the "then" function, and instead of clicking the Run button to run the tests, click the Debug button. You can then use the dev tools to debug the code and inspect the values.
    Daniel Gallo
    Senior Solutions Architect
    Sencha Inc.

  4. #3

    Default

    Okay, alert() does in fact display the small window as usual but that is not the problem.
    Using the debugger also shows me that I correctly fetched the credentials from the database.

    The thing is that no error explicitly is thrown apart from those that occur later on because ST can not reference components specified in the test cases (that have started by now) as it is still stuck at the login page.

  5. #4
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    Redwood City, CA
    Posts
    341
    Answers
    55

    Default

    I haven't used that particular node module before, but you may need to structure your "beforeAll" like this - by specifying a "done" parameter in the "beforeAll". This will cause the tests to wait until you explicitly tell it that the beforeAll is done, otherwise it will proceed to the next test without executing the contents of the promise.

    Something like this:

    Code:
    beforeAll(function (done) {
        const pool = mysql.createPool(// db connection);
        
        let promise = new Promise(// db query);
    
        promise.then(function(user){
            stpo.loginPage.username()
                .focus()
                .type(user.name);
    
            stpo.loginPage.password()
                .focus()
                .type(user.password);
                
            stpo.loginPage.loginButton()
                .focus()
                .click()
                .and(done);
        }).catch(function(err){
            // Error handling
        });
    });
    Daniel Gallo
    Senior Solutions Architect
    Sencha Inc.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •