How can I get my cypress custom command to ingest this data (i think i structured the data wrong)? - logic

Alright, as the title says- i'm trying to write a custom command for a cypress test suite. The situation as as follows: I have several tests that need to select an indeterminate number of fields and select an option from the each fields list of drop downs.
The logic for this is crayons-in-mouth simple and works fine:
cy.get(selector)
.select(selection)
.scrollIntoView()
and this works great. But because I use it a lot it's a lot of highly repetitive code so I'm trying to create a custom command where I can just inject an array of arrays (various sets of selectors and selections depending on the situation) into it and it'll do the rest.
This is the custom command as I have it written now.
commands.js
Cypress.Commands.add("assignImportFields", (array) => {
cy.wrap(array).each((selector, selection) => {
cy.get(selector)
.select(selection)
.scrollIntoView()
cy.log('using ' + selector + ' to select ' + selection)
})
})
I have the data in a seperate file that looks like this:
data.js
const importFields = {
actorListImports : [
[selectors.lastName, 'Last_Name'],
[selectors.firstName, 'First_Name'],
[selectors.phoneNum, 'Phone_Number']
]
}
exports.importFields = importFields;
and finally, in my test file:
tests.js
const {actorListImports} = data.importFields;
cy.assignImportFields(actorListImports)
The response I get from this is that the 'select' failed because it requires a dom element. My selectors are fine, so I think it's trying to use an entire array (both selector and selection at once) as the selector instead of the first part of the array.
I know i'm not structuring the data correctly, but i've tried a few different variations of it and my primitive monkey brain just can't put together.
Can someone help me identify what's wrong with how i've structure this?

You need to de-structure the array elements in the .each() parameter list, like this cy.wrap(data).each(([selector, selection])
This is a minimal example:
const selectors = {
'lastName': 'lastName'
}
const data = [
[selectors.lastName, 'Last_Name'],
// [selectors.firstName, 'First_Name'],
// [selectors.phoneNum, 'Phone_Number']
]
cy.wrap(data).each(([selector, selection]) => {
console.log(selector, selection)
expect(selector).to.eq('lastName') // passing
expect(selection).to.eq('Last_Name') // passing
})

Related

Use multiple values from the page in an assertion

I have a fairly complex test involving quite a few elements on the page, need to save the values and use them later in an assertion.
Currently I am using aliases to save the values, as per the docs recommendation. Is there a way to avoid deeply nesting like this?
For example,
cy.get(selector1).invoke('val').as('alias1')
cy.get(selector2).invoke('val').as('alias2')
cy.get(selector3).invoke('text').as('alias3')
cy.get(selector4).invoke('text').as('alias4')
cy.get(selector5).invoke('text').as('alias5')
// etc
cy.get('#alias1').then((val1) => {
cy.get('#alias1').then((val2) => {
cy.get('#alias1').then((val3) => {
cy.get('#alias1').then((val4) => {
cy.get('#alias1').then((val5)=> {
// assert values against fixture
expect([val1, val2, val3, val4, val5]).to.deep.eq(myFixture)
When you set an alias, it is also added as a property of the test context object.
You can use the function syntax to access "this" context, and read the values in a single callback at the end of the test.
cy.get(selector1).invoke('val').as('alias1')
cy.get(selector2).invoke('val').as('alias2')
cy.get(selector3).invoke('text').as('alias3')
cy.get(selector4).invoke('text').as('alias4')
cy.get(selector5).invoke('text').as('alias5')
cy.then(function() {
const actual = [this.val1, this.val2, this.val3, this.val4, this.val5]
expect(actual).to.deep.eq(myFixture)
})

Cypress: Is it possible to select an item out of a dynamic dropdown with only a partial word/s?

I have a dropdown list which is always increasing in the number of options available. I Have the 'RecieptName' from the option, however, it is followed by text that is constantly changing. eg: RecieptName: 'Changing sentence including words and numbers'.
What I am trying to do is something along the lines of:
cy.get('#RecieptName').select('RecieptName:');
However, it can't find an option with it as it is followed by changing the numbers. Is it possible to find the option based on a partial option?
You would need to scan the options first, find it's index and select by number
Using .contains() command
cy.get('option')
.contains('ReceiptName:')
.invoke('index') // index among siblings
.then(index => {
cy.get('dropdown').select(index);
})
Using a regex
cy.get('option')
.contains(/^ReceiptName:/)
.invoke('index') // index among siblings
.then(index => {
cy.get('dropdown').select(index);
})
Using selected prop
cy.get('#RecieptName option')
.contains(/^ReceiptName:/)
.invoke('prop', 'selected', true)
The option containing the the text "RecieptName:" can be selected by adding :contains()
cy.get('#RecieptName option:contains(RecieptName:)')
.then($option => {
cy.get('#RecieptName').select($option.text())
})
cy.get('#RecieptName option:contains(RecieptName:)')
.then($option => {
cy.get('#RecieptName').select($option.index())
})
filter will get the DOM element that match a specific selector. Once we get the element, then we get the exact text of RecieptName option by using the invoke(text) and then we pass the same text to the select command, something like this:
cy.get('#RecieptName option')
.filter(':contains("RecieptName:")')
.invoke('text')
.then((recieptNameText) => {
cy.get('dropdown').select(recieptNameText)
})
You can also use the value attribute to directly select the dropdown as I can see the value attributes are unique, I guess that would be the cleanest way to do this:
cy.get('select#RecieptName').select('scoredesc')
cy.get('select#RecieptName').select('modifieddesc')
cy.get('select#RecieptName').select('createdasc')

How to get a value from a datatable using cypress?

I want to get the 'Created On Date' from the datatable to find out which timezone it falls into?
I was searching for a particular campaign using filter. But I am not sure how to extract a text from the datatable.
cy.get('input[placeholder="Filter..."]:nth(2)').type("campaign1");
cy.get("tbody")
.contains("campaign1")
.closest("tr")
.should("contain.text", this.Advertiser)
.should("contain.text", this.Brand)
.then(text => {
const rowText = text;
});
}
But I got this response from Cypress
CypressError: Timed out retrying: expected '<tr.MuiTableRow-root>' to contain text undefined, but the text was 'Advertiser UKBrand UKcampaign14 Nov 2019'
How do I extract just the date from the datable?
You are on the correct way, but you're not searching deep enough. This should help:
cy.get('input[placeholder="Filter..."]:nth(2)').type("campaign1");
cy.get("tbody")
.contains("campaign1")
.closest("tr")
.should("contain.text", this.Advertiser)
.should("contain.text", this.Brand)
.find("td")
.eq(5)
.then(text => {
const rowText = text;
});
You did find the correct row, but not the correct cell. By adding the find("td") it does search for the cells. And the .eq(5) actually selects the 5th occurence of the td, which is the cell for the Created On column
What I can understand from your question is you just want to grab the date values and make an assertion on that.
Assumption Made: With filter applied you always get results and you want to write tests only for the cell with date.
This might help you:
function getTextOfCell(rowIndex){
cy
.get('tbody.MultiTableBody-root')
.get('tr.MultiTableRow-root')
.eq(rowIndex)
.find('td').eq(3).invoke('text').then((txt)=>{
cy.log(txt)
})
}
describe('Test Test', ()=>{
it('Test Test', ()=>{
cy.visit('yourURl')
cy.get('tbody.MultiTableBody-root tr.MultiTableRow-root').its('length').then((rowLength)=>{
for(let i=0; i<rowLength; i++){
getTextOfCell(i)
}
});
})
})
Note:
I am assuming that sometimes you will have more than one row as
result. So iterating over all the available rows.
And the date cell will have always have the index 3 for any
row.
In function getTextOfCell we are grabbing the text of the cell
and saving it in txt. Here I am logging out only. You can
make your assertion here.
The problem is that this.Advertiser is undefined when the should function is called. You didn't provide the piece of code where this member is initialed, but I guess that it's initialed inside a then or it's a property. Anyway, like most Cypress methods, when should is called, it doesn't actually performs the validation, but only queues a command that will be executed later to perform the validation. This means that when should is called, this.Advertiser is still undefined and this gets passed as an argument to theshould function, even though when the should command is executed later on, this.Advertiser has a valid value.
The solution should be, instead of using this.Advertiser, put the entire code block that appears in your question, inside the then block where the value in which this.Advertiser is initialized, and use the parameter passed to this then block instead. It should look something like this:
cy.somethingThatProvidesAdvertiser().then(advertiser => {
cy.get('input[placeholder="Filter..."]:nth(2)').type("campaign1");
cy.get("tbody")
.contains("campaign1")
.closest("tr")
.should("contain.text", advertiser)
.should("contain.text", this.Brand)
.find("td")
.eq(5)
.then(text => {
const rowText = text;
});
});
I guess that you should do the same for this.Brand, which means that you should have the 2 then clauses nested.
I hope I managed to explain it clear enough...
Thanks All for the responses. The below code helped me to fetch the desired value from the datatable.
const txt = [];
cy.get("tbody")
.contains("campaign1")
.parent()
.next("td")
.invoke("text")
.then(x => {
txt.push(x);
});
cy.log((this.txt = txt));
I'd suggest that you assign a cypress-dedicated id on a row/a column to easily extract data and tests are more maintainable.
<tr data-cy='campaign-${id}' ...>
<td data-cy='created-on'>
....
<td>
</tr>
For any given campaign, you can get its text with ease and code is readable.
cy.get('[data-cy=campain-3]').find('[data-cy=created-on]').invoke('text')

How to update item conditionally with branch in RethinkDB

I am trying to do simple upsert to the array field based on branch condition. However branch does not accept a reql expression as argument and I get error Expected type SELECTION but found DATUM.
This is probably some obvious thing I've missed, however I can't find any working example anywhere.
Sample source:
var userId = 'userId';
var itemId = 'itemId';
r.db('db').table('items').get(itemId).do(function(item) {
return item('elements').default([]).contains(function (element) {
return element('userId').eq(userId);
}).branch(
r.expr("Element already exist"),
//Error: Expected type SELECTION but found DATUM
item.update({
elements: item('elements').default([]).append({
userId: 'userId'
})
})
)
})
The problem here is that item is a datum, not a selection. This happens because you used r.do. The variable doesn't retain information about where the object originally came from.
A solution that might seem to work would be to write a new r.db('db').table('items').get(itemId) expression. The problem with that option is the behavior isn't atomic -- two different queries might append the same element to the 'elements' array. Instead you should write your query in the form r.db('db').table('items').get(itemId).update(function(item) { return <something>;) so that the update gets applied atomically.

Is it possible to filter the descendant elements returned from an XPath query?

At the moment, I'm trying to scrape forms from some sites using the following query:
select * from html
where url="http://somedomain.com"
and xpath="//form[#action]"
This returns a result like so:
{
form: {
action: "/some/submit",
id: "someId",
div: {
input: [
... some input elements here
]
}
fieldset: {
div: {
input: [
... some more input elements here
]
}
}
}
}
On some sites this could go many levels deep, so I'm not sure how to begin trying to filter out the unwanted elements in the result. If I could filter them out here, then it would make my back-end code much simpler. Basically, I'd just like the form and any label, input, select (and option) and textarea descendants.
Here's an XPath query I tried, but I realised that the element hierarchy would not be maintained and this might cause a problem if there are multiple forms on the page:
//form[#action]/descendant-or-self::*[self::form or self::input or self::select or self::textarea or self::label]
However, I did notice that the elements returned by this query were no longer returned under divs and other elements beneath the form.
I don't think it will be possible in a plain query as you have tried.
However, it would not be too much work to create a new data table containing some JavaScript that does the filtering you're looking for.
Data table
A quick, little <execute> block might look something like the following.
var elements = y.query("select * from html where url=#u and xpath=#x", {u: url, x: xpath}).results.elements();
var results = <url url={url}></url>;
for each (element in elements) {
var result = element.copy();
result.setChildren("");
result.normalize();
for each (descendant in y.xpath(element, filter)) {
result.node += descendant;
}
results.node += result;
}
response.object = results;
» See the full example data table.
Example query
use "store://VNZVLxovxTLeqYRH6yQQtc" as example;
select * from example where url="http://www.yahoo.com"
» See this query in the YQL console
Example results
Hopefully the above is a step in the right direction, and doesn't look too daunting.
Links
Open Data Tables Reference
Executing JavaScript in Open Data Tables
YQL Editor
This is how I would filter specific nodes but still allow the parent tag with all attributes to show:
//form[#name]/#* | //form[#action]/descendant-or-self::node()[name()='input' or name()='select' or name()='textarea' or name()='label']
If there are multiple form tags on the page, they should be grouped off by this parent tag and not all wedged together and unidentifiable.
You could also reverse the union if it would help how you'd like the nodes to appear:
//form[#action]/descendant-or-self::node()[name()='input' or name()='select' or name()='textarea' or name()='label'] | //form[#name]/#*

Resources