0

I have a very simple form: you enter a first name and a last name, click submit, it displays the full name. I wrote a simple test for this form using Selenium Webdriverjs. It works with chrome, but not with phantomjs. Am I missing any trick to make it work with phantomjs? I am using phantomjs version 2.0.0.

Here's the test code:

var webdriver = require('selenium-webdriver');
var test = require('selenium-webdriver/testing');
var assert = require('assert');
var By = webdriver.By;

test.describe('Name Form', function() {
    var browser;

    test.beforeEach(function() {
        browser = new webdriver.Builder()
            .forBrowser('phantomjs')
            .build();
    });

    test.afterEach(function() {
        browser.quit();
    });

    test.it('should display the entered name when the Submit button is clicked', function() {

        // The simplified approach below of calling Webdriver methods works because of
        // the Promise Manager (see https://code.google.com/p/selenium/wiki/WebDriverJs#Control_Flows)
        browser.get('http://localhost:8080');

        // Enter firstName, lastName and click submit
        browser.findElement(By.id('firstName')).sendKeys('Naresh');
        browser.findElement(By.id('lastName')).sendKeys('Bhatia');
        browser.findElement(By.id('submitButton')).click();

        // Take a screen shot right after the click - it shows that display name has not appeared
        browser.takeScreenshot().then(function(image) {
            require('fs').writeFile('./screenshot-1.png', image, 'base64');
        });

        // Wait for displayName to be populated
        browser.wait(function() {
            return browser.findElement(By.id('displayName')).getText()
                .then(function(displayName) {
                    if (displayName.length > 0) {
                        assert.equal(displayName, 'Naresh Bhatia');
                        return true;
                    }
                    else {
                        return false;
                    }
                });
        }, 20000);
    });
});

Here's the error:

Name Form
  1) should display the entered name when the Submit button is clicked


0 passing (1s)
1 failing

1) Name Form should display the entered name when the Submit button is clicked:

    AssertionError: '' == 'Naresh Bhatia'
    + expected - actual

    +Naresh Bhatia

    at test/nameFormTest.js:49:24
    at Array.forEach (native)
From: Task: Name Form should display the entered name when the Submit button is clicked
    at Array.forEach (native)

EDIT

Note there is no page change on submit of the form. There is a JavaScript function that changes the diplayName element:

$(document).ready(function() {

    $('#submitButton').click(function(e) {
        e.preventDefault();
        $('#displayName').text($('#firstName').val() + ' ' + $('#lastName').val());
    });
});
Naresh
  • 23,937
  • 33
  • 132
  • 204
  • Have you verified that the page is fully loaded or that you are even on the correct page? – Artjom B. Aug 26 '15 at 17:07
  • I think so. I put console.log statements to see if I am getting `firstNameElement`, `lastNameElement` etc. and I am getting them back correctly. Moreover, if I simply change the `forBrowser()` line to chrome, everything works perfectly. – Naresh Aug 26 '15 at 18:36
  • I see that you click on a submit button, so the page changes (or is it ajax?). First name and last name would then be from the previous page and wouldn't have any relation to the next page which you have problems with. Try to take a screenshot to make sure. – Artjom B. Aug 26 '15 at 18:44
  • There is no page change. There is a JavaScript function on the page that updated the displayName on the click of the button. Please see my updated questions. – Naresh Aug 26 '15 at 18:49
  • Did you try to have a timeout of 5 seconds before clicking on the button? It might be that the execution of the click starts when the dom value of the input box is not updated yet (execution of the test is too fast) and that is why the test might fail. – Diana R Aug 27 '15 at 07:49
  • I added a 20 second delay after the button click - still no difference. See my updated code above (I updated in place so that it is not confusing). I also took a screenshot right after the click - the form fields show that they are correctly filled but displayField is blank. As far as I can tell the JavaScript that show compute the full name and display it is not doing its job! – Naresh Aug 27 '15 at 23:06
  • Hey, did you find a solution for your problem? – kism3t Mar 07 '17 at 08:02

0 Answers0