How to use fakerjs in Cypress - cypress

I am trying to use fakerjs in my cypress tests to randomly generate fake data for my forms. I have tried exporting it in support/index.js which did not work.
Is there any standard way to add fakerjs to all cypress specs instead of adding it in every spec file?

First off, what's wrong with importing it in every spec?
That being said, you can do this:
cypress/support/index.js:
cy.faker = require('faker');
your specs:
it(`test`, () => {
const words = cy.faker.lorem.words();
});

I tried it and found very simple. Steps are mentioned below,
1- require faker
const faker = require("faker");
2- Use where you want like this
var firstName = faker.Name.findName(); // Variable declaration
cy.get("#firstName").type(firstName); // Use in locator
I hope it would help :)

For reference javascript :-
Https://zetcode.com/javascript/fakerjs/
You can use these in cypress:
npm i faker
const faker = require("faker");
let username = faker.name.findName()
let email = faker.internet.email()
let password = faker.internet.password()
Use all these in single specs file.

Related

Laravel store request all better way

I have following code:
//Create Assignment
$customer['projects_id'] = $Project->id;
$customer['user_id'] = $customeruser['user_id'];
$customer['user_name'] = $SalesRepresentative->user_name;
$customer['user_role'] = "Admin";
ProjectUser::firstOrCreate($customer);
Is there a way to write this code cleaner and don't have 4 lines? Maybe one instead? Thanks. request has a lot more values.
If you are using a validating request class you can simply call ProjectUser::firstOrCreate($request->validated()); in that case, only validated variables are passed to store method.

this.props.navigation.state.params is undefined

My code worked without problem with react navigation V3, but following an update to version 5.
I had a piece of code to get the name of the route of the active screen, but following an update to version V5, this.props.navigation.state.params is undefined.
this piece of code is no longer functional react navigation V5
const activeScreen = this.props.navigation.state.routes[
this.props.navigation.state.index
].routeName;
Thank you for your answers
For params, you need to use useRoute (or this.props.route)
const params = useRoute().params
https://reactnavigation.org/docs/en/route-prop.html
For navigator's state, you need to use useNavigationState:
const focusedRoute = useNavigationState(state => state.routes[state.index]):
https://reactnavigation.org/docs/en/use-navigation-state.html

How to verify a dynamic value with a space using jasmine expect(check).toContain(value)?

I want to validate a value , which is dynamic and retrieved from one page to another. That element also has space on it.
Below is my coding for that.
Page - 1
var PerAge = element(by.css("h1 > span.ng-binding")).getText();
This element has space on it , like this - > name
Page-2 - > same value displayed in an other page. This element has no space on it.
var HumAge = element(by.repeater("slide in ctrl.slides track by $index")).getText();
Now, I want to validate the value on Page 2 is same or not. Since , the repeater has bunch of other values , so I am using .toContain to validate the value.
The issue here is , the PerAge has space on it.
I checked this stack overflow question , but it did not work.
Expected '> ' to equal '> ' in jasmine.
expect(PerAge).toContain(HumAge);
Its returning following error
Expected 'Shane' to contain ' Shane'.
I tried trim, It doesn't recognize trim.
I cannot use .ToEqual like below since the element have bunch of other things.
expect(PerAge).toEqual('\xA0HumAge')
If I understand you correctly, you retrieve a value on page 1 like this:
var PerAge = element(by.css("h1 > span.ng-binding")).getText();
and use it on page 2 to compare it:
var HumAge = element(by.repeater("slide in ctrl.slides track by $index")).getText()
expect(HumAge).toEqual(PerAge);
This fails due to the spaces.
The reason why you can't use .trim() is because .getText() returns a promise. You first need to resolve the promise before you can use .trim().
What you can do is for example this. You also find an example of the code if you use a transpiler like Babel or use TypeScript which both support Async/Await.
var PerAge, HumAge;
element(by.css("h1 > span.ng-binding")).getText()
.then(function(text) {
PerAge = text.trim();
});
// Go to page 2
element(by.repeater("slide in ctrl.slides track by $index")).getText()
.then(function(text) {
HumAge = text.trim();
});
expect(HumAge).toEqual(PerAge);
// If you can use async await because you transpile with Babel or use typescript you can do this to tidy the code
// $ is the Protractor shorthand for the `css`-locator
const PerAge = (await $('h1 > span.ng-binding').getText()).trim();
const HumAge = (await $('[ng-repeater="slide in ctrl.slides track by $index"]').getText()).trim();
expect(HumAge).toEqual(PerAge);

Need assistance with unfamiliar syntax, error - e is undefined - Google Apps Script(GAS)

I'm using a script exactly like the one on the tutorial here, https://developers.google.com/apps-script/reference/ui/file-upload
However, despite using the syntax I keep getting e is undefined in the statement:
var fileBlob = e.parameter.dsrFile;
I think that means my function doPost(e) is probably wrong somehow. Here is my entire script below.
// Create Menu to Locate .CSV
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName("dsrFile"));
formContent.add(app.createSubmitButton("Start Upload"));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
// Upload .CSV file
function doPost(e)
{
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.dsrFile;
var doc = DocsList.createFile(fileBlob);
}
e is undefined because you are not passing anything to doPost. You have to pass the needed object to doPost. Check where you call the function and what parameters do you pass to it if any. Even if you pass a parameter to that function, it holds undefined value. Make sure that you are passing the correct objects to your functions.
Your script should work perfectly. e is defined by Google Apps Script, not need to pass anything in particular is contains the fields of your form, in particular in this case the file you uploaded.
I would suspect you may be falling foul to the dev url vs publish url syndrome, where you are executing an old scrip rather that the code you are currently working on.
Be sure you script end with 'dev' and not 'exec'
https://script.google.com/a/macros/appsscripttesting.com/s/AKfyck...EY7qzA7m6hFCnyKqg/dev
Let me know if you are still getting the error after running it from the /dev url

How to get part of string after some word with Ruby?

I have a string containing a path:
/var/www/project/data/path/to/file.mp3
I need to get the substring starting with '/data' and delete all before it. So, I need to get only /data/path/to/file.mp3.
What would be the fastest solution?
'/var/www/project/data/path/to/file.mp3'.match(/\/data.*/)[0]
=> "/data/path/to/file.mp3"
could be as easy as:
string = '/var/www/project/data/path/to/file.mp3'
path = string[/\/data.*/]
puts path
=> /data/path/to/file.mp3
Using regular expression is a good way. Though I am not familiar with ruby, I think ruby should have some function like "substring()"(maybe another name in ruby).
Here is a demo by using javascript:
var str = "/var/www/project/data/path/to/file.mp3";
var startIndex = str.indexOf("/data");
var result = str.substring(startIndex );
And the link on jsfiddle demo
I think the code in ruby is similar, you can check the documentation. Hope it's helpful.
Please try this:
"/var/www/project/data/path/to/file.mp3".scan(/\/var\/www(\/.+)*/)
It should return you all occurrences.

Resources