I have an object repository file where I store all the locators. However to improve maintainability and readability, I am now grouping the locators using const. For example:
const delivery = {
DELIVERY_HEADING: "xpath=//div[OOtext()='Delivery']",
DELIVERY_COUNT: '.bit-deliverylistrow'
};
const operations = {
SAVE_AUD: '.bit-save-btn',
SAVE_AUDNAME: "xpath=//*[text()='Audience name']/../input"
};
module.exports = { delivery, operations }
In the tests, I am using importing and using them as:
const or = require('../TestData/OR');
await page.focus(or.delivery.DELIVERY_HEADING);
await page.type(or.operations.SAVE_AUDNAME,'hello');
Is there a way I don't have to refer to the const and directly call the object locators in the test as it is difficult to identify which const has which locator ?
I would like to do await page.focus(or.DELIVERY_HEADING)
Any pointers will be helpful.
You can use the spread ... to create a single object.
module.exports = { ...delivery, ...operations }
Now you can do,
await page.focus(or.DELIVERY_HEADING)
Related
I have an existing Nodejs application that still uses CommonJS, it's been fine up till now but I've ran into a module that I'm not sure how to import. I was hoping there was a fast way of doing it rather than restructuring my whole app to the module standard.
Here is the module's import documentation:
import MetaApi, {CopyFactory} from 'module.cloud-sdk';
const token = '...';
const api = new MetaApi(token);
const copyFactory = new CopyFactory(token);
I got the CopyFactory part to work by destructuring like so:
const { CopyFactory } = require('metaapi.cloud-sdk')
const copyFactory = new CopyFactory(token)
But I can't find a way to import the api aspect with the token, is there anyway of doing it?
Thanks a lot
You can do it this way,
const MetaApi = require('metaapi.cloud-sdk');
const {CopyFactory} = MetaApi;
const token = '...';
const api = new MetaApi.default();
const copyFactory = new CopyFactory(token);
Hopefully this will work fine.
As suggested by Bergi, adding default made it work
const { default: MetaApi, CopyFactory } = require(…)
Is there a way to accomplish something like this:
const API_URL = `https://api.my-data-provider.com/items/${id}`
// [...]
// and later on or in another file, use it with something like
const result = await fetch(API_URL(id)) // or API_URL.apply(id), API_URL.apply({ id: 23}), etc...
I want to save template literals in a constants / configuration file, to use them later
Or is there any other standard or well established way to do this kind of thing?
You could use a function for this:
const generateApiUrl = (id) => `https://api.my-data-provider.com/items/${id}`;
const result = await fetch(generateApiUrl(id))
Oh? Hi there!
So, I had a little problem recently.
I'm probably too dumb to realize how to do this, but here it is. I need some sort of way to actually read JSON files. I am requiring them, but I have no idea how to actually use them.
Here is my JSON file:
{
"help": "Displays this message"
}
And my code here:
const cmdh = require("../cmdhandle");
const Discord = require("discord.js");
const util = require("../../util");
const json = require("../help.json");
module.exports = {
aliases: ["?"],
execute:function(msg, cmd) {
// Insert code here so that "def" is a list of elements and definitions in the json file
msg.channel.send(util.genEmbed("Help", def, "#ff5c5c"));
}
}
Edit: ;p i was an idiot back then, looks like this was wayyy easier than i thought lol
You've already loaded the JSON with const json = require("../help.json"), now all you have to do to use the stuff in it is to type json.help or json["help"].
If you want a list of the definitions and elements, you can use the Object.keys and Object.values functions, or Object.entries for key-value pairs.
const cmdh = require("../cmdhandle");
const Discord = require("discord.js");
const util = require("../../util");
const json = require("../help.json");
module.exports = {
aliases: ["?"],
execute:function(msg, cmd) {
// Get all entires
Object.entries(json).forEach(([key, msg]) => {
msg.channel.send(util.genEmbed(key, msg, "#ff5c5c"));
})
}
}
I am using OrientDb with JavaScript and I have tried with startingWith, containing, endingWith, notContaining, notEndingWith, notStartingWith predicates unsuccessfully. Maybe is a wrong implementation from my side but I have not found documentation about how to use.
I've been looking for a way to filter with lambdas to get a sql like behavior but have not been successful. I tried to use the method described in this answer, but it is not working on JavaScript. When using the predicates the answer is an error.
I've tried that too:
What is the equivalent of the gremlin queries in gremlin javascript?
My current JavaScript code:
import * as gremlin from 'gremlin';
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const TextPredicated = gremlin.process.TextP;
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator('usr', 'pwd');
const remote = new DriverRemoteConnection(
'ws://localhost:8182/gremlin', {
authenticator,
traversalSource: 'g'
});
remote.addListener('socketError', (error) => { console.log(`socketError: ${error}`); });
(async () => {
try {
remote.open();
const g = await traversal().withRemote(remote);
const results = await g.V()
.where('username', TextPredicated.containing('john'))
.toList();
console.log(results);
remote.close();
} catch (error) {
console.log(error);
} finally {
remote.close();
}
})();
You don't say what your error is, but I think your Gremlin should use has() rather than where():
const results = await g.V()
.has('username', TextPredicated.containing('john'))
.toList();
Also note that TextP did not become available until TinkerPop 3.4.0 so you'd need to be sure that your graph (in your case, OrientDB) supports at least this version of TinkerPop.
Asynchronous function definitions on MongoDB (Atlas) Stitch display warnings on the GUI editor. Including the example code provided on the reference for Triggers.
The code found here can be was copied over directly to the Stitch Function editor and produces warnings because of the async keyword.
Example code from the docs.
exports = async function (changeEvent) {
// Destructure out fields from the change stream event object
const { updateDescription, fullDocument } = changeEvent;
// Check if the shippingLocation field was updated
const updatedFields = Object.keys(updateDescription.updatedFields);
const isNewLocation = updatedFields.some(field =>
field.match(/shippingLocation/)
);
// If the location changed, text the customer the updated location.
if (isNewLocation) {
const { customerId, shippingLocation } = fullDocument;
const twilio = context.services.get("myTwilioService");
const mongodb = context.services.get("mongodb-atlas");
const customers = mongodb.db("store").collection("customers");
const { location } = shippingLocation.pop();
const customer = await customers.findOne({ _id: customer_id })
twilio.send({
to: customer.phoneNumber,
from: context.values.get("ourPhoneNumber"),
body: `Your order has moved! The new location is ${location}.`
});
}
};
I want to know if Stitch supports the async/await paradigm and if I should be concerned about the warnings shown.
After some testing I found that at this time the async/await keywords cause the linter to throw errors and warnings. This means that for async callbacks it is best to define them separately as it will improve the linting. IE. [].map(async () => {}) will prompt errors that can be worked around.
The runtime execution returns the results as expected from standard asynchronous operations.