Is there a way to get a QTP to fail a test if a particular element appears on a web page - testing

I am trying to test a JSP based web application with QTP. On some of the pages the JSP is coded to return a particular div element, which will have an ID attribute, to the browser, only of the underlying model has a certain boolean flag set. I'd like to be able to develop a QTP test that fails if the div is present in the returned web page. However, the QTP documentation doesn't seem to have any details on how to do this.

The point is to detect if the condition applies and then explicitly fail the test.
If Browser("b").Page("p").WebElement("html tag:=div", "html id:=theId").Exist Then
''# Report failure
Reporter.ReportEvent micFail, "Element Exists", "It shouldn't"
''# if you also want to stop the test
ExitTest
End If

The default behavior for QTP is to write in 'Report' when a check point fails. i.e. if an element is not found on the page , automatically the QTP will write in the log report. In order to disable that , and to customize your report depending on your test scenarios, you can disable the report logging from the
beginning and write only in case you found an abnormal behavior.
Reporter.Filter = rfDisableAll
'check point validations
Reporter.Filter = rfEnableAll

Related

Selenium - when exactly the webdriver instance gets updated?

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.

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.

Problem of Jmeter/Webdriver with testing a webapp searching data form which has Knockout installed

I am facing problem of testing jmeter/Webdriver for a webapp which has Knockout framework installed. I fill all the form fields OK but when run click search button it displays the error that required fields need not to be empty. I checked all web element value are not empty. As my understand Knockout is not activated to bind Web elements with DataModel from Knockout. I used wait WDS.browser.manage().timeouts().implicitlyWait for webpage fully loaded/ajax calling or event being fired but without any success. I Do you have any experiencing and solution for testing this kinds of webapp?
thanks
It might be the case you need to trigger a certain JavaScript event in order to "tell" the web application that you finish the input, i.e. onkeyup or onblur which is not being automatically called by Selenium's sendKeys() function
You can use i.e. WDS.browser.executeScript() function to send the event(s) which indicate that you filled in the form(s). Check out The WebDriver Sampler: Your Top 10 Questions Answered article to learn how to call scripts, use Waits, etc.

What is the proper way to test mandatory field in selenium

I am testing my web application using Selenium. All the form validation in the web application is done by HTML5 and some JS(for safari browser). I want to know what is the proper way to test the form validation.
Currently I am using one approach, i.e Before I filled up a mandatory field I clicked on the submit button. If the page is not refreshed then I assume the form validation working correctly. But I think there should be a better approach to do it. I am unable to find any proper solution. Any suggestion will be highly appreciated.
I also go through this link. But it is not working for me because it is not enough to have required attribute (eg required attribute does not work in older Safari browser).
Rather than checking if the page is refreshed or not, you should instead expect that it is not and that a certain error message or field highlighting or something is applied to the current page. When in an error state, the input fields probably get given an extra class, or an error div/span might be added to the DOM, try checking for that sort of thing

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).