Protractor-cucumber: isPresent is not function - cucumber

I have my code:
this.Then(/^I should see "([^"]*)" link$/, function (callback) {
var logoutpath = by.xpath('//div[#id="account_logout"]/a');
browser.wait(function() {
return dv.isElementPresent(logoutpath);
}, 30000);
browser.driver.isElementPresent(logoutpath).then(function(isPresent){
expect(isPresent.isPresent()).toBe(true);
browser.driver.findElement(logoutpath).then(function(start){
start.click();
});
});
browser.sleep(2222);
console.log(">>>>>>>"+browser.getTitle());
callback();
});
when i run and get error in console:
TypeError: isPresent.isPresent is not a function
at c:\Users\binhlex\WebstormProjects\untitled\Feature\Steps\login_steps.js:33:30
at [object Object].promise.ControlFlow.runInFrame_ (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1857:20)
at [object Object].goog.defineClass.notify (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2448:25)
at [object Object].promise.Promise.notify_ (c:/Users/binhlex/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:564:12)
at Array.forEach (native)
I have some question?
- Why didn't i use isPresent method?
- When i run console.log(">>>>>>>"+browser.getTitle());, why it display >>>>>>>Promise::222 {[[PromiseStatus]]: "pending"}, how can i use it to verify with expected title of the page?

To your latest question, because browser.getTitle() is a promise, if you want to console.log Title you'd have to do: browser.getTitle().then(function(title){console.log(title)});
For your first question, I don't get why you are trying to obfuscate the code so much. in protractor you don't have to wait for element before clicking it. ( if you don't have ignore synchronization on).
So this:
browser.driver.findElement(logoutpath).then(function(start){
start.click();
equeals:
logoutpath.click()

Related

TypeError: invNum.next is not a function

I have tried this code :
const invNum = require('invoice-number');
router.post('/checkout', async (req, res, next) => {
if (!req.session.cart) {
return res.redirect('/pos/');
}
var saleList = Sale.find().sort({ _id: -1 }).limit(1); // removed (err, data)=>{} to simply view it is working tested already
var settings = await Setting.find({}); // removed try and catch to simply view it is working tested already
var ticketNumber;
ticketNumber = !saleList ? invNum.next('0000000') : invNum.next(saleList.ticket_number);
var sale = new Sale({
ticket_number:ticketNumber,
cart: req.session.cart,
created_at: new Date()
});
sale.save((err, product) => {
createReceipt(settings, req.session.cart, "receipts/"+ticketNumber+".pdf");
req.session.cart = null;
res.redirect('/pos/');
});
});
I got this error:
TypeError: invNum.next is not a function
The problem is with invNum.next().
invNum.next() is a Node.js module to generate invoice number sequentially installed from npm.
Example:
invNum.next('2017/08/ABC001')
// => 2017/08/ABC002
I have tried already suggestions from previous stackoverflow posts by trying Promises or await async function in order to get this code to work. Hopefully, you can help or suggest something. Thank you.
There is a problem in version of invoice-number module. In the npm it is showing as 1.0.6 but in the GitHub repository it has 1.0.5 in the package.json file.
https://github.com/amindia/invoice-number.
I have tested this module by taking from Github repository and it's working fine.
Please take the source of this module from the given link it will works fine.
Seems to be some error in the module. I tried the below code snippet on RunKit
https://runkit.com/embed/ws2lv1y38mt4
var invNum = require('invoice-number')
try{
invNum.next('sdfsd1')
} catch(e){
console.log(e)
}
Getting the same error
I got this error:
TypeError: invNum.next is not a function UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
What is the output when you use the console.log on invNum?
Also use try catch and inside call invNum.next with await. Maybe something inside this function is throwing an error.
Edit: as jfriend00 says, if an plain text (like your "0000...") is working, probably the saleList is returning some error and you are not catching or treating the error.
Edit2: The last update on this NPM code is from 1 year ago and fewer people used this lib, probably is broken.
There is some part of the code from the index.js of the lib:
function _next (invoiceNumber) {
if (!invoiceNumber)
throw new Error('invoiceNumber cannot be empty')
var array = invoiceNumber.split(/[_/:\-;\\]+/)
var lastSegment = array.pop()
var priorSegment = invoiceNumber.substr(0, invoiceNumber.indexOf(lastSegment))
var nextNumber = alphaNumericIncrementer(lastSegment)
return priorSegment + nextNumber}
var api = { next: _next}
module.exports = api

Intern/Leadfoot : Getting error - Expecting a function in instanceof check, but got [object Object] on .Click() method

I am learning Intern/leadfoot and trying to write a simple test. The test is logging an user to the site and logging out the user after verification on the next page.
Using Chromedriver v2.21.
Getting unknown error : Expecting a function in instanceof check, but got [object Object] for click() method for an element. However, the element is being identified and get the value for getVisibleText().
Here is my test Code:
define(function (require) {
var registerSuite = require('intern!object');
var assert = require('intern/chai!assert');
registerSuite({
name: 'Acceptance',
'Login': function () {
return this.remote
.get(require.toUrl('http://example.com'))
.setFindTimeout(5000)
.findByXpath('id("ius-userid")')
.click()
.type('user#user.com')
.end()
.findByXpath('id("ius-password")')
.click()
.type('password')
.end()
.findByXpath('id("ius-sign-in-submit-btn")')
.click()
.end()
.sleep(15000)
},
'HomePage': function () {
return this.remote
.setFindTimeout(5000)
.findByXpath('id("userWelcome")')
.getVisibleText()
.then(function (text) {
assert.strictEqual(text, 'Welcome user#user.com!', 'Vaerify that, the Home page for the logged in user is displayed!');
})
.end()
.findByXpath('id("settingsAndLogout")/A[2]')
.getVisibleText()
.then(function(text){
console.log("The Sign out link text is :...", text.trim());
assert.strictEqual(text.trim(), 'Sign Out', 'Verify that, the Sign Out link is present.');
})
.click()
.end()
}
});
});
And, here is the output:
Listening on 0.0.0.0:9000
Tunnel started
? Created session chrome on any platform (5fcd3559690a324e3a5a3db6cd367387)
√ chrome on any platform - Acceptance - Login (20.268s)
The Sign out link text is :... Sign Out
x chrome on any platform - Acceptance - HomePage (0.13s)
UnknownError: [POST http://localhost:4444/wd/hub/session/5fcd3559690a324e3a5a3db
6cd367387/element/0.8815118646376954-2/click] unknown error: Expecting a functio
n in instanceof check, but got [object Object]
(Session info: chrome=49.0.2623.112)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7
c4),platform=Windows NT 6.1 SP1 x86_64)
at runRequest <node_modules\leadfoot\Session.js:88:40>
at <node_modules\leadfoot\Session.js:109:39>
at new Promise <node_modules\dojo\Promise.ts:411:3>
at ProxiedSession._post <node_modules\leadfoot\Session.js:63:10>
at Element._post <node_modules\leadfoot\Element.js:23:31>
at Element.click <node_modules\leadfoot\Element.js:138:15>
at Command.<anonymous> <node_modules\leadfoot\Command.js:680:19>
at <node_modules\dojo\Promise.ts:393:15>
at run <node_modules\dojo\Promise.ts:237:7>
at <node_modules\dojo\nextTick.ts:44:3>
at Command.target.(anonymous function) [as click] <node_modules\leadfoot\Comm
and.js:674:11>
at Test.registerSuite.IQC_HomePage [as test] <tests\functional\IQC_Acceptance
.js:44:7>
at <node_modules\intern\lib\Test.js:181:24>
at <node_modules\intern\browser_modules\dojo\Promise.ts:393:15>
at runCallbacks <node_modules\intern\browser_modules\dojo\Promise.ts:11:11>
at <node_modules\intern\browser_modules\dojo\Promise.ts:317:4>
at run <node_modules\intern\browser_modules\dojo\Promise.ts:237:7>
at <node_modules\intern\browser_modules\dojo\nextTick.ts:44:3>
at nextTickCallbackWith0Args <node.js:453:9>
at process._tickCallback <node.js:382:13>
No unit test coverage for chrome on any platform
Need help in figuring out the issue. Thanks in advance!
Here's a suggestion:
.findByXpath('id("settingsAndLogout")/A[2]')
.getVisibleText()
.then(function(text){
console.log("The Sign out link text is :...", text.trim());
assert.strictEqual(text.trim(), 'Sign Out', 'Verify that, the Sign Out link is present.');
})
.end()
.findByXpath('id("settingsAndLogout")/A[2]')
.click()
.end()
Not the most elegant solution as findbyXpath is redundant. But the thing is, .click() expects that previous command/element promise returns actual element when resolved (findByXpath will do that).
Hope this helps!

Error handling in Node.js for calling a function with wrong name

I am requiring a module and saving it in a variable. But when I call a module function by wrong name, it does not throw any error or consoles any error. How do I make this throw error?
var module = require('../pre_process/' + preProcessFolder + '/' + preProcessModule);
// module -> { XYZ: [Function] }
//Following does not throw error and doesn't console anything.How to handle/debug this error
module['XY'](result, userId)
.then(function(recData) {
})
I am using q library for promise.
So you want to check if a function (provided by a modul) exists.
You could use try like the example here:
Javascript check if function exists

Node.js Callback Failing - Function pass Variables with Callback

I'm new to Node.js and I'm struggling to get a callback to work. I have the following function call:
memberPhotoPath(dbResults[i].userid2,dbResults[i].userid2Gender,'small',dbResults[i].userid2PhotoName,dbResults[i].userid2PhotoVerified,false,function(path) {
console.log(path);
});
and the following function:
function memberPhotoPath(userid,gender,photoSize,photoName,photoVerification,border,callback) {
if(photoVerification) {
callback('http://www.datingimages.co/online-dating/dating-photos/'+userid+'/'+userid+'-'+photoSize+'-'+photoName+'.jpg');
}else{
if(border) {
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'.png');
}
}else{
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'-noborder.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'-noborder.png');
}
}
}
}
I get the following error in Node.js:
TypeError: undefined is not a function
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:223:21
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:214:9
at Array.forEach (native)
at /etc/node/index.js:208:34
at Query._callback (/etc/node/index.js:287:9)
at Query.Sequence.end (/usr/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
at Query._handleFinalResultPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
Any advice on what I'm doing wrong?
thankyou
I have just run this code in the chrome console and it works fine. The error must be somewhere down the line. With asynchronous functions it is sometimes not possible to track the exact stack trace. I advise you brace yourself and go through your other code again

Node-Webkit with external module containing native code

I'm using node-webkit with an external module called edge.
According to the node-webkit docs modules that contain native code must be recompiled using nw-gyp as oppose to node-gyp. I was able to recompile without error and node-webkit seems to import the module OK.
Heres my code. The code I'm trying to use:
var edge = require('edge.node');
var hello = edge.func(function () {/*
async (input) =>
{
return ".NET welcomes " + input.ToString();
}
*/});
hello('Node.js', function (error, result) {
if (error) throw error;
console.log(result);
});
Which throws the following error when run within node-webkit.
Uncaught TypeError: Object [object Object] has no method 'func'
If write the object out to console.log I can see:
Object {initializeClrFunc: function}
initializeClrFunc: function () { [native code] }
__proto__: Object
So the module seems to have loaded. If I run the same code outside of node-webkit, everything works perfectly and I can access the func function. This is driving me crazy - and any help would be really appreciated.
func method is provided by edge.js, the wrapper around edge.node native module. So you should replace require('edge.node') by require('edge').

Resources