如何设置测试环境以便使用selenium和phantomjs进行测试?

Sas*_*wat -6 javascript testing selenium phantomjs selenium-webdriver

我正在尝试使用Selenium和PhantomJS设置无头测试环境.

设置phantomjs:

我已经创建了一个文件夹c:/phantomjs并将所有phantomjs脚本文件放在那里(下载后).

然后我创建了一个文件夹 C:\xampp\htdocs\testPhantomJS

现在我在我的系统中安装了nodeJS.

然后我遍历到C:\xampp\htdocs\testPhantomJS命令提示符并安装了phantomJS,如下所示:

C:\xampp\htdocs\testPhantomJS>npm install -g phantomjs

图像表示不同的位置.那是因为它来自我同事的电脑.我们都在进行同样的安装,他把图像发给我参考.这就是为什么它与我的文件夹位置不同,但我说的位置,它是我工作的那个.

在此输入图像描述

现在在命令提示符下键入phantomjs,当我们输入时

C:\xampp\htdocs\testPhantomJS>phantomjs

phantom>

设置Selenium-Webdriver

C:\xampp\htdocs\testPhantomJS在命令提示符中遍历并安装了selenium webdriver,如下所示:

C:\xampp\htdocs\testPhantomJS>npm install selenium-webdriver

在此输入图像描述

安装后,文件夹结构如下: 在此输入图像描述

现在我有一个测试脚本test.js,如下所示:

describe('Test example.com', function(){
    before(function(done) {
        client.init().url('http://google.com', done);
    });

    describe('Check homepage', function(){
        it('should see the correct title', function(done) {
            client.getTitle(function(err, title){
                expect(title).to.have.string('Example Domain');
                done();
            });
        });

        it('should see the body', function(done) {
            client.getText('p', function(err, p){
                expect(p).to.have.string(
                    'for illustrative examples in documents.'
                );
                done();
            })
        });
    });

    after(function(done) {
        client.end();
        done();
    });
});
Run Code Online (Sandbox Code Playgroud)

问题是,我应该把上面的脚本放在哪里,以及如何运行它?我只是不需要使用phantomjs运行,我需要使用phantomjs和selenium进行测试.

Max*_*ard 6

这个解决方案来自这个非常简洁的教程,关于如何使用selenium和phantomjs设置测试:http://code.tutsplus.com/tutorials/headless-functional-testing-with-selenium-and-phantomjs--net-30545

以下是一些教程:

结合一切

现在我们已经拥有了所有的东西,我们必须将所有东西放在一起.

记住:在运行任何测试之前,您必须运行Selenium Server:

1 java -jar selenium-server-standalone-2.28.0.jar Selenium将在内部运行PhantomJS; 你不必担心.

现在,我们需要从JavaScript连接到Selenium.这是一个示例代码段,它将启动与Selenium的连接,并有一个就绪对象来控制我们的Selenium实例:

// Use webdriverjs to create a Selenium Client
var client = require('webdriverjs').remote({
    desiredCapabilities: {
        // You may choose other browsers
        // http://code.google.com/p/selenium/wiki/DesiredCapabilities
        browserName: 'phantomjs'
    },
    // webdriverjs has a lot of output which is generally useless
    // However, if anything goes wrong, remove this to see more details
    logLevel: 'silent'
});

client.init();
Now, we can describe our tests and use the client variable to control the browser. 
Run Code Online (Sandbox Code Playgroud)

文档中提供了webdriverjs API的完整参考,但这是一个简短的示例:

client.url('http://example.com/')
client.getTitle(function(title){
    console.log('Title is', title);
});
client.setValue('#field', 'value');
client.submitForm();
client.end();
Run Code Online (Sandbox Code Playgroud)

让我们用Mocha和Chai语法来描述一个测试; 我们将测试example.com网页的一些属性:

describe('Test example.com', function(){
    before(function(done) {
        client.init().url('http://example.com', done);
    });

    describe('Check homepage', function(){
        it('should see the correct title', function(done) {
            client.getTitle(function(title){
                expect(title).to.have.string('Example Domain');
                done();
            });
        });

        it('should see the body', function(done) {
            client.getText('p', function(p){
                expect(title).to.have.string(
                    'for illustrative examples in documents.'
                );
                done();
            })
        });
    });

    after(function(done) {
        client.end();
        done();
    });
});
Run Code Online (Sandbox Code Playgroud)

您可能希望在许多测试文件上共享一个客户端初始化.创建一个小型Node模块以初始化并将其导入每个测试文件:

client.js:

exports.client = require('webdriverjs').remote({
    // Settings
};
Run Code Online (Sandbox Code Playgroud)

test.js:

var client = require('./client').client;
var expect = require('chai').expect;

// Perform tests
Run Code Online (Sandbox Code Playgroud)

根据评论中的问题编辑:

以下是如何安装selenium服务器,是的,你需要这样做.

下载Selenium Server.它作为单个jar文件分发,您只需运行:

java -jar selenium-server-standalone-2.28.0.jar
Run Code Online (Sandbox Code Playgroud)

执行此命令后,它会立即启动测试代码将连接到的服务器.请注意,每次运行测试时都需要运行Selenium Server.