Selenium - when exactly the webdriver instance gets updated? - selenium

I'm using selenium to automate a task on a very dynamic website with pyhton.
For this reason certain HTML elements of the current loaded page may or may not be present at the moment of the request from my code.
How exactly the webdriver instance gets updated and receives the new data from the web page?
Is it constantly connected and receive the change in the HTML code instantly?
Or it first download a first verion of the page when driver.get() is called, and then updates it whenever a function such as .find_element_by_class_name() is called?

Q. Is it constantly connected and receives the change in the HTML code instantly?
Ans. Well, for each Selenium command, there will be an HTTP request that will send to the browser driver and then to the server and the command will get, A HTTP response will be returned and the command/commands will get executed based on the response.
Now let's say in a situation you do,
driver.get()
Basically, this is a selenium command.
It would fire an HTTP request stating to launch the URL provided. Based on the response (200-Ok or anything else), you would either see the WebPage getting loaded or an error message.
Same way in Sequence all the Selenium commands will get executed.
It's obvious that we need locators to locate web elements in the UI.
Once we have them, we can tightly couple them with
driver.find_element_by_***
methods.
Things to be noted down here
You need to learn/understand :
Implicit Waits.
Explicit Waits.
Fluent Waits.
Implicit Waits :
By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.
Basically what it means is, whenever you use drive.find_element it gonna look for implicit waits if you have defined one.
If you've not defined one implicit wait, then the default value is 0.
Explicit wait
They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.
FluentWait
FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Reference Link
Updated :
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
Locators (by priority from top):
ID
name
classname
linkText
partialLinkText
tagName
css selector
xpath

Web page is loaded by driver.get().
But the driver doesn't "know" what elements are existing there. It just opens, loads the web page.
To access any element, to check element presence etc. you will need to do that particularly per each specific element / elements using commands like .find_element_by_class_name() with a specific element locator.

Related

How to indicate certain events to Selenium tests?

I'm relatively new to Selenium testing. I have a React SPA I want to create Selenium tests for. I can work with locators and such, but I don't know how I can make the test wait for certain events.
I.e.: The test opens a page where I have a list that is loaded via an API. I need the test to wait until the content is loaded. How can I indicate this to the test? I may be able to check the spinner and wait until it disappears, but there could be situations when the spinner doesn't even appear. Besides, I need to handle events when there are no elements in the list or when there is a communication issue with the API. I really don't want to rely on user messages and spinners.
Should I create an DOM element which is invisible to the user and fill it with the event result so the test can read it? This also doesn't seem a good solution for me.

Robotframework - Selenium - How to verify page loaded completely (successfully) without relying on a specific element in the page?

I was wondering if there is anything in Selenium that can verify if a page loaded completely without relying on a specific element in the page to appear (while using the "wait until page contains" or similar keywords).
The idea is reusability. If there is a need to add new websites to the robot automation later, I do not want to rely on a specific element that might exist in one page but not in another to verify if the page loaded fully.
Is there any keyword that addresses that in Robot Framework - Selenium Library?
Thanks!
Edit - I am aware that some AJAX requests are impossible or extremely difficult to conclude if they finished or not (or if they just keep going forever) so let's assume the website does not have any of that.
Once the page is finished loading, all requests are done.
driver.execute_script("return document.readyState === 'complete'")
use execute script document.readyState , if it returns complete it means the loading have finiished.
But if there is no ajax you don't have to do it selenium does it automatically so your question won't be valid:
https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/#:~:text=Defines%20the%20current%20session's%20page,loading%20takes%20lot%20of%20time.
normal This will make Selenium WebDriver to wait for the entire page
is loaded. When set to normal, Selenium WebDriver waits until the load
event fire is returned.
By default normal is set to browser if none is provided.
I couldn't find some built-in option but there is another way to achieve this. Check if your web app uses any third-party library which shows progress bar, if yes then probably there is function which returns the status of page loading including Ajax call. If not, then you can ask devs to add some progress bar, for instance Pace.JS
document.readyState does not wait for Ajax calls, personally I found it less helpful.

How to identify element for notifications that disappear within few seconds?

I've a test case where after creation of an item, notification is shown, but that notification disappear s within 2-3 seconds.
I want to identify the element for that notification, but when I try to inspect element for that in firebug, it's HTML snippet disappears very quickly since notification itself disappears. Hence I couldn't identify element for it and finding it very difficult to automate.
Can anyone suggest how to deal with such situation?
Since you say "I've a test case", I'm thinking you are testing an application of yours. The plainest way to get around your problem is to know your application. Even if a third-party library is providing the notification code, you can read the doc to see if you can increase the delay or you can read the source code to figure out how it creates the element and where.
If the above fails, then if you can get together a sequence of operations in Selenium that triggers a notification, you should be able to get a serialization of body quickly enough that you can then examine at your leisure. Using Selenium for Python, it would go:
print driver.execute_script("return document.body.outerHTML;")
I would run the code above with redirection to save the output of the print statement to a file that I'd then examine at my leisure. You could make it narrower if you wish by getting the outerHTML of a descendant of body. I like to have a good bit of context so that I know where exactly the element is being created. I've used libraries and configurations that create such notifications as children of body, and some that put them as children of other elements.
Open the previous page before the notification
Press "Ctrl+Shift+c", Navigate to "sources" tab
Do the manual step to generate the notification, which is going to hide in few seconds
Press "F8". Page load scripts will be paused
Then inspect the element as usual and fetch the xpath at your convenience
You can use the below JAVA code to wait for 20 seconds till any element with text 'your notification' appears in the page:
try{
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Your notification here')]")));
}catch(Throwable e){
System.err.println("Error while waiting for the notification to appear: "+ e.getMessage());
}

Persisting DOM en cache problems using Selenium on SlickGrid

I'm testing (using Selenium) a site containing a slickgrid.
To find the correct field to enter a value, I have to apply a filter, and then double click the field to enter the data.
The problem is, that after applying the filter nine out of ten times Selenium ends up with an exception that the element is no longer attached to the DOM, or is not present in the cache anymore. One out of ten doesn't fail on this point.
I've tried about every bit of advice I can find on this issue, but none has brought any sufficient help. Waiting an looping until the element is present, visible etc. doesn't work.
So: is there a way to have Selenium locate an element in a slickgrid after the page has changed because of a filter action?
Thanks!

How to test ajax application using webdriver?

Is there some specific way to test ajax based web application using webdriver?
Yes you should be careful when you write tests for pages that use JavaScript/Ajax heavily.
Main point in such case is to use wait condition each time you do something and result is not available instantly or via page change. When you need to add a wait condition examine behavior of the page and try to find some event that is a sign for you that operation is completed (attribute change, new element appears or disappears and so on).