Selenium RC is ignoring my timeout - selenium

I have a simple Selenium test that runs against a remote Selenium Server instance.
I'm trying to test for page performance, and some pages can exceed the max execution time, and I'm trying to catch that.
No matter what I put in setTimeout(), it always waits for the full page to load or the server times out.
public static $browsers = array(
array(
'name' => 'Firefox on Ubuntu',
'browser' => '*firefox',
'host' => 'dev-ubuntudesktop',
'port' => 4444,
'timeout' => '1000',
),
)
public function testSlowPage() {
$this->setTimeout(1000);
$this->open('myslowaddress');
$this->assertTextNotPresent('Internal Server Error');
}
Even though I'm not using openAndWait, the above example doesn't reach the assert line until after the page is loaded or the web server terminates the request.
What I'd really like is a test that confirms "Page loads in under 1 second", without waiting 30 seconds (or whatever the PHP timeout happens to be set to).

Open method implicitly invokes wait, whether you want it to or not. This wait defaults to 30sec. And setTimeOut is used when your page does not load with in 30 sec. Hence if your page does not load in 30 second then you could use setTimeOut else you tests would fail, so it would be -
selenium.Open(appURL);
selenium.setTimeOt(timeOut);
Now coming to your test objective. You could do assertion to check how long page takes to load, I have checked with 1 sec here -
(It's in java but you should be able to find PHP equivalent)
int testStartTime = Calendar.getInstance().get(13);
selenium.open(appURL);
int testEndTime = Calendar.getInstance().get(13);
Assert.assertTrue((testEndTime-testStartTime)>1, "Fail");
Notice that I have not used setTimeOut method here, hence if page does not load with in 30sec then test would fail any way and you would know that page does not load with in 30 sec. In this situation if you yet want to check page load time then can use exception handling along with time calculation to find page load time.

Related

How do I wait for an element to disappear in Cypress?

I'd like to preface this by saying that I really tried looking here and anywhere else for an answer but without a succes.
Here is the problem:
I'm trying to automate a test for a web application that is loading a bigger amount of data and therefore has to be loading for a while. I have to wait for the page to load to do the next step and I'm trying to figure how to wait for the loading gif to be complete so that my tests can continue. The gif is a basic spinning thingy.
I would like to avoid using implicit wait time (cy.wait()) that is really only the last resort if noone is able to solve this.
What I found o far are these two functions:
cy.waitFor - this seems to be used mostly in situations, where you are waiting for an element to apper - I've tried this in different scenarios and works perfcetly but I havent been able to apply it here.
cy.waitUntil - this seems to be the thing I'm looking for but there is one huge problem. This function seems to have a timeout and I haven't been able to change it any other way than by changing timeouts globally for all the functions. If I set the global timeout to some longer period (minute +), then it works exactly how I would want it to work but obviously I dont really want to have such a long timeout for everything.
The way I see it, there are two possible solutions to this problem>
to change/turn off the timeout for the waitUntil function.
2)Or to somehow make it work with waitFor, because there seems to be no implicit timeout there and it just kind of waits forever for something to happen.
here is the code snippet of the situation:
cy.get('#load-data-button').click() // this clicks the button that stars the loading proces
cy.waitUntil(() => cy.get('body > div > my-app > billing > div > div > div.text-center > img').should('not.be.visible',) ) // this is the function that waits until the loading gif is invisible
I would be eternaly grateful for a solution, because unfortunatelly no one at my company is really a Cypress expert so they are not able to help.
Could you try something like:
cy.get([loading-spinner-identifier]).should("not.be.visible", { timeout: 60000 });
cy.get([an element that should now be visible]).should("be.visible");
It's a bit rough and ready, but I've used it when waiting for spinners to finish up. This waits a minute to see the spinner isn't there and then checks to see that something I'm expecting is there. I'd had varying success with this, but it has helped in some situations.
You can indeed use cy.waitUntil(). As you can see in the documentation of that package (https://www.npmjs.com/package/cypress-wait-until#arguments), the timeout that the function has is just the default (5000 ms) of an argument you can change it. You can even change how often cypress checks the condition you want, so if you do something like:
cy.waitUntil(() => cy.get('body > div > my-app > billing > div > div > div.text-center > img').should('not.be.visible'), {
errorMsg: 'The loading time was too long even for this crazy thing!',
timeout: 300000,
interval: 5000
});
...it will try for 300 seconds (5 minutes) each 5 seconds (just an example with very long timeout).
Also, maybe you can consider to wait until some element is visible after that loading spinner, instead of checking if it dissapeared. If you want to specifically test it, is fine, but if some element appears or becomes interactable after the loading, it could happen that is still not in the state you want, for a fraction of a second, after the spinner is not visible. If that is the case, I would avoid the checking of the spinner dissapearing in first place, unless it adds value to your test, and I would just wait for that element to be in the state you want (for example wait until some input is not disabled, element is visible, text appeared somewhere...).
Add positive and negative assertion to make sure loading spinner is visible.
Default Time is 4s for assert statement.
You can increase defaultTime to wait for assertion to validate by adding configuration in cypress.json
{
...
"defaultCommandTimeout": 4000,
...
}
const waitForSpinner = () => {
// First, make sure the loading indicator shows up (positive assertion)
cy.get('[data-qa="qa-waiting-spinner"]').should('be.visible')
// Then Assert it goes away (negative assertion)
cy.get('[data-qa="qa-waiting-spinner"]').should('not.be.visible')
}

How to change timeout in JDI Test Automation Framework

I need to change timeout for waiting particular webElements. For all of others default is pretty fine. Default timeout could be changed in settings:
timeout.wait.element=10
How to change timeout only for given elements?
For setting specific timeout on element you can use
textField.setWaitTimeout(10);
textField.input("TEXT");
But if you also has some waiting criteria and want to do something after it becomes true you can use
String text = textField.wait(w -> w.getText(), t -> t.equals("test"), 10);
or textField.wait(w -> w.getText().equals("test"), 10); if you just want to wait something
For setting specific implicit timeout on element you can use #WaitTimeout(sec)
#WaitTimeout(sec) — set sec seconds implicit wait for the element
or if you just want to wait until element is present in DOM:
someWebElement.waitFor(10).displayed();

How to reduce tag waiting time in selenium

I'm trying to click a specific link in the web page with specific text.
However, if the link is not present, it takes 1 minute before it prints out element is not found. How do I reduce this time for faster execution?
try{
if (!driver.findElements(By.xpath("//a[text()='specifictext']/#href")).isEmpty())
{
By loadMoreComment=By.linkText("specifictext");
driver.findElement(loadMoreComment).click();
}
}
catch (NoSuchElementException e)
{
logger.warn("Specific text not found");
}
That would only happen because of implicit waits. Look at below definition
Implicit Waits
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
So you should lower that implicit wait if you want an early failure
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
The above call before your code, will ensure the failure happens within 2 seconds
Use Implicit wait to reduce tag waiting time.
Implicit waits are used to provide a waiting time (say 30 seconds)
between each consecutive test steps across the entire test script or
program. Next step only executed when the 30 Seconds (or whatever time
is given is elapsed) after execution of previous step.
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

How to handle a longer wait time for complex queries? Angularjs

I am using angularjs, breeze, mssql2012.
I have several queries that takes about 40 seconds to 1 minute to complete running. It is a search function that goes through several tables with records above 900K with several joins.
It shows up with a few errors:
500 (Internal Server Error)
[Error] Error retrieving dataThe wait
operation timed out Error: The wait operation timed out
I am not sure whether the error is with breeze or angular, but I'd like to make the wait time longer than a minute. The query does work on the server.
I've tried using the $timeout from angular, but it doesn't seem to work.
getSearch().then(function () {
common.$timeout(function () {
toggleSearchSpinner();
}, 1250);
});
Not quite sure how to use the timeout function.
I do have $timeout defined in the common module:
commonModule.factory('common',
['$q', '$rootScope', '$timeout', 'commonConfig', 'logger', common]);
function common($q, $rootScope, $timeout, commonConfig, logger) {
var throttles = {};
var service = {
// common angular dependencies
$broadcast: $broadcast,
$q: $q,
$timeout: $timeout,
};
return service;
}
I have multiple places in the application where queries might just take a long time and unfortunately it is unavoidable. It would be great if there was a one time setting that prolongs the timeout wait time.... Is there?

Does Selenium implicit wait always take the entire wait time or can it finish sooner?

Does Selenium implicit wait always take the entire wait time or can it finish sooner? If I set the implicit wait to 10 seconds, could a call to .findElement finish in a few seconds or would it always take the entire 10 seconds?
This page implies that it waits the full 10 seconds, which is very confusing because its not what the javadoc implies.
The following code comment from WebDriver.java implies that its a polling action which can finish sooner than the implicit timeout is defined at. BUT, the last sentence in the comment really throws a wrench into that belief and makes me not totally sure about it. If it is actually polling, then how would it "adversely affect test time", since it wouldn't go the entire implicit wait duration?
/**
* from WebDriver.java
* Specifies the amount of time the driver should wait when searching for an element if
* it is not immediately present.
* <p/>
* When searching for a single element, the driver should poll the page until the
* element has been found, or this timeout expires before throwing a
* {#link NoSuchElementException}. When searching for multiple elements, the driver
* should poll the page until at least one element has been found or this timeout has
* expired.
* <p/>
* Increasing the implicit wait timeout should be used judiciously as it will have an
* adverse effect on test run time, especially when used with slower location
* strategies like XPath.
*
* #param time The amount of time to wait.
* #param unit The unit of measure for {#code time}.
* #return A self reference.
*/
Timeouts implicitlyWait(long time, TimeUnit unit);
Also, if anyone can provide information on how often the default "polling" occurs?
It can finish once it was able to find the element. If not it does throws the error and stops. The poll time is again very specific to the driver implementation ( not Java bindings , but the driver part, example: FireFox extension, Safari Extension etc.)
As I have mentioned here, these are very specific to the driver implementation. All driver related calls goes via execute method.
I'm putting up the gist over of the execute method (you can find the full source here):
protected Response execute(String driverCommand, Map<String, ?> parameters) {
Command command = new Command(sessionId, driverCommand, parameters);
Response response;
long start = System.currentTimeMillis();
String currentName = Thread.currentThread().getName();
Thread.currentThread().setName(
String.format("Forwarding %s on session %s to remote", driverCommand, sessionId));
try {
log(sessionId, command.getName(), command, When.BEFORE);
response = executor.execute(command);
log(sessionId, command.getName(), command, When.AFTER);
if (response == null) {
return null;
}
//other codes
}
The line:
response = executor.execute(command);
says the whole story. executor is of type CommandExecutor, so all calls goes to the specific driver class like ChromeCommandExecutor,SafariDriverCommandExecutor, which has their own handling.
So the polling is upto the driver implementation.
If you want to specify the polling time, then you should probably start using Explicit Waits.
As mentioned the code comment:
* When searching for a single element, the driver should poll the page until the
* element has been found, or this timeout expires before throwing a
* {#link NoSuchElementException}.
Its going to wait till that element present, or timeout occurs.
For example, If you set Implicit wait as 10 seconds, .findElement is going to wait maximum of 10 seconds for that element. Suppose that element available in the DOM in 5 seconds, then it will come out of "wait" and start executing next step.
Hope this clarifies.
To my knowledge polling period is not 0.5 seconds with implicit wait. It is the case with explicit wait. Explicit wait polls the DOM every 500ms. Implicit wait, if the element is not found on page load waits for the specified time and then checks again after time is run out. If not found it throws an error