Cypress Testing - Expect a date format - testing

I'm creating a Cypress test to my rest call API. Anyways I need to verify a date format, for example, in my JSON Response I have:
"documentDate": "2022-01-28"
So, with that i need to verify the date format not its value. There is a way on Cypress "expect" to verify that? Idk, maybe something like:
expect(documentDate)to.have.format('yyyy-MM-dd');
If anyone can help me, it will be great.
Thanks.

An alternative is to use the dayjs library.
The problem with regex is it only checks characters, not date validity
import dayjs from 'dayjs'
it('checks date string is a valid date', () => {
const documentDate = "2022-02-29" // not valid but passes with regex
const parsed = dayjs(documentDate, 'YYYY-MM-DD')
// fails with error "expected 2022-03-01 to equal 2022-02-29"
expect(parsed.format('YYYY-MM-DD')).to.eq(documentDate)
})

You can use a regex for this. You can use the below regex, which has been taken from this StackOverflow thread.
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
In your test you can use:
expect(documentDate).to.match(/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/)

You should us regex to match the format of your date.
\d is regex for matching any digit character (0-9)
{} is quantifier for the preceding token
expect(documentDate).to.match(/\d{4}-\d{2}-\d{2}/)

Related

How to convert date (1991-08-23T00:00:00) which is captured in response and then convert this format (08-23-1991) in vugen?

I am working on a script where I need to submit a form and do some authentication during the steps. I am using LoadRunner Vugen for scripting. For one of the request's response I can see birthdate is coming like below:
"suffixName": ""
"birthDate": "1991-08-23T00:00:00",
"agencyName": ""
In another later request I can see in jason body the same date is used like below
"Body={\agentSuffix":null,"agentFirstName":"XYZ","agentLastName":"WSD","agentBirthDate":"08-23-1991" and so on with additional body.
I am able to capture the date from response by using web_reg_save_param_ex. But now how do I convert the value so that I can use it in next jason body in a custom request. I just need help to capture it.
Captured value: 1991-08-23T00:00:00
Expected value: 08/23/1991
Thanks in advance
Correlate for the string
Use your programming skills in the language of your virtual user to transform transform the string into a new string. Your test code might be C, JavaScript, Java, ... so use language-appropriate string conversion functions.
Hints
"LB=birthDate":"", "RB=T00:00:00"
Conside the loop and what this means for the use of substring and sprintf();
for (counter=0;counter<=strlen(lr_eval_string(MyCorrelatedVarName));counter++)
{ lr_message("%s",&lr_eval_string(MyCorrelatedVarName)[counter]); }

Cypress Assertion

I have a question regarding Cypress assertions, just recently start playing with this testing platform, but got stuck when the URL returns a random number as shown below.
/Geocortex/Essentials/REST/sites/SITE?f=json&deep=true&token=SK42f-DZ_iCk2oWE8DVNnr6gAArG277W3X0kGJL1gTZ7W5oQAAV9iC4Zng4mf0BlulglN-10NK&dojo.preventCache=1575947662312
As you can see token is random and dojo.preventCache is also a random string. I want to detect this url and check if deep=true regardless the token number, but I don't know how to achieve this.
cy.location('origin', {timeout: 20000}).should('contain', '/Geocortex/Essentials/REST/sites/SITE?f=json&deep=true&token=**&dojo.preventCache=**');
Anyone any idea?
You can check both the path and query like this (note that cy.location('origin') doesn't yield neither pathname nor query from your original question, so I'm using cy.url()):
cy.url()
.should('contain', '/Geocortex/Essentials/REST/sites/SITE')
.should('contain', 'deep=true');
or check each separately:
cy.location('pathname').should('contain', '/Geocortex/Essentials/REST/sites/SITE');
cy.location('search').should('contain', 'deep=true');
or, use a custom callback in which you do and assert whatever you want:
cy.url().should( url => {
expect(/* do something with url, such as parse it, and access the `deep` prop */)
.to.be.true;
});

Express routes regex - correct syntax

I looked at the following posts but they did not help with this. It's probably simple, alas...
Express routes parameter conditions
https://forbeslindesay.github.io/express-route-tester/
I have the following regex - /^\d+x\d+/i. I want a number separated by an x, so a route would be /100x100,
The regex works on it's own, but not as a route. I tried various escapeings but I keep getting a 404 back. What would be the correct syntax? (I tried something like this already router.get('/\/^\d+x\d+/i'))
PS - As my plan is only to accept digit x digit, I'd be happy to hear about any flaws in this regex.
That's an interesting problem. This is one solution to achieve what you are looking for.
router.get('^/:dimensions([0-9]+[x][0-9]+)', function(req, res) {
//to show you that it hits the route and what it catches
res.send('Route match for dimensions: ' + req.params.dimensions);
});

Email validation passes without domain extension

I am using Aurelia-Validation in my project and trying to validate email address. When I add an email example#g, it passes the validation. Shouldn't email validation have .com, .net, etc extension at the end to pass the validation? See the plunker link as an example.
Here is the screenshot to show what I mean:
This is a bit nit-picky, but like emix already pointed out in comments, the validation regex used by aurelia-validation is currently the widely accepted standard as specified by WHATWG. This is the same regex as documented on MDN and w3.org.
Mozilla seems to follow this specification for validating input type="email" at least. I couldn't find any official sources on chrome or edge though.
The JavaScript version of that regex is:
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
In simple terms this translates to:
(required) before the # symbol, any number of alphanumeric characters and (certain) symbols
(required) after the # symbol, between 1 and 63 alphanumeric characters or hyphens (and cannot start or end with a hyphen)
(optional) same as 2 (but starting with a period), repeated for any number of times
If you want to restrict this validation to emails which are routable in the internet, simply change the asterisk * at the end of the regex to a plus +. That keeps the regex identical except there must now be at least one segment starting with a period.
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/
Made change to my validation rule with below code:
Original code:
ValidationRules.ensure('setEmail')
.displayName('Email')
.required()
.email()
.on(this);
Modified code with matches pattern:
ValidationRules.ensure('setEmail')
.displayName('Email')
.required()
.email()
.matches(/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)
.on(this);
See the screenshot:
Invalid:
Valid:
In the latest versions I think we can use Validators.pattern(REGEX_VALID_EMAIL) instead of Validators.email in the formControl validator.
and REGEX_VALID_EMAIL can be the following.
const REGEX_VALID_EMAIL = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/
;

Rails 3 - Make text field accept only numeric values

How do I make a text field accept only numeric values? If I press a letter or a symbol, the text field should not be filled, it should only allow numbers.
Is there a rails way to do this?
Use number_field_tag, this will generate a HTML5 number field
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/number_field_tag
On the server side validate numericality:
class SomeModel
validates :some_column, :numericality => {:only_integer => true}
end
and on the client side, add an input mask via javascript https://github.com/ruoso/jquery-regex-mask-plugin
$('#some_input').regexMask(/^\d+$/);
#clyfe's answer is good, but that plugin doesn't work with HTML5 type=number elements. Here's some quick jQuery code that only allows integers:
$("input[type=number]").keypress(function(event) {
if (!event.charCode) return true;
ch = String.fromCharCode(event.charCode);
return (/[\d]/.test(ch));
});
to allow decimals or commas, make the regex look more like those in the plugin, e.g. https://github.com/ruoso/jquery-regex-mask-plugin/blob/master/regex-mask-plugin.js#L8 :
/^((\d{1,3}(\,\d{3})*(((\,\d{0,2}))|((\.\d*)?)))|(\d+(\.\d*)?))$/
(Note that different locales have different conventions for decimals and commas, so it's probably safer to just allow digits :-)
Note also that this is a workaround for the Chrome bugs mentioned here:
https://code.google.com/p/chromium/issues/detail?id=304455
https://github.com/angular/angular.js/issues/2144