Thread.sleep() proving superior to implicitlyWait - eclipse

I have a lot of Thread.sleep() commands in my java selenium webdriver project. I have read often and again that using the sleep command is a bad practice and implicitlyWait command should instead be declared once after a browser instance is launched and it will take care of the whole script. This is not working for me on this project.
As There are a lot of NoSuchElementExceptions being thrown for certain elements, I added a sleep command just before each element that poses a problem. The script passed. When I took off the sleep commands and declared implicitlyWait 30 seconds, script failed with a WebdriverException/NoSuchElementException as webdriver is unable to find each of those elements. Sleep command is finding the elements in 2 or 3 seconds. Is Thread.sleep() proving superior here? If I have to use ExplicitlyWait, then I have to declare a lot of them also, as there are many elements that require expected conditions. Is there a workaround?

Thread.sleep() - Set each time you want to wait. A "dumb" wait. It waits for the time specified, no more, no less. If your element is available in 25ms, it will still wait the 10s that you specified leading to wasted time each run.
Implicit wait - Set once and persists throughout the life of the WebDriver instance. It waits for the specified time for every .findElement() call. So if you ever need to check that an element does not exist, etc. then your check will wait for the time specified in the implicit wait which also leads to wasted time.
Explicit wait (WebDriverWait) - Set each time you want a wait. This is just a specific instance of a FluentWait. It is customizable with all the common needs (visible, clickable, etc.) using ExpectedConditions. This is the best practice.
Fluent wait - Set each time you want a wait. The Swiss Army knife of waits. Can do anything... is fully customizable. Can be useful in very specific instances but generally most of your wait needs will be covered by WebDriverWait with ExpectedConditions.
How I use waits...
I use the page object model and in the constructor for each page object that is run when a page is loading, I use a WebDriverWait to wait for an element on the page to be available. Once that element is available, I know that the full page is loaded. Now I can scrape whatever elements I want off the page. If I ever change the state of the page, I have another WebDriverWait to wait for a new element to appear which signals that the dynamic page change has finished. Now I can scrape at will again.
You really shouldn't need to wait for every .findElement() call. Wait for the page to load or wait for a dynamic page change as I described above and you'll only need waits in very specific instances... not everywhere.
NOTE: Do not mix implicit and explicit waits. You will get bad/weird results.

Use explicit wait for your element to locate. It will wait till the condition or the maximum wait time provided before throwing the Exception.
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); // pass time in seconds as much you need
WebElement myElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your element's id")));
Note:
FluentWait may be another good option too.

Related

How to use waitForAngular() in protractor test scripts?

I wanna implement browser.waitForAngular() in all by e2e test scripts instead of explicit waits. Any help is appreciated....
Usually you don't need to call .waitForAngular() function manually in your tests, since it will be executed automatically before every your action on the page - http://www.protractortest.org/#/infrastructure
However, this does not means you can remove your explicit waits from code. Your tests will be much more stable if you will be using both - automatic wait for angular AND explicit waits for needed elements conditions.

Understanding Chrome Dev Tools timeline

I'm trying to understand why I have several Long Frames reported by Chrome Dev Tools.
The first row (top of the call stack) in the flame chart are mostly Timer Fired events, triggered by jQuery.Deferred()s executing a bunch of $(function(){ }); ready funcs.
If I dig into the jQuery source and replace their use of setTimeout with requestAnimationFrame the flame chart doesn't change much, I still get many of the rAFs firing within a single frame (as reported by dev tools) making long frames. I'd have expected doing the below pseudocode:
window.requestAnimationFrame(function() {
// do stuff
});
window.requestAnimationFrame(function() {
// do more stuff
});
to be executed on two difference animation frames. Is this not the case?
All of the JS that is executing is necessary, but what should I do to execute it as "micro tasks" (as hinted at, but not explained here https://developers.google.com/web/fundamentals/performance/rendering/optimize-javascript-execution) when setTimeout and rAF don't seem to achieve this.
Update
Here's a zoomed in shot of one of the long frames that doesn't seem to have any reflows (forced or otherwise) in it. Why are all the rAF callbacks here being executed in one frame?
Long frames are usually caused by forced synchronous layouts, which is when you (unintentionally) force a layout operation to happen early.
When you write to the DOM, the layout needs to be reflowed because it has been invalidated by the write operation. This usually happens at the next frame. However, if you try to read from the DOM, the layout happens early, in the current frame, in order to make sure that the correct value gets returned. When forced layout occurs, it causes long frames, leading to jank.
To prevent this from happening, you should only perform the write operations inside your requestAnimationFrame function. The read operations should be done outside of this, so as to avoid the browser doing an early layout.
Diagnose Forced Synchronous Layouts is a nicely explained article, and has a simple example demo for detecting forced reflow in DevTools, and how to resolve it.
It might also be worth checking out FastDom, which is a library for batching your read and write. It is basically a queuing system, and is more scalable.
Additional Source:
What forces layout / reflow, by Paul Irish, contains a comprehensive list of properties and methods that will force layout/reflow.
Update: As for the assumption that multiple requestAnimationFrame calls will execute callbacks on separate frames, this is not the case. When you have consecutive calls, the browser adds the callbacks to a document list of animation callbacks. When the browser goes to run the next frame, it traverses the document list and executes each of the callbacks, in the order they were added.
See Animation Frames from the HTML spec for more of the implementation details.
This means that you should avoid using the consecutive calls, especially where the callback function execution times combined exceed your frame budget. I think this would explain the long frames that aren't caused by reflow.

Why my custom activity never returns?

I'm a newbie to WF and rather lost. Here's what I have so far:
I've created a workflow service app (xamlx), added needed variables
I've created a custom NativeActivity where I'm calling CreateBookmark from within Execute, which is between the Receive & Send activity for the service. (Ultimately this will actually do something besides creating the bookmark).
The bookmark gets created just fine, but after stepping out of the Execute method, nothing happens for one minute until the service times out, giving me that message "The request channel timed out while waiting for a reply after 00:00:59.9699970. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout." (I tried posting an image of the xamlx, but as a newbie it won't let me; suffice it to say I'm getting from my Receive, into my custom native activity, but never getting as far as the SendReply).
I assume I'm missing something rather fundatmental, but I can't see what. I've originally tried using NativeActivity<T> to return what I want, but that behaves the same.
Found out what I was doing wrong: needed to use overload of CreateBookmark that has BookmarkOptions parameter and set it to BookmarkOptions.NonBlocking.
Strangely, I did not find one example anywhere that mentioned this.

How a runloop actually works

Earlier this month I asked this question 'What is a runloop?' After reading the answers and did some tries I got it to work, but still I do not understand it completely. If a runloop is just an loop that is associated with an thread and it don't spawn another thread behind the scenes how can any of the other code in my thread(mainthread to keep it simple) execute without getting "blocked"/not run because it somewhere make an infinite loop?
That was question number one. Then over to my second.
If I got something right about this after having worked with this, but not completely understood it a runloop is a loop where you attach 'flags' that notify the runloop that when it comes to the point where the flag is, it "stops" and execute whatever handler that is attached at that point? Then afterwards it keep running to the next in que.
So in this case no events is placed in que in connections, but when it comes to events it take whatever action associated with tap 1 and execute it before it runs to connections again and so on. Or am I as far as I can be from understanding the concept?
"Sort of."
Have you read this particular documentation?
It goes into considerable depth -- quite thorough depth -- into the architecture and operation of run loops.
A run loop will get blocked if it dispatches a method that takes too long or that loops forever.
That's the reason why an iPhone app will want to do everything which won't fit into 1 "tick" of the UI run loop (say at some animation frame rate or UI response rate), and with room to spare for any other event handlers that need to be done in that same "tick", either broken up asynchronously, on dispatched to another thread for execution.
Otherwise stuff will get blocked until control is returned to the run loop.

Tutorial OpenCl event handling

In my last question, OpenCl cleanup causes segfault. , somebody hinted that missing event handling, i.e. not waiting for code to finish, could cause the seg faults. Since then I looked again into the tutorials I used, but they don't pay attention to events (Matrix Multiplication 1 (OpenCL) and NVIDIA_OpenCL_GettingStartedLinux.pdf) or talk about it in detail and (for me) understandable.
Do you know a tutorial on where and how to wait in OpenCL?
Merci!
I don't have a tutorial on events in OpenCL, and I'm by no means an expert, but since no one else is responding...
As a rule of thumb, you'll need to wait for any function named clEnqueue*. Those functions return immediately before the job is done. The easiest way to make sure your queue is finished is to call clFinish(). It won't return until the entire queue has completed.
If you want to get a little fancier, most of the clEnqueue* functions have an optional cl_event parameter that you can pass in. You can check on a particular event with clGetEventInfo(), and you can wait for a particular set of events to finish with clWaitForEvents().