JQuery aproach to count elements before and after: always same value - cypress

I have a table with columns and below, an icon upon clicking, I can modify the table columns.
Now I want to count the columns before and after. I have a solution which works, where I call the the following before and after and then use the wrapped alias (via parseInt) to compare:
cy.get('body').then(($el) => {
// eslint-disable-next-line #typescript-eslint/no-unsafe-assignment
const countColsNr = $el.find('th[e2e-tag-header]').length;
cy.wrap(**to be named**).as(`${s}`);
});
This counts the actual columns and saves it in the variable to be named.
However, if I use a JQuery approach, it always gets the same column number, which is at the beginning of the test:
const beforetColsNr = Cypress.$('th[e2e-tag-header]').length;
log(beforetColsNr.toString());
... column handling code
... also tried with wait inbetween steps for debug
const afterColsNr = Cypress.$('th[e2e-tag-header]').length;
log(afterColsNr.toString());
Before number and after, are the same! When I look at the state of the browser (screenshot), I can see different columns amount at time of counting in after. This JQ-approach does not count properly the second time or uses the first value.
Is this something which is expected? Or is something I have to investigate?

cypress is asyncrhonous, so beforetColsNr and aftterColsNr are initialized at the same moment.
In the "cypress" mode in your first code block it works because of the usage of the .then().

Related

IMPORTXML To Return a Value from dividendhistory.org

I want to return the value of "Yield:" from a series of stocks from a URL dividenhistory.org, for example HDIF into a Google sheet using IMPORTXML, where F7 represents the user supplied ticker.
=IMPORTXML("https://dividendhistory.org/payout/TSX/"&F7, "/html/body/div/div[2]/div[2]/p[4]")
The problem with the above is the yield value is not always located in the same paragraph, depending on the ticker. It also returns with the word "Yield:" as part of the value.
I believe I should be using the XPATH parameter which should find and return the yield value only, but I am lost. I am open to all suggestions!
I tried with a few of the tickers there, and this should work. For example:
=IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()")
Output:
Yield: 1.05%
Obviously, you can change 'ctas' for any user input.
Try this and see if it works on all tickers.
EDIT:
To get only the number 1.05, you need to split the result and output the 2nd part:
=index(split(IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()"), ": "),2)
Output:
0.0105

How to test the text of the second element in cypress?

I can test the first element of the Dom element but I don't know how to get the second element?
it('should display highlight', () => {
const highlights = cy.get(`.${pageClass} .page_highlight`);
highlights.should('have.length', 2);
highlights.first().should('contain.text', translations.highlight);
});
There are two options for your case.
Since the total number of elements is just two in your case, you can use something called last. You can read more here.
If the number of elements is dynamic, you can use something called eq and pass the order of element as an index. You can read more here.

Cypress - counting number of elements in an array that contain a specific string

Attempting to confirm that of all the schema in the head of a page exactly 3 of them should have a specific string within them. These schemas have no tags or sub classes to differentiate themselves from each other, only the text within them. I can confirm that the text exists within any of the schema:
cy.get('head > script[type="application/ld+json"]').should('contain', '"#type":"Product"')
But what I need is to confirm that that string exists 3 times, something like this:
cy.get('head > script[type="application/ld+json"]').contains('"#type":"Product"').should('have.length', 3)
And I can't seem to find a way to get this to work since .filter, .find, .contains, etc don't filter down the way I need them to. Any suggestions? At this point it seems like I either need to import a custom library or get someone to add ids to these specific schema. Thanks!
The first thing to note is that .contains() always yields a single result, even when many element match.
It's not very explicit in the docs, but this is what it says
Yields
.contains() yields the new DOM element it found.
If you run
cy.get('head > script[type="application/ld+json"]')
.contains('"#type":"Product"')
.then(console.log) // logs an object with length: 1
and open up the object logged in devtools you'll see length: 1, but if you remove the .contains('"#type":"Product"') the log will show a higher length.
You can avoid this by using the jQuery :contains() selector
cy.get('script[type="application/ld+json"]:contains("#type\": \"Product")')
.then(console.log) // logs an object with length: 3
.should('have.length', 3);
Note the inner parts of the search string have escape chars (\) for quote marks that are part of the search string.
If you want to avoid escape chars, use a bit of javascript inside a .then() to filter
cy.get('script[type="application/ld+json"]')
.then($els => $els.filter((index, el) => el.innerText.includes('"#type": "Product"')) )
.then(console.log) // logs an object with length: 3
.should('have.length', 3);

SendKeys() is adding default value (issue) + datetime value sent

basically the issue is taking place at the moment when I send some value which is appended to a default value '01/01/2000' somehow. I've tried different ways to do this without succeed, I've used these exact lines in other script and it worked but I don't know why this isn't working here. Please find below the last code I used followed by the picture with the issue displayed.
var targetStartDate = browser.driver.findElement(by.id('StartDate'));
targetStartDate.clear().then(function () {
targetStartDate.sendKeys('09/01/2016');
})
example of the issue
Thanks in advance for any response.
You can try issuing clear() call before sending keys:
targetStartDate.clear();
targetStartDate.sendKeys('09/01/2016');
The other option would be to select all text in the input prior to sending keys:
// protractor.Key.COMMAND on Mac
targetStartDate.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a"));
targetStartDate.sendKeys('09/01/2016');
I have encountered this same issue before. There is an input mask formatting the input in the field. In order to solve this, you must write your test as if it were the actual user, with the formatting in mind:
var targetStartDate = browser.driver.findElement(by.id('StartDate'));
// Remove the forward slashes because the input field takes care of that.
var inputDate = '09012016';
targetStartDate.clear();
// Loop through each character of the string and send it to the input
// field followed by a delay of 250 milliseconds to give the field
// enough time to format the input as you keep sending keys.
for (var i = 0; i < inputDate.length; i++) {
targetStartDate.sendKeys(inputDate[i]);
browser.driver.sleep(250);
}
Depending on the latency of the site and performance, you may either need to decrease the 250 millisecond delay, or be able to decrease it.
Hope this helps!

Getting values from Kendo Grid row on DetailExpand

I have got a Kendo Grid and I want to access the data from the row whose detail I expanded. For testing purposes, I have this:
function detailExpand(e)
{
var aux = e.sender.MyModelId;
var aux2 = this.MyModelId;
...
But none of those variables have the MyModelId in it.
I have inspected it and I can't find the model properties unless inside the e.sender._data[index-here] but I don't know the index of the row whose detail I've expanded.
e.sender.dataItem(e.masterRow).MyModelId
http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events-detailExpand
http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#methods-dataItem
For the record, you should try to avoid using methods starting with an underscore (_). I believe kendo uses the underscore to show it's an internal method (a "private"). Unexpected behavior could occur.

Resources