How to wait until all loaders disappears using Selenium WebDriver through Java - selenium-webdriver

I am using selenium web driver and I need to wait until all loaders disappear. I have 12 widgets on dashboard page and I need to wait until all widgets are loaded. Loader shows on each widget. I have used both following ways but nothing works and no errors, it just passes on to next statement.
new WebDriverWait(driver,60)
.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loader')]")));
WebDriverWait wait2 = new WebDriverWait(driver,60);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.loader")));

As you are having total 12 widgets on dashboard page and you need to wait until all widgets are loaded you have to induce WebDriverWait for the invisibilityOfAllElements() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(By.cssSelector("div.loader"))));
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(By.xpath("//div[#class='loader']"))));

Related

How to Handle this type of Popups is selenium WebDriver Using Java

How to Handle this type of popups using selenium with java, tried Using getWindowHandles method but cant figure Out the Problem
It is only possible to give a generic answer to your question on how to get rid of an ad popup.
Please be aware, that you must make sure to really alway get the same ad popup and this is usually not the case with advertising.
Assuming that you always get the exact same (also the implementation) popup, you must first inspect the popup or directly the close button within the popup and find some attributes that can be used to identify the close button element.
Let‘s now also assume that the close button can be identified, you are pretty much done and only need to use selenium to select the element (the css selector is just an example) and send a click command.
element = driver.findElement(By.cssSelector("input[value='close']"));
element.click();
Pop up can be handled with window handlers
String mainwindow=driver.getWindowHandle();
// To handle all new opened window.
Set<String> s1=driver.getWindowHandles();
Iterator<String> i1=s1.iterator();
while(i1.hasNext())
{
String ChildWindow=i1.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
// Switching to Child window
driver.switchTo().window(ChildWindow);
element = driver.findElement(By.cssSelector("input[value='close']"));
element.click()
// Closing the Child Window.
driver.close();
or else we can use alert
Alert confirmationAlert = driver.switchTo().alert();
String alertText = confirmationAlert.getText();
System.out.println("Alert text is " + alertText);
confirmationAlert.accept();

How can I call a predefined Geb Page element using WebDriver?

I created a re-usable WebDriver method, but I can't for the life of me figure out how I can call a predefined Geb page CSS selector using WebDriver.
This method works, but I can't seem to call it using WebElement element:
static void WaitVisibilityOfElement(WebDriver driver, By cssSelector, int timeoutInSeconds) {
WebDriverWait Wait = new WebDriverWait(driver, timeoutInSeconds)
Wait.until(ExpectedConditions.visibilityOfElementLocated(cssSelector)).click()
}
This is the element I am trying to call, and I want to just be able to use the name of the element flipperCardOne, but if I do that it doesn't work.
flipperCardOne(wait:true) {$ (".flex-item:nth-child(5) .front > .w-100")}
Geb's selector results (the things returned from calls to $()) are called Navigators and are a wrapper around collections of Selenium's WebElements. Because Navigators can be created from more than just css selectors (you can chain multiple find() calls on them, you can base them off xpath expressions and you can filter contents of them by matching their text, just to give a number of examples) you cannot map one to one from a Navigator to a css selector. If your intention is to always wait for flipperCardOne to be visible before interacting with it then the idiomatic way to do that would be to use waitCondition content option:
flipperCardOne(wait:true, waitCondition: { it.displayed }) {$ (".flex-item:nth-child(5) .front > .w-100")}
Then you could just do flipperCardOne.click() and it would work.
And if you insist on using a method similar to what you listed in the question (and I'd argue that it's not the right thing to do) then you'd need something like:
static void WaitVisibilityOfElement(WebDriver driver, WebElement webElement, int timeoutInSeconds) {
WebDriverWait Wait = new WebDriverWait(driver, timeoutInSeconds)
Wait.until(ExpectedConditions.visibilityOf(webElement)).click()
}
and then call it like:
WaitVisibilityOfElement(browser.driver, flipperCardOne.singleElement, 5)

Application of DOM locators in Selenium

I understood DOM locators but I don't know how to apply and locate the elements using DOM locators. I automate using Selenium with java but in DOM we have to locate writing document.getElementById("id of the element"). Copying same thing in Java code gives me error. Does any Library needs to be imported to use DOM locators or something else ?
If you want to use document.getElementById function inside your Java Selenium tests you can go for JavaScriptExecutor.executeScript() method like:
WebElement element = (WebElement) driver.executeScript("return document.getElementById('id of the element');");
however much easier would be using WebDriver.findElement(By.Id)
WebElement element = driver.findElement(By.id("id of the element"));
it is less code, more clear, faster and you will be able to use Explicit Waits in case of testing AJAX applications
WebElement element = new WebDriverWait(driver,10)
.until(ExpectedConditions
.presenceOfElementLocated(By.id("id of the element")));

How to wait for page load to complete in Chrome browser

What is the command in webdriver to wait for page to load fully before performing action on the page in CHROME browser?
PageLoadTimeOut is not waiting for the page load to complete in Chrome whereas it is working fine in firefox.
In Chrome, if webelement is there on the page, webdriver is performing the action on webelement while page is getting loaded. This is causing scripts to fail randomly with exception 'element is not clickable'.
Please let me know if there is any solution for this.
Thanks
You can use custom wait methods for an element to be enable or visible on the page.
For Example create a custom method isElementEnable using following code:
driver.findElement(locator).isEnabled();
Or use the following code to check if the element is visible on page:
driver.findElement(locator).isDisplayed();
pass your element locator as input parameter to the method.
Or Alternatively use implicit wait:
driver.manage().timeouts().implicitlyWait(number_of_seconds, TimeUnit.SECONDS);
Hope this will help.
You can use the JavaScriptExecuter.executeScript to get document.readyState,to compare it to complete,as a condition to WebDriverWait.Like this:
WebDriverWait wait = new ...;
wait.until(new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver) {
return (JavaScriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
});
Unfortunately, with Chrome if you have a alert on page,you will get a UnhandledAlertException.And this will not occur for Firefox
It's a really old question, but anyone having the same question might find this useful:
For chromedriver, it is best to use sleep method:
import time
# some of your code
time.sleep(2)
# here your program will wait for two seconds
# rest of your code
You can find an appropriate time span that will ensure your page loads and pass that time as parameter. But using too big time will make program slower.

selenium firefoxdriver - wait for ajax

When testing an ajax page there is a challenge how to wait till the page is loaded.
The way i found in the web is to wait explicitly for a certain element to load.
There is another way in htmlunit, which is to convert all asynchronous javascript to synchronous javascript.
client.setAjaxController(new NicelyResynchronizingAjaxController());
This is more generic, as we don't need to know exactly for which element to wait.
Is there a way to implement this with the firefoxdriver.
This is how you could wait for a specific element that is loaded in the DOM after a Ajax Request.
Suppose you have the following:
<div id="postAjaxRequest">This only appears after something request me!</div>
The all you had to do is with a started WebDriver and WebDriverWait element is the following:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10); // Starts a wait of a maximum 10 seconds.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("postAjaxRequest")));
driver.findElement(By.id("postAjaxRequest")).click(); // or any other action
This is the simplest i could explain.

Resources