How can i get xpath count in selenium webdriver using jasmine javascript? - javascript

var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );
while printing the count I am getting error:
error occuredTypeError: undefined is not a function

Like #alecxe stated, the syntax for getXpathCount() is browser.getXpathCount("//somenode").
I saw you opened an issue on the selenium git and had more code there. What isn't showing here is you have just the following.
var browser = require('selenium-webdriver');
var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );
I haven't used WebDriverJs, so someone please correct me if I am wrong, but I think you need to create a browser object. Right now you only have created a driver object named browser.
Can you try the following snippet?
var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
browser.get('http://en.wikipedia.org/wiki/Wiki');
browser.getXpathCount('//*[#id="www-wikipedia-org"]/div[1]/div');

findElements method returns an Array promise, so you have to do something like this:
browser.findElements(driver.By.xpath('//somenode')).then(function(elements) {
var count = elements.length;
...
})

I think you are not using getXpathCount() correctly. You should do it this way:
browser.getXpathCount("//somenode");

Related

Can't use findElement in Selenium (Javascript)

Then("a Cucumber sentence", async function () {
await driver.findElements(By.xpath('/html/body'))
});
I try to use findElements from Selenium but keep getting
ReferenceError: By is not defined
anything I can do for this?
I've seen it imported like this (looks weird to me):
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;

Using Turndown in Vue - TypeError: TurndownService is not a constructor

I'm trying to convert HTML to Markdown in a Vue-application.
I'm trying to implement Turndown, but I'm getting the error, in the console:
TypeError: TurndownService is not a constructor
I'm using Webpack, to compile it. This is the vue-component I'm trying to use it in:
<script>
var TurndownService = require('turndown');
console.log( new TurndownService() ); // Returns 'undefined';
export default {
mounted() {
var turndownService = new TurndownService();
this.markdownContent = turndownService.turndown(
'A link<p>Hello world</p>'
);
},
...
...
This is what it looks like in node_modules:
I've tried all kind of things, to solve it. Based on this, I tried
var TurndownService = require('turndown').TurndownService;
and
var TurndownService = require('turndown/dist/turndown').TurndownService;
... But no cigar. :-/
Found your post while dealing with the same.
This fixed it for me:
const TurndownService = require('turndown').default;
After that, the regular instructions worked like a charm!

How to show result of console.timeEnd() in react native android app?

I have installed package and import it like this:
import 'react-native-console-time-polyfill';
and have function like this:
search = () => {
let s = this.state.file.toLowerCase();
let p = this.state.search.toLowerCase();
console.time('t');
let result = kmp.findAll(s, p);
let time = console.timeEnd('t');
alert(time);
};
Why the result in alert returned as "undefined"?
My expectation is result time for processing function is show up at alert popup. Search is a function onpress at button.
Sorry my bad english, hopefully you guys understand my question ^^
The way the library you are using is written, it only outputs the resulting time to console, it does not return it. Because of that, while you can see it in debug console, you will always receive undefined from both .time() and .timeEnd() functions. This is also the same for browsers, and you can actually test it in your Javascript console.
However the library's code seems to be short, you can actually add the functionality. If you add return delta.toFixed(3); at the end of the .timeEnd() function (21st line in the index.js) you can get the result you want.

Verifying amount of elements using expect

So I have an element identified
var errorAlert = element.all('[id*="type-alert"]');
I am trying to create a function that will verify there are 2 alerts displayed..
this.isAlertsDisplayed = function() {
return expect(errorAlert.count()).toEqual(2);
};
I keep gettin a friken typeError : .toEqual is not a function when I have seen this example work for others.. What am I doing wrong?
#Danny is on the right track. Except that you are simply missing the by.css() part, replace:
var errorAlert = element.all('[id*="type-alert"]');
with:
var errorAlert = element.all(by.css('[id*="type-alert"]'));
Or with:
var errorAlert = $$('[id*="type-alert"]');
Note that you can spot this kind of problems much earlier in the process - statically even before executing your tests and trying to figure out what went wrong. If you would use ESLint and eslint-plugin-protractor plugin (>=1.29.0), the valid-locator-type rule would warn you if you have passed a literal to element() or element.all(), or if you have passed a "by" locator to $() or $$().
And, if you have ESLint configured in your IDE of choice, here is how you would get the warning during the live-coding (example from PyCharm):
your locator is wrong. Your locator should have by.css() or by.id or something like this.
Try var errorAlert = $$('[id="type-alert"]') or
var errorAlert = element.all(by.id('type-alert'))

cannot denodeify methods in node-ftp module

I am new to both node.js and promise style function call. By looking at an denodeify example at http://runnable.com/Ulatc0QnzUgUAAAK/adapting-node-js-with-q-for-promises, I am trying to denodeify the methods of the node.js node-ftp module as following:
var ftp = require('ftp');
var q = require('q');
var ftpClient = new ftp();
ftpClient.on('ready', function() {
var ftpList = q.denodeify(ftpClient.list);
ftpList().then(function(list) {
console.log(list);
}.then(null, function(err) {
console.log(err);
}).done(function() {
ftpClient.end();
});
});
ftpClient.connect();
However, when running that code with node, it shows the error "list error: TypeError: Object # has no method '_pasv'"
I am not sure what's wrong with that piece of code. Does anyone know what's wrong with that? Can you point me some way to debug/troubleshoot the cause of that error message?
Thanks.
When you pass
ftpClient.list
to Q.denodefiy, you are getting the function object, list from the ftpClient object. It will be just a function and the relationship with the parent is lost. This is important because, the bound function list might be dependent on the ftpClient object. So, you must make sure that link is not broken.
Quoting from the Q.denodeify docs,
Note that if you have a method that uses the Node.js callback pattern,
as opposed to just a function, you will need to bind its this value
before passing it to denodeify, like so:
var Kitty = mongoose.model("Kitty");
var findKitties = Q.denodeify(Kitty.find.bind(Kitty));
The better strategy for methods would be to use Q.nbind, as shown below.
So, you can fix your code in two ways,
Using Q.denodeify and Function.prototype.bind, like this
var ftpList = q.denodeify(ftpClient.list.bind(ftpClient));
Using Q.nbind, like this
var ftpList = q.nbind(ftpClient.list, ftpClient);
you need to use q.nbind
q.nbind(ftpClient.list, ftpClient);

Categories

Resources