How to filter tests with an array of meta value through testcaferc.json - testcafe

I have tests with different meta data values.
Sample code
import { t, Selector } from "testcafe";
fixture("Filtering test cases example 2");
test("test case 1", async (t) => {
console.log("Test case 1");
}).meta( {"type" : "smoke", "environment" : "qa" , "severity" : "high" });
test("test case 2", async (t) => {
console.log("Test case 2");
}).meta( {"type" : "smoke", "environment" : "qa" , "severity" : "low" });
test("test case 3", async (t) => {
console.log("Test case 3");
}).meta( {"type" : "regression", "environment" : "production" , "severity" : "high" });
test("test case 4", async (t) => {
console.log("Test case 4");
}).meta( {"type" : "regression", "environment" : "qa" , "severity" : "low" });
How to configure the .testcaferc.json file to run with severity low and high.
I was looking for something similar to following to filter the test cases.
{
"browsers": "chrome",
"filter": {
"testMeta": {
"severity" : ["high", "low"]
}
}
}

This is a complex usage scenario and I recommend you use the .testcaferc.js configuration file instead of .testcaferc.json.
Please take a look at the following example:
module.exports = {
filter: function (testName, fixtureName, fixturePath, testMeta, fixtureMeta) {
return testMeta.severity === 'high' ||
testMeta.severity === 'low' ||
testMeta.severity === 'medium';
}
}
You can find more information about the usage of the filter method in this topic: https://testcafe.io/documentation/402657/reference/testcafe-api/runner/filter#header

Related

Angular json, filter unwanted json fields

I have an json from backend:
{
"_embedded" : {
"countries" : [ {
"id" : 1,
"name" : "Brazil",
"code" : "BR",
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/countries/1"
},
"country" : {
"href" : "http://localhost:8080/api/countries/1"
},
"states" : {
"href" : "http://localhost:8080/api/countries/1/states"
}
}
}, {
"id" : 2,
"name" : "Canada",
"code" : "CA",
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/countries/2"
},
"country" : {
"href" : "http://localhost:8080/api/countries/2"
},
"states" : {
"href" : "http://localhost:8080/api/countries/2/states"
}
}
}
}
}
And on frontend, I want to get data according to typescript interface:
interface Country {
id: number;
code: string;
name: string;
}
I have a code which maps above json to interface:
getCountries(): Observable<Country[]> {
return this.httpClient.get<CountriesResponse>(this.countriesUrl).pipe(
map(response => <Country[]>response._embedded.countries)
)
}
interface CountriesResponse {
_embedded: {
countries: Country[]
}
}
But after mapping method still returns data which is not present in typescript interface Country, I mean _links field.
Please suggest how to avoid it.
I actually found some solution but still thinks it could be done better:
getCountries(): Observable<Country[]> {
return this.httpClient.get<CountriesResponse>(this.countriesUrl).pipe(
map(response => {
const data: Country[] = response._embedded.countries
return data.map(c => ({
id: c.id,
name: c.name,
code: c.code
}))
})
)
}

Retrieve data from JSON file by ID in Angular. TypeError: items.find is not a function

Trying to get data by id from JSON file, but gives the error in console
TypeError: items.find is not a function
{
"product" :{
"data" : [
{ "itemID" : "1" , "name" : "pen" , "qty" : "8" }`,
{ "itemID" : "2" , "name" : "notepad" , "qty" : "5" }
]
} }
Function I am using where TypeError: items.find is not a function in console
getitems(itemID: string) {
return this.http.get<Array<Fruits>>('assets/localjson.json')
.pipe(
map((items: Array<any>) => {
return items.find((item: Fruits) => {
return item.itemID=== itemID;
});
})
);
}
office.ts
export class officeitems {
itemID: string;
name: string;
qty: string;
}
You need to use your structure correctly. Your structure is an object and you have treated it as an array.
So, you need to get your data from the product attribute because data is your array.
You need to change your getItems method and fix the interface defined.
map((product: any) => { // <- you need to use your interface Product { } instead of any
return product.data.find((item: Fruits) => {
return item.itemID=== itemID;
});
{
"product" :{
"data" : [
{ "itemID" : "1" , "name" : "pen" , "qty" : "8" }`,
{ "itemID" : "2" , "name" : "notepad" , "qty" : "5" }
]
}
}

How to fetch nested values in MongoDB?

I have a 3 layer nested document something like this.
{
"_id" : ObjectId("5b5acaf0589ff6bfb5dd091f"),
"date" : "2018/07/31",
"clock" : [
{
"time" : "10:12:02",
"values" : [
{
"name" : "A1003",
"value" : "777"
},
{
"name" : "A0001",
"value" : "888"
}
]
},
{
"time" : "13:12:02",
"values" : [
{
"name" : "A1003",
"value" : "111"
}
]
}
]
}
I'm able to sort the date by using $gte $lte as below and all values are fetched.
getData(name: string[], fromDate: string, toDate: string): Promise<{ message: string }> {
return this._mongoUtility.testDb
.then(db => {
let collection = db.collection('TestDoc');
let fromOnlyDate = fromDate.split(' ');
let toOnlyDate = toDate.split(' ');
return collection.find({
'date': {
$gte: `${fromOnlyDate[0]}`,
$lte: `${toOnlyDate[1]}`
}
}).toArray();
})
.catch(err => {
return Promise.reject({message: 'Data not found', err: err})
})
}
I want to filter by using time and again by name and should display the value.
I tried in many ways but I'm getting the result. Is there nay other method to do so in MongoDB? Kindly suggest.
Expected output should look like below
0:{date: "2018/07/31 10:12:02", value-A1003: "777", value-A0001: "888"}
1:{date: "2018/07/31 13:12:02", value-A1003: "111"}
you can use the aggregate framwork to achive a simmler result(you cant create attr by value)
db.getCollection('sss').aggregate([
{$unwind:'$clock'},
{$unwind:'$clock.values'},
{$project:{
date: {$concat:[ "$date" ,' ' , "$clock.time" ]},
value:'$clock.values.value',
name:'$clock.values.name'
}}
])

How to access nested data in json string in typescript

I have this json structure an can't find a way to access the data values(data1, data2 and date), i'd like to have those values in an array than i can sort by date:
{
"07" : {
"07" : {
"data1" : "-1",
"data2" : "test",
"date" : "1995-07-07"
},
"08" : {
"data1" : "1",
"data2" : "test",
"date" : "1995-07-08"
},
"09" : {
"data1" : "-1",
"data2" : "test",
"date" : "1995-07-09"
},
"10" : {
"data1" : "-1",
"data2" : "test",
"date" : "1995-07-10"
}
},
"08" : {
"07" : {
"data1" : "1",
"data2" : "test",
"date" : "1995-08-07"
},
"08" : {
"data1" : "1",
"data2" : "test",
"date" : "1995-08-08"
},
"09" : {
"data1" : "1",
"data2" : "test",
"date" : "1995-08-09"
}
}
}
Because my keys aren't defined as constant i don't know what they'll be in advance.
Polyfill for Object.entries:
const reduce = Function.bind.call(Function.call, Array.prototype.reduce);
const isEnumerable = Function.bind.call(Function.call, Object.prototype.propertyIsEnumerable);
const concat = Function.bind.call(Function.call, Array.prototype.concat);
const keys = Reflect.ownKeys;
if (!Object.values) {
Object.values = function values(O) {
return reduce(keys(O), (v, k) => concat(v, typeof k === 'string' && isEnumerable(O, k) ? [O[k]] : []), []);
};
}
if (!Object.entries) {
Object.entries = function entries(O) {
return reduce(keys(O), (e, k) => concat(e, typeof k === 'string' && isEnumerable(O, k) ? [[k, O[k]]] : []), []);
};
}
Code:
for (const [key, value] of Object.entries(myObject))
{
for (const [key2, value2] of Object.entries(value))
{
value2.data1;
value2.data2;
value2.date;
}
}
Instead Object.entries you can enumerate object like this.
for (var key in myObject)
{
for (var key2 in myObject[key])
{
myObject[key][key2].data1;
myObject[key][key2].data2;
myObject[key][key2].date;
}
}
You can get all the key names from your json as an Array by calling the method:
keys = Object.getOwnPropertyNames(jsonObj);
In your example this will return an array ['07', '08'] to get the actual objects from the name you can call:
keys.forEach((key) => {
objects = Object.getOwnPropertyDescriptor(jsonObj, key)
})
And then you can find the names of the keys of these objects and repeat
objects.forEach((object) => {
keys = Object.getOwnPropertyNames(object);
})

retrieve an element from a json list with angularjs

I have this list JSON:
[{
"password" : "ppp",
"function" : 0,
"id" : 1,
"login" : "ness",
"nom" : "nesrine",
"mail" : "nes#gmail",
"tel" : "238555555"
},
{
"password" : "pass",
"function" : 0,
"id" : 2,
"login" : "bilel.troudi",
"nom" : "bilel",
"mail" : "bilel.troudi91#gmail",
"tel" : null
},
{
"password" : "undefined",
"function" : 1,
"id" : 4,
"login" : "undefined",
"nom" : "ahmed",
"mail" : "ahmed#gmail.com",
"tel" : "221474"
},
{
"password" : "khm",
"function" : 0,
"id" : 5,
"login" : "khm",
"nom" : "khmayes",
"mail" : "bke#live.fr",
"tel" : "235684522"
}
]
I want to retrieve the names(nom) of users with angular I recovered this list in a variable in my code.
if you do not want to use angular js then you can use simple jquery too.
var list = [{"password":"ppp","function":0,"id":1,"login":"ness","nom":"nesrine","mail":"nes#gmail","tel":"238555555"},{"password":"pass","function":0,"id":2,"login":"bilel.troudi","nom":"bilel","mail":"bilel.troudi91#gmail","tel":null},{"password":"undefined","function":1,"id":4,"login":"undefined","nom":"ahmed","mail":"ahmed#gmail.com","tel":"221474"},{"password":"khm","function":0,"id":5,"login":"khm","nom":"khmayes","mail":"bke#live.fr","tel":"235684522"}];
var nameList = [];
for(var i=0;i<list.length;i++){
nameList.push(list[i].nom);
}
$scope.list =[{"password":"ppp","function":0,"id":1,"login":"ness","nom":"nesrine","mail":"nes#gmail","tel":"238555555"},{"password":"pass","function":0,"id":2,"login":"bilel.troudi","nom":"bilel","mail":"bilel.troudi91#gmail","tel":null},{"password":"undefined","function":1,"id":4,"login":"undefined","nom":"ahmed","mail":"ahmed#gmail.com","tel":"221474"},{"password":"khm","function":0,"id":5,"login":"khm","nom":"khmayes","mail":"bke#live.fr","tel":"235684522"}];
$scope.nameList = [];
angular.forEach($scope.list,function(Obj,val){
$scope.nameList.push(Obj.nom);
});
you can use this code. in $scope.nameList you can get all names.
For this code you have to use angular js
Try
function getNames() {
return $http.get("/allU").then(function(result) {
return result.data.map(function(item) {
return item.nom;
});
});
}
getNames().then(function(data) {
$scope.names = data; //$scope.names = ["nesrine", "bilel", "ahmed", "khmayes"]
})