Codeception Acceptance Test: How to test AJAX request? - ajax

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');

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');

Why do I see a big difference in loadtime between jMeter and user experience when browsing?

My problem is that I see the load time for a web page element on a test in jMeter # 200 miliseconds and when browsing, most of the time I get 3 or 4 seconds, in the condition where the size in bytes is # 331000.
I must mention that I cleared the cache and cookies for each iteration and I inserted also the constant timer between the steps.
The searching an id is the actual case described previously.
var pkg = JavaImporter(org.openqa.selenium);
var wait_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait);
var wait = new wait_ui.WebDriverWait(WDS.browser, 5000);
WDS.sampleResult.sampleStart()
var searchBox = WDS.browser.findElement(pkg.By.id("140:0;p"));
searchBox.click();
searchBox.sendKeys("1053606032");
searchBox.sendKeys(org.openqa.selenium.Keys.ENTER);
WDS.sampleResult.sampleEnd()
I expected to see the same load time results, but maybe an option would be if I wait until some elements on the search results page are visible. But I cannot bring an argument why is this difference. I had another case where the page loads in 10 seconds in Chrome and in jMeter Test Results 300 miliseconds.
Please try with wait until for a specific element which loads as close as the page load.
Below is another try for the same. Use the below code and check if this helps:-
WDS.sampleResult.sampleStart()
WDS.browser.get('http://jmeter-plugins.org')
//(JavascriptExecutor)WDS.browser.executeScript("return document.readyState").toString().equals("complete")
WDS.browser.executeScript("return document.readyState").toString().equals("complete")
WDS.sampleResult.sampleEnd()
For me without execute script page loads in 3 sec and with executeScript it loads in 7 sec..while in browser that loads in around 7.57sec..
Hope this helps.

Can't seem to run a process in background of Sinatra app

I'm trying to display a number from an api, but I want my page to load faster. So, I'd like to get the number from the api every 5 minutes, and just load that number to my page. This is what I have.
get '/' do
x = Numbersapi.new
#number = x.number
:erb home
end
This works fine, but getting that number from the api takes a while so that means my page takes a while to load. I want to look up that number ahead of time and then every 5 minutes. I've tried using threads and processes, but I can't seem to figure it out. I'm still pretty new to programming.
Here's a pretty simple way to get data in a separate thread. Somewhere outside of the controller action, fire off the async loop:
Data = {}
numbers_api = Numbersapi.new
Thread.new do
Data[:number] = numbers_api.number
sleep 300 # 5 minutes
end
Then in your controller action, you can simply refer to the Data[:number], and you'll get the latest value.
However if you're deploying this you should use a gem like Resque or Sidekiq; it will track failures and is probably optimized more

Regulating / rate limiting ruby mechanize

I need to regulate how often a Mechanize instance connects with an API (once every 2 seconds, so limit connections to that or more)
So this:
instance.pre_connect_hooks << Proc.new { sleep 2 }
I had thought this would work, and it sort of does BUT now every method in that class sleeps for 2 seconds, as if the mechanize instance is touched and told to hold 2 seconds. I'm going to try a post connect hook, but it is obvious I need something a bit more elaborate, but what I don't know what at this point.
Code is more explanation so if you are interested following along: https://github.com/blueblank/reddit_modbot, otherwise my question concerns how to efficiently and effectively rate limit a Mechanize instance to within a specific time frame specified by an API (where overstepping that limit results in dropped requests and bans). Also, I'm guessing I need to better integrate a mechanize instance to my class as well, any pointers on that appreciated as well.
Pre and post connect hooks are called on every connect, so if there is some redirection it could trigger many times for one request. Try history_added which only gets called once:
instance.history_added = Proc.new {sleep 2}
I use SlowWeb to rate limit calls to a specific URL.
require 'slowweb'
SlowWeb.limit('example.com', 10, 60)
In this case calls to example.com domain are limited to 10 requests every 60 seconds.

Ruby/Selenium WebDriver - Pausing test and waiting for user input, i.e. user inputs captcha

I'm using the Selenium WebDriver and Ruby to perform some automation and I ran into a problem of a captcha around step 3 of a 5 step process.
I'm throwing all the automation in a rake script so I'm wondering is there a command to pause or break the script running temporarily until I enter data into the captcha and then continue running on the next page.
To build on seleniumnewbie's answer and assuming that you have access to the console the script is running on:
print "Enter Captcha"
captchaTxt = gets.chomp
yourCaptchaInputWebdriverElement.send_keys captchaTxt
If you just want to pause and enter the captcha in your browser, you can just have it prompt at the console to do that very thing and it'll just sit there.
print "Enter the captcha in your browser"
gets
You could also set the implicit wait to decently long period of time so that Selenium would automatically see the next page and move out. However, this would leave the important Captcha step (for documenting / processes sake) out of your test unless you're pretty anal with your commenting.
Since this is an actively participating test requiring user input I would say that making the tester press "enter" on the console is the way to go.
Since you are writing the test in a script, all you need to do is add a sleep in your test i.e. 'sleep 100' for example.
However, it is bad to add arbitrary sleeps in tests. You can also do something like "Wait for title 'foo'" where 'foo' is the title of the page in Step 4. It need not be title, it can be anything, but you get the idea. Wait for something semantic which indicates that step 3 is done and step 4 is ready to start.
This way, its more targeted wait.
This has been implemented in JAVA, but similar technique.Your solution could be found here

Resources