Click on element if exists - uiveri5

I want to use setInterval and to try for five times to check if element exists (a button). If it exists, I want to click it and to proceed after it and, if not, I want to just proceed.
How to do so?

This is not the correct way with webdriver that is the underlying API in UIVeri5. The right way is to use the browser.driver.wait() with a predicate that checks for the condition.
But such polling should not be necessary at all with UIVeri5 when working against a UI5 application because UIVeri5 handles the waiting automatically. With the limitation that this magic works only in UI5 apps because it depends on the UI5 runtime to detect when the UI5 app is ready. So when writing tests for UI5, you don't need any pollings, you just write element(by.control()).isDisplayed() and it works fine and perfectly reliable. The check will happen at the correct time - when ui5 app is fully rendered and no polling is necessary.

Based on #Max, I am just writing the answer in a more dummy proof way:
If one needs to check if a button or something else is displayed, isDisplayed() should be called. isDisplayed() is a promise and, if the element is displayed, then in the then() resolution the continuation should be called. If the element is not displayed, the callback should be called in the catch() resolution of the promise.
Example:
iCheckForElementAndContinue: function(callback) {
element.isDisplayed()
.then({
// Element is found
if (callback) {
callback();
}
})
.catch({
// Element is not found
if (callback) {
callback();
}
});
}

Related

How to give Fixed wait in playwright With out any condition Like we had in cypress : Cy.wait(600)

How to give Fixed(Implicit Wait) wait in playwright
With out any condition
Like we had in cypress :
Cy.wait(600);
Thank You
Like we had in cypress :
Cy.wait(600);
To give a fixed wait in Playwright without using any conditions, you can use the page.waitForTimeout method. This method will pause the script for a specified amount of time before continuing.
await page.waitForTimeout(2000); // waits for 2 seconds
You can also use the page.waitForFunction method with a function that returns true after a certain amount of time. For example:
await page.waitForFunction(() => {
const now = Date.now();
return now - start > 2000;
});
This will wait for 2 seconds before returning true.
Note that these methods should be used sparingly as they can cause delays in the script execution. It is generally better to use specific conditions to determine when to continue, such as waiting for a specific element to be present or for a page to load.
This is 5 sec timeout
await new Promise(resolve => setTimeout(resolve, 5000));
Avoid using hard waits with playwright!
Issue with Hard waits:
Hard waits do only one thing , they simply wait for specified time without considering any application actual response time which is very rigid approach.
See the below example:
await page.waitFor(5000); // hard wait for 5000ms
This will wait for 5 seconds regardless of actual response time which may be less or more than 5 seconds which is bad in both cases as in first case it has to wait unnecessary for longer time where object is already loaded and in second scenario it will fail after waiting unnecessary for 5 seconds if object is not going to load at all in failing scenario.
Recommended Approach
In-built waits
Playwright has in-built waiting mechanism on navigation and page interactions. As they are part of playwright itself , it is better to use them and customize it if required.
Explicit waits
There is another way to handle waits explicitly only in scenarios where auto-waiting is not a sufficient or efficient approach to handle depending on specific scenarios but in general it should be avoided if auto-waiting is enough for the requirement.
Waiting for page navigations and API responses
1)page.waitForNavigation to wait until a page navigation (new URL or page reload) has completed.
2)page.waitForLoadState This waits until the required load state has been achieved.
3)page.waitForURL This waits until a navigation to the target URL.
4)page.waitForResponse waits till the response from the given API is received.
Wait for element
In lazy-loaded pages, it can be useful to wait until an element is visible with locator.waitFor(). Alternatively, page interactions like page.click() auto-wait for elements.
// Navigate and wait for element
await page.goto('https://example.com');
await page.getByText('Example Domain').waitFor();
// Navigate and click element
// Click will auto-wait for the element
await page.goto('https://example.com');
await page.getByText('Example Domain').click();
Waiting on page events
We can also directly wait on page events using page.waitForEvent.
Customized Wait Function
At last we can also write (if really needed) customized wait functions using WaitForFunction for very specific scenarios or specific applications or controls.

How does testcafe decide an iframe has loaded?

I'm currently on the latest package 1.18.4 and having issues switching to an iframe during test execution.
Each time t.switchToIframe() is called, the following error is displayed:
Content of the iframe in which the test is currently operating did not load.
This happens even though i can clearly see the iframe has loaded, even placing a length wait of 60 seconds when landing on the page for testing purposes.
This rough code below should be runnable and reproduce the problem:
import { Selector, t } from "testcafe";
fixture(`Quick Test`);
test
.page(`https://www.terrific.ie/dealer/bradys-of-castleknock/finance/45784/13295`)(
`Quick Test`,
async () => {
await t
.click(`.submit-outer`)
.expect(Selector(`.car-summary-box`).visible)
.ok()
.expect(Selector(`.cw-finance-plugin.plugin-loaded`).visible)
.ok()
.switchToIframe(`.cw-finance-plugin.plugin-loaded`)
.click(`.cw-checkmark`)
.debug();
});
In general, there are two steps to switch to iframe. The first step is getting a selector with an iframe, but if the selector doesn't exist, you will get another error. The next step is getting contentWindow with a native getter. The error probably occurs on this step, but I can't reproduce this case with your iframe example. Could you share a full test example that illustrates this error?
Also, you put the content of the iframe between the tags, but it doesn't work like this. You need to set the path to the document in the src attribute of the iframe.

Is it possible to cancel all running JS functions when destroying a Vue component?

I'm wondering if it's possible to cancel all running async functions belonging to a component when destroying that component. I have a View-component which fetches a lot of data when the user enters that page, the problem is if the user goes to another page before the data is fully loaded, then the request-queue-limit is reached and slows down the data-loading for the new page.
What I did to fix this was to just check the lifecycle-status of the component instance. I found out that it was possible to check this._isBeingDestroyed
and this._isDestroyed, and then do an early return.
async fetchDataFunction() {
if (this._isBeingDestroyed || this._isDestroyed) return;
// Code
}

Cycle.js/xstream click event streamed once, but fired twice

There's a single button element on the page and the following click stream:
let submitClick$ = sources.DOM.select(buttonSel)
.events("click")
.mapTo(true)
.debug(console.log)
Once I click on the button, true is logged, which is correct.
However, when I map the stream, the code inside runs twice:
let submitDeal$ = submitClick$.map(() => {
console.log("Clicked")
// ...
})
No other event handlers should be attached to the button, and the element itself sits inside a div:
button(".btn--add", "Submit")
The usual event.preventDefault() and event.stopPropagation() doesn't make a difference, and inspecting the event does show that it is fired from the same element button.btn--add.
Not really sure what's going on, any ideas are appreciated! Thanks!
Versions:
"#cycle/dom": "^12.2.5"
"#cycle/http": "^11.0.1"
"#cycle/xstream-run": "^3.1.0"
"xstream": "^6.4.0"
Update 1: I triple checked and no JS files are loaded twice. I'm using Webpack that bundles a single app.js file that's loaded on the page (Elixir/Phoenix app). Also when inspecting the button in the Event Listeners tab in Chrome's Developer Tools, it seems that only 1 event handled is attached.
Update 2: Gist with the code
Too little information is given to resolve this problem. However some things come to mind:
You shouldn't use .debug(console.log) but .debug(x => console.log(x)) instead. In fact .debug() is enough, it will use console.log internally.
Then, is the button inside a <form>? That may be affecting the events. In general this question needs more details.
Turns out this was due to a bug in xstream, which was fixed in xstream#7.0.0.

Selenium: Ajax Testing

Please brief me about the Ajax testing with selenium RC.
As in Ajax element not reflect on the view-source, but using firebug we can see the changes in HTML source code.
There are two methods associated with it Ajax testing..
1-The method "waitForCondition (java.lang.String script, java.lang.String timeout), to provide script we have to create a java script by own or it should be the same Ajax script/java script present on the web page.
Please correct me if i am wrong on below point..
2-The method "waitForElemantPresent(Locator)", We check the Element in the firebug and check the same in this method is self waitForElemantPresent(Locator).
Let me know if anything else I am missing testing Ajax application.
I got help on this from one article and with help of #Hannibal
http://agilesoftwaretesting.com/?p=111
JQuery: “jQuery.active”
Prototype: “Ajax.activeRequestCount”
Dojo: “dojo.io.XMLHTTPTransport.inFlight.length”
So if there is Ajax call we can use second option.
selenium.waitForCondition(
"selenium.browserbot.getCurrentWindow().jQuery.active == 0",
timeout);
To answer your first point, yes waitForCondition(javascript,timeout) will run the javascript till it returns a true value OR when timeout happens. You should take a look at the api documentation for this as you need to use browserbot to run the script in your application window. Link to API documentation is here
In Selenium 1, one way by which you can handle the Ajax conditions are by creating custom functions which will wait till the condition is met or till a timeout happens. While a normal Selenium.isElementPresent will fail immediately if the element is not present, your custom function will wait for some more time (the time for Ajax to load) before it fails. As an example you can refer the following custom method for isElementPresent. This is written in JAVA, you should be able to use the same logic in the programming language that you use.
public boolean AjaxIsElementPresent(String elementToLookFor, int timeOutValueInSeconds){
int timer=1;
while(timer<=timeOutValue){
if(selenium.isElementPresent(locator))
return true;
else
Thread.sleep(1000);
}
return false;
}
This method will return a boolean false if the element is not present even after the specified timeoutValue. If it finds the element within the timeoutvalue then it returns a true.
I have seen some in built functions for AjaxCondition handling in Selenium 2. But I have not used it. You can refer to Selenium 2 code base