Running WebdriverIO 'spec' tests as node file - webdriver-io

I'm new to webdriverio. I don't understand how it's supposed to be configured and used within a node application.
How do you run 'spec' tests when webdriverio is being imported? Can it be done?
// based on http://webdriver.io/guide.html
var webdriverio = require('webdriverio');
var options = {
desiredCapabilities: {
browserName: 'firefox'
},
specs: './test/spec/**' // why doesn't this work, when it would work when run from the wdio cli
};
webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.title(function(err, res) {
console.log('Title was: ' + res.value);
})
.end();

There are two ways to use WebdriverIO. The standalone mode allows you to integrate test automation using the WebdriverIO API within arbitrary NodeJS scripts (e.g. this example). It is often used to embed WebdriverIO into a different library like Chimp.js.
The other way is the WDIO test runner (cli runner) which is better suited for sufficient e2e testing. It requires a configuration file (wdio.conf.js or whatever name you want) and to pass this file name as argument to the wdio cli command (e.g. these examples). This is the common way if you want to create an e2e test suite for your project.

Related

Test VueJs localization using Nightwatch

I have an application that I started in English from scratch. I had nightwatch tests and everything is working perfectly fine... but after adding 2 more languages, I want the main tests to run as they used to in English, then change the browser language (since that's the criteria on which I choose the language) so I can run the other tests in German or French... etc. Is there a way to start a test suite by changing the browser's language?
I looked into the documentation and found nothing in this area
In order to set the language and run the tests locally, I did the following:
chrome: {
desiredCapabilities: {
chromeOptions: {
prefs: {
intl: { accept_languages: "ss-ZA" }
}, args: []
}
}
}
this allows you to run the tests locally, on only that language. or you can manage setting that locale code to a variable in a way and try setting it at the beginning of your test suite.
I did not get that far, because in my case, I have to run my tests headless because they will run later on a remote git server that runs only headless tests. According to this issue on Nightwatch's github page, setting a browser language parameter for a headless test is not possible!

Run Same Testcafe tests with different URLs per environment

I am working on a TestCafe proof of concept. I have a few tests working in one test environment. I need a way to run the same tests in up to 3 different test environments that have different URLs. Is there a best practice for this scenario?
A solution is to add custom options on the testcafe command-line like for example : --env=uat.
Use minimist to read all custom options that you have added to the TestCafe command-line and export a config object like this:
import * as minimist from 'minimist';
const args = minimist(process.argv.slice(2));
// get the options --env=xxx --user=yyy from the command line
export const config = {
env: args.env,
user: args.user,
};
Then import that config object wherever you need it in the test code.
see How do I work with configuration files and environment variables? for more details.
In v1.20.0 and later, TestCafe offers a way to specify the baseUrl in the test run setup:
CLI
Program API runner.run({baseUrl})
Config file
You can use this approach along with environment variables or custom command line arguments to determine what url should be assigned to the baseUrl option.
Alternatively, you can have a different configuration file for each test run setup and switch between these files using the --config-file option.

Driving Nightwatch Tests through REPL

I currently have nightwatch.js setup using the vue automated setup. That template is located here.
https://github.com/vuejs-templates/webpack/tree/master/template/test/e2e
Is it possible to run nightwatch assertions through the command line in a REPL like fashion that is available in webdriver.io? Here is a reference to the webdriver feature https://twitter.com/webdriverio/status/806911722682544128
Update, we have moved to using Cypress.io It has made us pretty happy
You can use nightwatch-repl package available on NPM.
https://www.npmjs.com/package/nightwatch-repl
// nightwatch.conf.js
var repl = require('nightwatch-repl');
module.exports = (function (settings) {
repl.init(settings);
...
...
return settings;
})(require('./nightwatch.json'));
Once you run your tests and invoke browser.repl()
you should see the following in your console
Running: Login to dashboard
Type in a command (eg: browser.function()) or type "quit" to exit
repl>

The previously configured ChromeDriver service is still running / Running Webdriver tests with Grunt

Hi I am trying to write mocha tests for my react application that leverage selenium-webdriver.
I have a few questions but help either of them would help so I can move forward.
First of all, ideally, I would like to share the same webdriver sessions across my different tests since I do not care about what order they run. I just want to load the webpage once, run all of the tests and then close the webpage. Is this possible? I initially put my before and after cases in a different file outside of a describe and it was working fine...but then I could not access the instance of the driver in any of my test files.
If sharing the same session is not possible, then how can I solve the error below which occurs when I try to run two specFiles..
Here is the error:
$ grunt test-e2e
Running "mochatest:e2e" (mochatest) task
Running Mocha tests on files
/Users/userName/Desktop/myReactApp/tests/e2e/testSpecOne.js
/Users/userName/Desktop/myReactApp/tests/e2e/testSpecTwo.js
Error: The previously configured ChromeDriver service is still running. You must shut it down before you may adjust its configuration.
at Error (native)
at Object.setDefaultService (/Users/userName/Desktop/myReactApp/node_modules/selenium-webdriver/chrome.js:264:11)
at Object.<anonymous> (/Users/userName/Desktop/myReactApp/tests/e2e/testSpecTwo.js:8:8)
at Module._compile (module.js:556:32)
at loader (/Users/userName/Desktop/myReactApp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/userName/Desktop/myReactApp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
A typical test looks like this:
import assert from 'assert';
import test from 'selenium-webdriver/testing';
import webdriver, {By, until} from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome';
import chromedriver from 'chromedriver';
import helpers from './helpers.js';
chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
test.describe('Main page', () => {
let driver = new webdriver
.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
test.before(() => {
helpers.launchTheApp(driver, 'http://localhost:8000/myApp', 'elementOne', 10000);
});
test.after(() => {
helpers.closeTheApp(driver);
})
test.it('Test some items appear', () => {
helpers.checkIfElementIsPresent(driver, By.className, 'elementOne');
helpers.checkIfElementIsPresent(driver, By.className, 'elementTwo');
helpers.checkIfElementIsPresent(driver, By.className, 'elementThree');
});
});
I am using a grunt-mocha-test to run these tests configured like this
e2e:{
options: {
timeout: 3000000,
ignoreLeaks: true,
ui: 'bdd',
run: true,
log: true,
reporter: typeof process.env.FUSION_BUILD_GENERATED === 'undefined' ? 'spec' : 'xunit-file',
grep: grunt.option('grep')
},
src: ['tests/e2e/**/**/*Spec.js']
}
One possible solution is to run your sessions in isolated environments (for example in Docker containers). However trying to achieve this with standard Selenium is complicated - you have to manually start and stop containers or use an infrastructure automation tool like Ansible. Here is where a new Selenium-compatible tool called Selenoid comes into play. Just request two separate sessions in your test and they will run in parallel inside Docker containers. This gives you a lot of flexibility. My talk about it: https://www.youtube.com/watch?v=TGjpc32my0Y

Test browser code with Intern through Grunt and Phantomjs

I have been playing with Intern and made my tests work in the browser. To better integrate with my current workflow I'm trying to run my tests through grunt with phantomjs. However all my attempts have failed. I've been looking at this question as a reference Intern WebDriver and PhantomJS but can't figure out all of the steps to make it work.
First of all: Is it even possible to run the tests through grunt and phantomjs?
A little bit of info:
I don't want/can't connect to Sauce Labs or a Selenium testing environment.
I want to test browser code while having jQuery as a shimmed dependency
I have Grunt 0.4.1 and phantomjs 1.9.1 installed
I'm not testing any ajax request (as the linked question is having problem with)
I'm not familiar with neither Selenium nor Sauce Lab
If it is possible to test through grunt and phanomjs, how would I go about doing it?
I guess that I have to start the GhostDriver
phantomjs --webdriver=8910
But then what are the important pieces of info in the Intern config to make this work?
define({
proxyPort: 9000,
proxyUrl: 'http://localhost:9000/',
environments: [
{
browserName: 'phantom',
version: '1.9.0',
platform: 'Linux'
}
],
webdriver: {
host: 'localhost',
port: 8910
},
maxConcurrency: 3,
useSauceConnect: false,
// ...
});
How does the environments browserName map to phantomjs? I've tried the browserNames 'phantom' as well as 'phanomjs' with different versions and platforms (running Mac 10.7)
My Gruntfile section for intern looks like:
intern: {
phantom: {
options: {
config: 'tests/intern',
reporters: ['webdriver']
}
}
}
Running this setup without any test-suite that includes browser code outputs
'ReferenceError: document is not defined' in 'node_modules/intern/lib/reporters/webdriver.js:41:19'
Adding browser tests gives 'ReferenceError: window is not defined' in 'src/vendor/jquery/jquery-1.9.1.js:9597:5'
Running it through node gives the same 'window is not defined' error.
node node_modules/intern/client.js config=tests/intern
What am I doing wrong/missing here?
There are two problems with your Gruntfile configuration:
The default run type for the Grunt task is 'client', which means it runs the Node.js client by default, not the test runner. You need to set runType: 'runner' in your Gruntfile options to run tests against your specified environments.
The webdriver reporter is for use in a browser client only and is specified automatically by the test runner when it is needed. You should typically never use it directly. The reporter you specify in the Gruntfile should be the one you want the test runner to use; the console default is usually appropriate.
These two things in combination mean that you’ve configured Intern to try to use a reporter that only works in a browser in conjunction with the test runner inside the Node.js client, hence the error. Once corrected, the remaining configuration should work properly to run on PhantomJS, assuming it is already running on localhost at port 8910.