未加星标

Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

When it comes to testing your JS code, it is of utmost importance that you include acceptance tests apart from unit tests.

This is where tools like Selenium or PhantomJS come into play. These tools help us run tests against our JS code in a real browser. If we put

in a bit of effort, we can run those on multiple devices or platforms using services like Sauce Labs or BrowserStack .

Enter WebdriverIO !! WebdriverIO lets you write tests, with actions through which you can control the browser and assert the results. You can use services such as Mocha, Cucumber, or Jasmine.

Read more: javascript Testing Framework Comparison: Jasmine vs Mocha

Here we will discuss how to setup Cucumber.js with WebdriverIO. If you are new to Cucumber.js or have not heard about it.

I would suggest reading about Cucumber.js here. You might not find a lot of information regarding it since it’s a bit spread out (might want to use your Google-ing skills :) ).

While using Cucumber.js for testing, you basically write feature files to test multiple scenarios. For instance, the following is a feature file:

# title.feature Feature: Title check I should be able to go to a website and check its title Scenario: Get the title of webpage Given I go to the website "http://www.google.com" Then I expect the title of the page "Google"

If you’ve noticed, the syntax is pretty much self-explanatory and readable. You must be thinking, really… that’s it :open_mouth:? Well, yes and no.

The code above will work as an entry point to your test, but WebdriverIO (the test runner), does not know what those lines mean. This is where step definitions come in.

Step definitions, as the name suggests is JS code (which the runner understands) for each step in the test. For the example above, the following are the step definitions:

// stepdefinitions.js const expect = require('chai').expect; module.exports = function () { this.Given(/^I go to the website "([^"]*)"$/, (url) => { browser.url(url); }); this.Then(/^I expect the title of the page "([^"]*)"$/, (title) => { expect(browser.getTitle()).to.be.eql(title); }); }

You will notice here that for each Given and Then statements, we have corresponding JS definitions. These definitions can be used in other feature or test files as well.

To make things clear, all your test files (or feature files .feature) and step definition files (*.js) are separate.

Now we have our test files and step definitions in place, it’s

time to link them using WebdriverIO, which will bring all these files together and run it for us.

The folder structure looks something like this:


Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO

Before we get started, let’s install WebdriverIO and Cucumber deps first.

[sudo] npm i -g WebdriverIO [sudo] npm i -g wdio-cucumber-framework

In order to start the browser for testing, we need to install and start Selenium. Selenium is the heart of browser automation testing, and WebdriverIO provides binding for Selenium in Node.js.

[sudo] npm i [email protected] -g

Once all of them is installed, navigate to the directory where your feature files and step definitions are present―assuming that, we have

them in ~/project_name/test directory. Let’s continue.

Invoke WebdriverIO

In order to do so, we need to create a configuration file. To create one, type…

wdio

…inside the test folder. This will prompt you to create a configuration file and give you four options.

For the time being, we will concentrate on the first option as that would help us get WebdriverIO up and running on our local machine.

When prompted, select the following options:

Where do you want to execute your tests? On my local machine Which framework do you want to use? cucumber Shall I install the framework adapter for you? No # we already have installed the adapter Where are your feature files located? (./features/**/*.feature) # this is a glob pointing to the folder where all your feature (test files) are located. Where are your step definitions located? (./features/step-definitions) # Your step definitions folder Which reporter do you want to use? dot - https://github.com/webdriverio/wdio-dot-reporter # default reporter Shall I install the reporter library for you? Yes Do you want to add a service to your test setup? Selenium Standalone Level of logging verbosity silent In which directory should screenshots gets saved if a command fails? ./errorShots/ What is the base url? http://localhost # In case you did not provide a full url in tests

After answering all the questions, it will look like this:


Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO

Once that is done, you might want to install the assertion library of your own choice. I am using Chai here. Install and save Chai, using

npm i --save-dev chai

Once you are happy with the configuration, run the tests (in the current directory) using:

selenium-standalone install # install selenium selenium-standalone start # start selenium wdio wdio.conf.js

If everything went according to plan, you will see:


Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO
Pro tip

Instead of starting Selenium separately, there is a plugin which is part of WebdriverIO. This will start Selenium and stop Selenium

according to tests. In order to do that, install the plugin first:

npm i wdio-selenium-standalone-service --save-dev

And, in the wdio.conf.js file, add the following as parent key

exports.config = { ... services: ['selenium-standalone'], ... }

That’s it, you can try running wdio wdio.conf.js again without starting the selenium-standalone. Please make sure that the selenium-standalone is not running in the background.

Hope this helps someone.

In the next tutorial, we will discuss on how to automate the whole process using Gulp .

###Related tutorials you might be interested in

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJavaNode.jsGulp
分页:12
转载请注明
本文标题:Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO
本站链接:http://www.codesec.net/view/483823.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(20)