How to remove \n\t in Cypress? - cucumber

I am using BDD with Cypress. The element I want to check is
So this element is on the PageObject:
allowPriceEmbeddedBarcodeAtPOS(){
return cy.get('im-data-table.hydrated').shadow().find('tr:nth-child(2) > td:nth-child(2)')
}
And on the Step Definition file I have:
And("Global Configuration is set true", () => {
platformadmin.allowPriceEmbeddedBarcodeAtPOS().should('contains.text','{"AllowPriceEmbeddedBarcodeAtPOS": true}')
})
But when I run cypress it founds the element with \n\t
Any ideas how can I remove this?

You can use a regex with \s to remove all whitespace
platformadmin.allowPriceEmbeddedBarcodeAtPOS()
.then($el => $el.text().replace(/\s/g, ''))
.should('eq','{"AllowPriceEmbeddedBarcodeAtPOS":true}')
Ref Cypress recipe have-attr-assertion

You can do something like this:
And('Global Configuration is set true', () => {
platformadmin
.allowPriceEmbeddedBarcodeAtPOS()
.invoke('text')
.then((text) => {
expect(text.trim().replace(/[\n\t]/g, '')).to.eq(
'{"AllowPriceEmbeddedBarcodeAtPOS": true}'
)
})
})

Related

Cypress get text value from an element

I am trying to get a text from an element with Cypress in the first test from the first domain and then type it in the second test in another domain, here is a code
I have to grab code from h4.
I implemented next part of code:
get studentCouponValue() {
return cy.get('h4').then(($span) => {
const couponValue = $span.text();
cy.log(couponValue);
})
}
in logs, I see the correct coupon's value, but when I am trying to type it into the field I get an error
The chain approach doesn't fit my expectation, cause i am going to use it in different tests.
Try this:
get studentCouponValue() {
return cy.get('h4').then(($span) => {
const couponValue = $span.innerText;
cy.log(couponValue);
})
}
i resolved
initStudentCouponValue() {
const self = this;
return cy.get('main > .container-fluid').find('h4').then((span) => {
self.couponValue = span.text();
cy.log('First log '+ self.couponValue);
return new Cypress.Promise((resolve) => {
return resolve(self.couponValue);
});
});
}
getStudentCouponValue() {
return this.couponValue;
}
in the test where we want to use value
let couponValue;
admin.initStudentCouponValue().then(() => {
couponValue = admin.getStudentCouponValue()
});
and later we can use
coupoValue
for inputs

Tiptap how to create a paragraph (p) on Shift-Enter, instead of a br?

Using TipTap, I'm trying to avoid adding a <br />, but create a <p></p> instead, with the focus inside that <p>|</p> when the user hit shift-Enter but I can't make it work.
Here's what I did so far:
new (class extends Extension {
keys () {
return {
'Shift-Enter' (state, dispatch, view) {
const { schema, tr } = view.state
const paragraph = schema.nodes.paragraph
console.log(tr.storedMarks)
const transaction = tr.deleteSelection().replaceSelectionWith(paragraph.create(), true).scrollIntoView()
view.dispatch(transaction)
return true
}
}
}
})()
How can I do this?
I don't know if this is still relevant but as I was looking for the same thing, I found two ways to make this work.
NOTE:
I'm using tiptap v2, if that's not a problem, then:
I overrode the HardBreak extension, since it's the one that use the Shift-Enter keybinding. It looks something like;
const CustomHardBreak = HardBreak.extend({
addKeyboardShortcuts() {
return {
"Mod-Enter": () => this.editor.commands.setHardBreak(),
"Shift-Enter": () => this.editor.commands.addNewline(),
};
},
});
And used it like so;
editor = new Editor({
extensions: [
customNewline,
CustomHardBreak,
]
});
Use the default editor command createParagraphNear. E.g this.editor.commands.createParagraphNear()
I tried creating a custom extension from your code and ended up with something similar to the command above, i.e;
export const customNewline = Extension.create({
name: "newline",
priority: 1000, // Optional
addCommands() {
return {
addNewline:
() =>
({ state, dispatch }) => {
const { schema, tr } = state;
const paragraph = schema.nodes.paragraph;
const transaction = tr
.deleteSelection()
.replaceSelectionWith(paragraph.create(), true)
.scrollIntoView();
if (dispatch) dispatch(transaction);
return true;
},
};
},
addKeyboardShortcuts() {
return {
"Shift-Enter": () => this.editor.commands.addNewline(),
};
},
});
And added this as an extension in my editor instance.
PS:
They both work, almost exactly the same, I haven't found a difference yet. But there's somewhat of a 'catch' if you would call it that; Both these methods don't work on empty lines/nodes, a character has to be added before the cursor for it to work, any character, even a space.
In TipTap 2.0 I am able to use this custom extension:
const ShiftEnterCreateExtension = Extension.create({
addKeyboardShortcuts() {
return {
"Shift-Enter": ({ editor }) => {
editor.commands.enter();
return true;
},
};
},
});
To make shift + enter behave like enter.
In my case I actually wanted enter to do something different. So I use prosemirror events to set a ref flag on whether shift was pressed. Than I check that flag under the "Enter" keyboard event -- which could be triggered normally or through the shift + enter extension.

Nightwatch-cucumber: How to use the Gherkin "And" keyword in step definitions?

I am using nightwatch-cucumber to write tests. I have a scenario that reads something like this:
Given I have loaded the dashboard page
And I have clicked on the result menu item
When I click on 'OK' in the prompt box
Then the results page is present
My question is: how do I create a step using the "And" keyword? e.g.:
And(/^I have clicked on the result menu item$/, () => {
return client.click('#results-box');
});
When I try this I get the following error:
ReferenceError: And is not defined
My solution was to use the "defineStep" method as follows:
defineSupportCode(({ Given, When, Then, defineStep }) => {
const And = defineStep;
Given(/^I have loaded the options page$/, () => {
return client
.url('http://localhost:3001/options')
.waitForElementVisible('body', 30000);
});
And(/^I have clicked on the toggle switchd$/, () => {
return client.click('#toggle-switch');
});
When(/^I click on the save button$/, () => {
return client.click('#save-button');
});
....
Switch the And to Given
defineSupportCode(({ Given }) => {
Given(/^I have clicked on the result menu item$/, () => {
return client.click('#results-box');
});
});

Inline if statement for puppet parameters

I have a following puppet module
class base (
$someBoolean=false,
)
{
exec { 'Do something':
command => '/usr/bin/someStuff',
timeout => (someBoolean) ? 100000000 : 300
}
}
The timeout => () ? : is enssentially what I want to do, but what is the correct syntax to do it? Is it possible at all?
Puppet's version of the ternary operator is the more general "selector". The syntax for your case looks like this:
exec { 'Do something':
command => '/usr/bin/someStuff',
timeout => $someBoolean ? { true => 100000000, default => 300 }
}
The control expression ($someBoolean in the above) can in fact be any expression that produces a value, and any number of corresponding cases can be provided.

conditional within define in puppet

Running Puppet 3.8
I have two defines:
define desktop::vinstall () {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
}
}
and
define desktop::vinstallwseeds () {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
file { "/var/cache/debconf/pkg-${title}.seeds":
source => "puppet:///modules/desktop/pkg-${title}.seeds",
ensure => present,
}
}
Would like to turn these into one define statement with an optional boolean argument, something like:
define desktop::vinstallopt ( $queryresponse = 'false', ) {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
if $queryresponse == 'true' {
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
}
file { "/var/cache/debconf/pkg-${title}.seeds":
source => "puppet:///modules/desktop/pkg-${title}.seeds",
ensure => present,
}
}
and then instantiate it with statements like this one in init.pp:
#desktop::vinstallopt { 'gdebi': queryresponse => 'false', }
But doing so gives an error:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Puppet::Parser::AST::Resource failed with argument error ArgumentError: Invalid resource type desktop::vinstallopt at /etc/puppet/modules/desktop/manifests/init.pp:40 on node machine.prvt.net
where line 40 has the syntax above. I'm a newbie with puppet, so my apologies if this turns out the be a simple syntax question. I've tried to find a way to make this work from the PuppetLabs documentation and from other puppet users, so far without luck.
You are trying to embed an if block inside a resource declaration. Alas, this is not possible. The block must be global or in a regular block (e.g., class body, define body, lambda body).
In this case, you want to "amend" the package resource, so to speak. I like to use the following construct for this purpose:
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
}
if $queryresponse {
Package[$title] {
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
}
Please note that this override syntax is only allowed in this scope because the require and responsefile attributes don't have any value assigned originally.

Resources