Logging and asserting the number of previously-unknown DOM elements - cypress

I'ts my first tme using Cypress and I almost finalized my first test. But to do so I need to assert against a unknown number. Let me explain:
When the test starts, a random number of elements is generated and I shouldn't have control on such a number (is a requirement). So, I'm trying to get such number in this way:
var previousElems = cy.get('.list-group-item').its('length');
I'm not really sure if I'm getting the right data, since I can not log it (the "cypress console" shows me "[Object]" when I print it). But let's say such line returns (5) to exemplify.
During the test, I simulate a user creating extra elements (2) and removing an (1) element. Let's say the user just creates one single extra element.
So, at the end os the test, I need to check if the number of eements with the same class are equals to (5+2-1) = (6) elements. I'm doing it in this way:
cy.get('.list-group-item').its('length').should('eq', (previousTasks + 1));
But I get the following message:
CypressError: Timed out retrying: expected 10 to equal '[object Object]1'
So, how can I log and assert this?
Thanks in advance,
PD: I also tryed:
var previousTasks = (Cypress.$("ul").children)? Cypress.$("ul").children.length : 0;
But it always returns a fixed number (2), even if I put a wait before to make sure all the items are fully loaded.
I also tryed the same with childNodes but it always return 0.

Your problem stems from the fact that Cypress test code is run all at once before the test starts. Commands are queued to be run later, and so storing variables as in your example will not work. This is why you keep getting objects instead of numbers; the object you're getting is called a chainer, and is used to allow you to chain commands off other commands, like so: cy.get('#someSelector').should('...');
Cypress has a way to get around this though; if you need to operate on some data directly, you can provide a lambda function using .then() that will be run in order with the rest of your commands. Here's a basic example that should work in your scenario:
cy.get('.list-group-item').its('length').then(previousCount => {
// Add two elements and remove one...
cy.get('.list-group-item').its('.length').should('eq', previousCount + 1);
});
If you haven't already, I strongly suggest reading the fantastic introduction to Cypress in the docs. This page on variables and aliases should also be useful in this case.

Related

My flow fails for no reason: Invalid Template Language? This is what I do

Team,
Occasionally my flow fails and its enough test it manually to running again. However, I want to avoid that this error ocurrs again to stay in calm.
The error that appears is this:
Unable to process template language expressions in action 'Periodo' inputs at line '0' and column '0': 'The template language function 'split' expects its first parameter to be of type string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#split for usage details.'.
And it appears in 2 of the 4 variables that I create:
Client and Periodo
The variable Clientlooks this:
The same scenario to "Periodo".
The variables are build in the same way:
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'client = '),1)),'indicator')))
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'period = '),1)),'DATA_REPORT_DELIVERY')))
The same scenario to the 4 variables. 4 of them strings (numbers).
Also I attached email example where I extract the info:
CO NIV ICE REFRESCOS DE SOYA has finished successfully.CO NIV ICE REFRESCOS DE SOYA
User
binary.struggle#mail.com
Parameters
output = 7
country = 170
period = 202204012
DATA_REPORT_DELIVERY = NO
read_persistance = YES
write_persistance = YES
client = 18277
indicator_group = SALES
Could you give some help? I reach some attepmpts succeded but it fails for no apparent reason:
Thank you.
I'm not sure if you're interested but I'd do it a slightly different way. It's a little more verbose but it will work and it makes your expressions a lot simpler.
I've just taken two of your desired outputs and provided a solution for those, one being client and the other being country. You can apply the other two as need be given it's the same pattern.
If I take client for example, this is the concept.
Initialize Data
This is your string that you provided in your question.
Initialize Split Lines
This will split up your string for each new line. The expression for this step is ...
split(variables('Data'), '\n')
However, you can't just enter that expression into the editor, you need to do it and then edit in in code view and change it from \\n to \n.
Filter For 'client'
This will filter the array created from the split line step and find the item that contains the word client.
`contains(item(), 'client')`
On the other parallel branches, you'd change out the word to whatever you're searching for, e.g. country.
This should give us a single item array with a string.
Initialize 'client'
Finally, we want to extract the value on the right hand side of the equals sign. The expression for this is ...
trim(split(body('Filter_For_''client''')[0], '=')[1])
Again, just change out the body name for the other action in each case.
I need to put body('Filter_For_''client''')[0] and specify the first item in an array because the filter step returns an array. We're going to assume the length is always 1.
Result
You can see from all of that, you have the value as need be. Like I said, it's a little more verbose but (I think) easier to follow and troubleshoot if something goes wrong.

Cypress find() keeping state?

I'm using the latest version of Cypress (4.12.0). I'm having problems using two find() commands in a row. It seems to me that Cypress is somehow keeping state, which is changed by doing a find().
Here is my Cypress JS code, to find the contents of three fields that are at the same level:
1. const shipmentContainer: any = cy.get(`div .shipment-container:contains("${SHIPMENT_LABEL} ${shipmentIndex + 1}")`);
2. shipmentContainer.find('div div').contains(`${TRACKING_NUMBER_LABEL} ${shipment[TRACKING_NUMBER_FIELD]}`);
3. shipmentContainer.find('div div').contains(`${SHIPPING_STATUS_LABEL} ${shipment[SHIPPING_STATUS_FIELD]}`);
4. shipmentContainer.find('div div').contains(`${SHIP_METHOD_LABEL} ${shipment[SHIP_METHOD_FIELD]}`);
I'm not including the HTML, because I don't think the details of the data matter.
Here is what is happening. Line 1 sets the container correctly. Line 2 gets the correct data, which I can see in the Cypress UI. But line 3 fails, because it appears to be starting in a totally different location than what the container was set to. If I comment out line 2, then line 3 works, but line 4 doesn't. So only the first find() ever works.
This led me to believe that the container must be keeping state. So I tried the following, but it made no difference at all, which debunked my hyposthesis.
const shipmentContainer: any = cy.get(`div .shipment-container:contains("${SHIPMENT_LABEL} ${shipmentIndex + 1}")`);
_.cloneDeep(shipmentContainer).find('div div').contains(`${TRACKING_NUMBER_LABEL} ${shipment[TRACKING_NUMBER_FIELD]}`);
_.cloneDeep(shipmentContainer).find('div div').contains(`${SHIPPING_STATUS_LABEL} ${shipment[SHIPPING_STATUS_FIELD]}`);
_.cloneDeep(shipmentContainer).find('div div').contains(`${SHIP_METHOD_LABEL} ${shipment[SHIP_METHOD_FIELD]}`);
Is Cypress itself keeping state on a find()? And if so, how can I reset it back so that each of my finds on a container starts in the same location?
That's because cypress commands are running asynchronously:
Return Values
You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.
For me, I often use alias to reuse the commands:
cy.get('div .some-class').as('fancyDiv');
cy.get('#fancyDiv').find('something').contains('bar');
cy.get('#fancyDiv').find('something').contains('foo');

How can I get Watir to make a fresh reference on a non-stale element?

A portion of some tests I am writing calls for checking if an option gets removed from a select list once that option has been used. I am inconsistently getting this error: timed out after 60 seconds, waiting for {:xpath=>"//select[#id = 'newIdentifierType']//option", :index=>31} to be located (Watir::Exception::UnknownObjectException)
It causes my test to fail maybe 2-3 times out of 10 runs and seems kind of random. I think Watir is looking for the "old" select list with this ID since it caches the element and may also include that it had 32 items, but it times out since a select list with this ID and 32 items no longer exists. The new select list has the same ID but only 31 items.
Is there a way to always get a new reference on this element even though it's not technically going stale? Am I experiencing this problem due to a different issue?
My current code for getting the options in the select list:
#browser.elements(:xpath => "//select[#id = 'newIdentifierType']//option")
I am using Ruby/Cucumber with Selenium and Watir Webdriver level. I first tried defining the element as a select_list in a page-object but moved it to the step definitions using #browser.element to see if that would stop the timeout. I thought it may ignore Watir's cached elements and get the most current one with the ID, but that does not appear to be the case.
Please avoid using XPath with Watir. Everything you can do with XPath, Watir has a much more readable API to handle.
To check for a specific option not being there, you should avoid collections and locate directly:
el = browser.select_list(id: "newIdentifierType").option(value: "31"))
# or
el = browser.select_list(id: "newIdentifierType").option(text: "This one"))
Then to see if it has gone away:
el.stale?
# or
el.wait_until(:stale?)
That won't test the right thing if the entire DOM has changed, though, so you might need to just relocate:
browser.select_list(id: "newIdentifierType").option(text: "This one")).present?
If you are intent on using a collection, the correct way to get the list of options is:
options = #browser.select(id: 'newIdentifierType').options
el = options.find { |o| o.text == 'This one' }
# Do things
el.stale?

What means "Name=SWEIPS" Parametr in Siebel

Writing script in LR for Siebel Open UI. All my requests contains this parameter, with different values. What does it mean?
Examples (from different requests):
"Name=SWEIPS", Value = #0'0'1'0'GetProfileAttr'3'attrName'SBRF Position Id'"
"Name=SWEIPS", Value = #0'0''0'3'1-SQE21A, 1-SQL21E, 1SQE31"
And so on.
Can I simple delete it?
Can I simply delete it? - No, you’re not supposed to delete it.
Compare SWEIPS value by recording twice or trice with different data sets, check is there any date/time values in SWEIPS. If there is nothing to correlate leave as it is, no need to delete.
Ensure to correlate values like SWET,ROWID,SWECount,SWEC and so on.

The "getnodevalue" of HTMLUNIT not working on domattr

every time i want to get the Value of my DomAttr i get an TypeError:
My Code:
Wanted = page.getByXPath("//span[contains(.,'Some')]/parent::a/#href");
return this
[DomAttr[name=href value=URLSTRING]]
Now i want to geht the value (=URLSTRING) with Wanted.getNodeName();
but every Time i get the Error
Cannot find function getNodeValue in object [DomAttr[name=href value=
same when i use getValue
please help me
There are some things that make no sense in the code (particularly, because it is not complete). However, I think I can guess what the issue is.
getByXPath is actually returning a List (funny thing you missed the part of the code in which you specify it as a list and replaced it with a Wanted).
Note you should probably also have type warnings in the code too.
Now, you can see that the returned value is in square brackets. That means it is a List (confirming first assumption).
Finally, although you happened to miss that part of the code too, I guess you are directly applying the getValue to the list instead of the DomAttr elements in the list.
How to solve it: If you need more than 1 result iterate over the elements of the list (that Wanted word over there). If you need 1 result then user the getFirstByXPath method.
Were my guesses right?

Resources