using click() in Cypress - cypress

I am testing my app using Cypress, in my app there is a one minute timer. When the timer expires, no button should work. To put it more precisely, even though the user clicks on buttons, nothing should happen (functions connected to those buttons should not be triggered). How can I test such a thing?
cy.get('#timer-btn').click().wait(60000)
cy.get('#timer').should('have.text', 'TIME IS UP, your score is: 0')
cy.get('.btn').click()
//???

I believe you could use cy.clock() and .tick() to manipulate the time the browser thinks has passed
cy.clock();
cy.get('#timer-btn').click();
cy.tick(60000);
cy.get('#timer').should('have.text', 'TIME IS UP, your score is: 0');
cy.get('.btn').click();

Instead of using wait use timeout because wait waits for a total of 60 seconds, but with a timeout, it is terminated whenever the expected condition is met.
cy.get('#timer-btn').click()
cy.get('#timer', {timeout: 60000}).should(
'have.text',
'TIME IS UP, your score is: 0'
)
cy.get('.btn').click()
Now if the button is disabled after 60 seconds you can use:
cy.get('#timer-btn').click()
cy.get('#timer', {timeout: 60000}).should(
'have.text',
'TIME IS UP, your score is: 0'
)
cy.get('.btn').should('be.disabled')
Now, if you see on comparing the HTML of the button during and after the timer, if any attribute-value pair is added to the button after the timer is completed, you can assert that using:
cy.get('#timer-btn').click()
cy.get('#timer', {timeout: 60000}).should(
'have.text',
'TIME IS UP, your score is: 0'
)
cy.get('.btn').should('have.attr', 'attr-name', 'attr-value')
If you see just an attribute is added you can just use
cy.get('.btn').should('have.attr', 'attr-name')

Related

Cypress wait for delay in text change assertion?

So Im curious if there is a good solution for this. I have a text field I am asserting on that it changes to a specific text after updating a form in a modal.
The problem is the text takes a good 2 to 3 seconds to change and the field is there beforehand so the assertion fails before it changes.
By default does Cypress "wait" when looking for text changes/assertions on text fields? (IE: <element>.should('contain.text', 'Assertion text here!')
or if it sees ANYTHING will it just do the assertion right away?
Im looking for a "smart" way to wait essentially instead of just cy.wait(x)
From the banner at the top of the documentation on cy.should():
Assertions are automatically retried until they pass or time out.
By default, Cypress commands have a 4 second timeout. If your update consistently happens in under 3 seconds, you probably won't have to modify that value. But, if you needed to, you could modify that timeout value directly in the test.
// below changes the timeout to 10s (10000ms)
// timeout is passed from cy.get to cy.should
cy.get('foo', { timeout: 10000 }).should('have.text', 'bar');

How to set a default wait for Alert with Capyabara?

In my code I don't want to use sleep. How do I use wait_until.
########### my code ###############33
btn_logout.click
sleep 3
page.driver.browser.switch_to.alert.accept
This should work
accept_alert do
btn_logout.click
end
It'll wait for the modal as long as the max wait time that is set for Capybara.

Firing Alerts for an activity which is supposed to happen during a particular time interval(using Prometheus Metrics and AlertManager)

I am fairly new to Prometheus alertmanager and had a doubt regarding firing alerts only during a particular period
I have a microservice which receives a file and does some processing on it, which is only invoked when it gets a message through a Kafka queue. The aforementioned is supposed to come every day between 5 am and 6 am(UTC time). The microservice has a metric which is incremented by 1 every time it receives a file. I want to raise an alert if it does not receive a file in the interval. I have created a query like this :
expr : sum(increase(metric_name[1m]) and on() hour(vector(time()))==5) < 1
for: 1h
My questions:-
1) Is it correct or is there a better way to do it
2) In case of no update, will it return 0 or "datapoints not found"
3) Is increase the correct function as it tends to give results in decimals due to extrapolation, but I understand if increase is 0, it will show 0
I can't really play around with scrape_intervals, which is set at 30s.
I have not run this expression but I expect it will cause an alert to fire at 06:00 only and then go off at 06:01. It is the only time the expression would hold true for one hour.
Answering your questions
It is correct if what you want is a single fire of alert (sending a mail by example) but then no longer firing. Even with that, the schedule is a bit tight and may get hurt by alertmanager delay causing the alert to be lost.
In case of no increase, you will get the expression will evaluate to 0. It will be empty when there is an update
Increase is the right function. It even takes into account reset of the counter.
Answering if there is a better way to do it.
Regarding your expression, you can have the same result, without for clause, with:
expr: increase(metric_name[1h])==0 and on() hour()==6 and on() minute()<1
It reads a : starting at 6am and for 1 minutes, if there was no increase of metric over the lasthour.
Alerting longer
If you want the alert to last longer (say for the day and you silence it when it is solved), you can use sub-queries;
expr: increase((metric and on() hour()==5)[18h:])==0 and on() hour()>5
It reads as : starting at 6am (hour()>5), compute the increase over 5-6am for the next 18 hours. If you like having a pending, you can drop the trailing on() hour()>5 and use a for: 1h clause.
If you want to alert until a file is submitted and thus detect a resolution, simply transform the expression to evaluate the increase until now:
expr: increase((metric and on() hour()>5)[18h:])==0 and on() hour()>5

JMeter Webdriver waittime issue

I have a scenario where I need to click on a button in the Web page which will do a process. Once I click on this button "process in progress" message will appear. I am waiting for this message to disappear from the web page for the next action.
This process will take time between 30 to 150 secs which I don't have control.So I have given a wait time of 180 secs in the sampler. The issue is some time the process will complete in 30 secs and webdriver will wait for 180 secs to complete for the next action. In this case application will log out because inactive user settings.
How to handle this situation?
You can use ExpectedConditions.presenceOfElementLocated combined with WebDriverWait.
It would wait at max 150s but if element is available before it doesn't wait that much:
var wait = new pkg.WebDriverWait(WDS.browser, 150);
wait.until(pkg.ExpectedConditions.presenceOfElementLocated(
pkg.By.cssSelector('ul.suggestions')))
See full details here:
https://jmeter-plugins.org/wiki/WebDriverSampler/
You can go for ExpectedConditions.invisibilityOfElementLocated(By) function which can be used via Explicit Wait so WebDriver will poll the DOM until the element disappears with the maximum of 150 seconds, default polling interval is 500 milliseconds, however it can be adjusted as required.
Example code would be something like:
var wait = new WebDriverWait(WDS.browser, 150)
wait.pollingEvery(1, java.util.concurrent.TimeUnit.SECONDS)
org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[contains(text('process in progress')]"))
More information: The WebDriver Sampler: Your Top 10 Questions Answered

Codeception Acceptance Test: How to test AJAX request?

Well I was using codeception to test on an ajax page, in which I click the button and some kind of text is shown after an AJAX request is performed.
$I->amOnPage('/clickbutton.html');
$I->click('Get my ID');
$I->see('Your user id is 1', '.divbox');
As you see, the test is supposed to work in a way that 'Your user id is {$id}' is returned(in this case the id is 1), and updates a div box with the text. However, it doesnt work at all, instead the test says the div box is blank. What did I do wrong? How can I use codeception to test an AJAX request?
You can also use this:
$I->click('#something');
$I->waitForText('Something that appears a bit later', 20, '#my_element');
20 = timeout (in seconds), give it some sane value.
It's a bit more flexible instead of hammering down things like $I->wait(X);, because they are usually a lot faster than waiting for them in seconds. So, for example, if you've got many elements that you need to "wait" for, let's say 15-20, then your test will spend 15-20 seconds "waiting" while actual operations finish in maybe 1-2s total. Across many tests this can increase build times significantly, which is... not good :)
Are you sure your request has finished by the time you check the div? Try adding a little wait after sending the request:
$I->amOnPage('/clickbutton.html');
$I->click('Get my ID');
$I->wait(1); //add this
$I->see('Your user id is 1', '.divbox');

Resources