Selenium Webdriverjs测试工作在chrome但不在phantomjs

时间:2015-08-26 16:04:19

标签: javascript selenium-webdriver phantomjs webdriverjs

我有一个非常简单的表单:输入名字和姓氏,点击提交,它会显示全名。我使用Selenium Webdriverjs为这个表单编写了一个简单的测试。它适用于chrome,但不适用于phantomjs。我错过任何使其与phantomjs一起工作的技巧吗?我正在使用phantomjs版本2.0.0。

这是测试代码:

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);
    });
});

这是错误:

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)

修改

请注意,提交表单时不会更改页面。有一个JavaScript函数可以更改diplayName元素:

$(document).ready(function() {

    $('#submitButton').click(function(e) {
        e.preventDefault();
        $('#displayName').text($('#firstName').val() + ' ' + $('#lastName').val());
    });
});

0 个答案:

没有答案