Appium - webdriverio init is not a function - node.js

I have some code that was generated by Appium Recorder. When I try to run node test.js it says "driver.init is not a function"
I ran npm install webdriverio in the same directory and I have the node_modules directory there.
Here is the contents of test.js:
// Requires the webdriverio client library
// (npm install webdriverio)
// Then paste this into a .js file and run with Node:
// node <file>.js
const wdio = require('webdriverio');
const caps = {"platformName":"android","platformVersion":"6.0.1","deviceName":"Nexus","automationName":"Appium","browserName":"Chrome"};
const driver = wdio.remote({
protocol: "http",
host: "localhost",
port: 4723,
path: "/wd/hub",
desiredCapabilities: caps
});
driver.init()
.element("com.android.chrome:id/url_bar")
.setValue("https://www.google.com/")
.end();
I expect the code to run, but it says TypeError: driver.init is not a function

it seems to depend on the version of webdriverio you are using. I got the same issue when working with the latest version, but with "webdriverio": "^4.6.1", it works. If you go on the Getting Started section, you'll see the new way to use the module : https://webdriver.io/docs/gettingstarted.html
Hope it helps

Related

NPM and NodeJS Compatibility: NodeJS works from PM prompt, but not script

I am attempting to get a lighthouse script running in Node.JS (which I am new to). I followed the intial instructions here https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically. I was able to complete the prior steps in the package manager console (Visual Studio 2017):
npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json
However, I do get an initial warning that NPM only supports Node.JS in versions 4 through 8 and recommends a newer version. The problem is I am running Node v12 and NPM v5 - both the latest.
When I create a script version like below (app.js)
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const config = {
extends: 'lighthouse:default',
settings: {
emulatedFormFactor: 'desktop',
onlyCategories: 'performance',
output: 'json',
outputPath: './report.json'
}
};
function launchChromeAndRunLighthouse(url, opts = null, config) {
return chromeLauncher.launch().then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
return chrome.kill().then(() => results.lhr);
});
});
}
// Usage:
launchChromeAndRunLighthouse('https://airhorner.com/', config).then(results => {
// Use results!
});
And run the command
C:\src\project> node app.js
I get the error - Cannot find module 'lighthouse'
don't install lighthouse locally use it inside your working dir .
first start by running npm init that will create the package.json file inside the current working dir
then npm install --save lighthouse will download it and save it to node_modules now you can use it locally inside your working dir
it should look something like this
app.js
package.json
node_modules/
then run node app.js

Casperjs doesn't work in windows 10

I installed nodejs, python, phantomjs, casperjs.
But when I test with test script file as below.
Path added to Environmental variables.
Also version can be showed at any directory.
Image Attached: Error Output
C:\Users>phantomjs --version
2.1.1
C:\Users>casperjs --version
1.1.4
C:\Users>cd jbm
C:\Users\JBM>cd Desktop
C:\Users\JBM\Desktop>casperjs.bat slmjstest.js
'casperjs.bat' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\JBM\Desktop>casperjs slmjstest.js
CasperError: Can't find module capser
phantomjs://code/bootstrap.js:307 in patchedRequire
var casper = require('capser').create({verbose: true,
logLevel: "debug"});
casper.start('http://casperjs.org/', function() {
this.echo(this.getTitle());
});
casper.thenOpen('http://phantomjs.org', function() {
this.echo(this.getTitle());
});
casper.run();
You have misspelled module name in your require.
var casper = require('capser')
var casper = require('casper').create({verbose: true,
logLevel: "debug"});
Also call the script with just casperjs script.js
Hope that helps!

Gulp: Trouble setting browserSync and Watch

I'm learning Gulp and NPM and decided to test myself by using Browser-Sync with a PHP project I'm working on (using XAMPP). I know Browser-Sync doesnt work with PHP files, but I wanted to use it with my .css files (and later on perhaps add Sass).
I installed npm, Gulp, Gulp-watch and Browser Sync to my project's root directory and all seemed fine.
I then created a gulp.js file with the following:
var gulp = require('gulp'),
watch = require('gulp-watch'),
browserSync = require('browser-sync').create();
gulp.task('watch', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
watch('css/*.css', function() {
browserSync.reload();
});
});
However, when I gulp watch a new browser window does open but just shows the Cannot GET / error.
Also, the URL shows http://localhost:3002/ rather than http://localhost:myproejct
I read this may have something to do with my baseDir so tried:
baseDir: ""
baseDir: "./"
baseDir: "../myproject"
Would anyone know what I've done wrong?
You are doing way more than is necessary to do what you want. You can just use browsersync as a proxy layer over the top of your hosted site.
See the following example from the docs
# Proxy a PHP app + serve static files & watch them
$ browser-sync start --proxy 'mylocal.dev' --serveStatic 'public' --files 'public'
I think this is what you will need, run it from the physical root of your site and replace mylocal.dev with your php server address
npm install browser-sync -g
browser-sync start --proxy 'mylocal.dev' --serveStatic 'css' --files 'css/*.css'
Your code works fine for me. Assuming your target HTML file works if opened in the browser manually: One common cause of the Cannot Get/ error is using an index file other than Browsersync's default expectation, index.html. Could that be the problem you're having? If you need a custom index file, you can set the index option:
browserSync.init({
server: {
baseDir: 'mybasedirectorypath',
index: 'notindex.html'
}
});
Fwiw, you can also do this more efficiently, and save yourself the weight of installing gulp-watch (this example adapted and simplified from this Browsersync docs example):
var gulp = require('gulp'),
browserSync = require('browser-sync').create();
gulp.task('watch', function() {
browserSync.init({
server: {
baseDir: './'
}
});
gulp.watch('css/*.css').on('change',browserSync.reload)
});
As for using a custom url, check out https://github.com/BrowserSync/browser-sync/issues/646 which has some solutions.

Make gulp.js open non-default browser

The title pretty much says it all. I'm on Windows 10, and I have a node app that I start with npm start. (I know almost nothing about node.js.) The app runs on gulp.js. I need the app to open in Firefox, even though Chrome is my default browser. How can I do this? Here's the code that opens the app now; the docs don't allude to there being such an option for the open parameter:
gulp.task('webserver', function() {
gulp.src('./app')
.pipe(webserver({
livereload: true,
open: true,
defaultFile: 'index.html',
port: serverPort
}));
});
Install gulp-open by typing the following in to your console
npm install gulp-open --save
Add the following to the top of your gulpfile.js
var open = require('gulp-open');
Add the following to the end of your gulpfile.js
gulp.task('browser', function(){
var options = {
uri: 'localhost:<enter the port you are using here>',
app: 'firefox'
};
gulp.src(__filename)
.pipe(open(options));
});
Add 'browser' to your gulp default. This is what actually opens the browser at the port you are running your app on
gulp.task('default', ['webserver', 'browser']);
in console type gulp

Error when running cucumberjs ft. selenium, phantomjs, webdriverio

I already installed phantomjs globally npm install -g phantomjs
Started selenium with java -jar selenium-standalone-2.52.0.jar
Configure the World object for cucumberjs properly like this:
var options = {
//desiredCapabilities: { browserName: 'internet explorer' }, // WORK
//desiredCapabilities: { browserName: 'chrome' }, // WORK
desiredCapabilities: { browserName: 'phantomjs' }, // NOT WORK
waitforTimeout : 2000,
host: '127.0.0.1',
port: 4444
};
this.browser = webdriverio.remote(options);
It worked with chrome & IE, but failed for phantomjs.
This is the error I got from Selenium console:
ERROR - org.apache.commons.exec.ExecuteException: Execution failed
(Exit value: -559038737. Caused by java.io.IOException: Cannot run
program "C:\Program Files (x86)\nodejs\phantomjs" (in directory "."):
CreateProcess error=193, %1 is not a valid Win32 application)
C:\Program Files (x86)\nodejs\phantomjs exists.
Do I miss any configuration for phantom / selenium?
Well, finally I found the reason in this similar github issue.
C:\Program Files (x86)\nodejs\phantomjs only works for Linux system. Looks like the webdriver for phantomjs didn't check for the case of running on window application.
To quick-fix the problem, I remove the file C:\Program Files (x86)\nodejs\phantomjs, so selenium will automatically call phantomjs.bat on the same folder. Everything will work fine.

Resources