How to wait dynamically until the progress bar to load completely in Selenium WebDriver?

6,006 views
Skip to first unread message

Sudhansu Sekhar panda

unread,
Jan 3, 2014, 5:04:48 AM1/3/14
to seleniu...@googlegroups.com
Hi All,

While automating one AJAX application i came across a the below problem. 

Scenario: I clicked on an element and the progress bar keeps on loading..I want to wait for the progress bar to stop. How to go ahead this type AJAX elements in Selenium Webdriver using java as the language. 

NOTE: When the progress bar is loading, in the background the element is PRESENT..otherwise i would have been used the wait for an element until the element to appear.

Can anyone please help me for the same?

Thanks,

Sudhansu

Abhay Rai

unread,
Jan 3, 2014, 12:20:29 PM1/3/14
to seleniu...@googlegroups.com
Hi,
   You can use jquery to solve this problem.
below is the code

public boolean waitForJQueryProcessing(WebDriver driver,
            int timeOutInSeconds) {
        boolean jQcondition = false;
        try {
            driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); // nullify
                                                                            // implicitlyWait()
            new WebDriverWait(driver, timeOutInSeconds) {
            }.until(new ExpectedCondition<Boolean>() {
 
                @Override
                public Boolean apply(WebDriver driverObject) {
                    return (Boolean) ((JavascriptExecutor) driverObject)
                            .executeScript("return jQuery.active == 0");
 
                }
            });
            jQcondition = (Boolean) ((JavascriptExecutor) driver)
                    .executeScript("return jQuery.active == 0");
            System.out.println(jQcondition);
            driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); // reset
                                                                                // implicitlyWait
            return jQcondition;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jQcondition;

David Lai

unread,
Jan 3, 2014, 12:43:33 PM1/3/14
to seleniu...@googlegroups.com
I think you can get a reference to the loading progress bar.  Then you can put a try/catch poll on it.  When the element turns stale, is when it goes off the screen.

waitForProgressBarToDisappear() {
  WebElement progressBar = ...;
  while(true) {
    try {
       if (progressBar.isDisplayed())
          return True;
    } catch (StaleElementException) {
       return true;
    }
   sleep(1000);
  }
}

another approach if your web app uses jQuery.  jQuery has different queues you can poll on.  There's an animations queue, if you want to poll if the progress bar is still moving, and there's and Ajax queue, if you want to check how many outstanding requests there are waiting to return.  Both are very helpful for working with responsive single page Ajax applications.

Sudhansu Sekhar panda

unread,
Jan 4, 2014, 2:24:18 AM1/4/14
to seleniu...@googlegroups.com
Hi David,

How to get the locator of the progress bar so that i can replace in place of "..."..Can you please help me?

Thanks,
Sudhansu

Oscar Rieken

unread,
Jan 4, 2014, 2:30:09 AM1/4/14
to seleniu...@googlegroups.com
right click inspect element. I think you should take some time and learn your tools.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/978edf9d-b9ca-4a22-9661-5d518335587f%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

keerthi sree

unread,
Jan 3, 2014, 1:14:04 PM1/3/14
to seleniu...@googlegroups.com
hi...
i hv installed selenium....im trying to open mozilla firefox browser using selenium...but im getting class
not found exception.....plz help me


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Sachin Mashalkar

unread,
May 9, 2017, 12:37:52 PM5/9/17
to Selenium Users
Use this below class. It worked for me perfectly on Chrome.

public class ChromeDownloadsPage {
@FindBy(xpath = "//html/body/downloads-manager")
private WebElement downloadsManager;
protected WebDriver driver;
public ChromeDownloadsPage(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver =driver;
}

public List<ChromeObjects> getChromeDownloadDetails() {
List<ChromeObjects> chromeDetailsList = new ArrayList<>();
for(WebElement e : getDownloadItems()) {
WebElement downloadShadowItemElement = CommonFunctions.expandShadowRootElement(e, driver);
//Wait until download is completed
CommonFunctions.waitForElementToBeDisappear(driver, downloadShadowItemElement.findElements(By.className("show-progress")));

WebElement downloadContent = downloadShadowItemElement.findElement(By.id("content"));
WebElement downloadDetails = downloadContent.findElement(By.id("details"));
WebElement downloadArea =  downloadDetails.findElement(By.id("title-area"));

//Download File
WebElement downloadFileElement = downloadArea.findElement(By.id("file-link"));
//File Download Status
WebElement fileDownloadStatusElement = downloadArea.findElement(By.id("tag"));
//Assigning the File name and Status
ChromeObjects obj = new ChromeObjects();
obj.setDownloadFileName(downloadFileElement.getText());
String tempStatus = CommonFunctions.waitForElement(driver, fileDownloadStatusElement).getText();
if(tempStatus.equalsIgnoreCase("")) {
obj.setDownloadStatus("Completed");
} else {
obj.setDownloadStatus(tempStatus);
}
chromeDetailsList.add(obj);
}
return chromeDetailsList;
}
//Get Shadow DOM WebElement of Download Item 
private List<WebElement> getDownloadItems() {
WebElement downloadsManagerShadowElement = CommonFunctions.expandShadowRootElement(downloadsManager, driver);
WebElement downloadListManager = downloadsManagerShadowElement.findElement(By.id("downloads-list"));
return downloadListManager.findElements(By.tagName("downloads-item"));
}
public ChromeDownloadsPage openDownloadsPage() {
//Open New Tab
openNewTab();
//Switching to New Tab
CommonFunctions.switchToNextWindow(driver);
//Hitting Download URL
openUrl();
moveToDownloads();
return new ChromeDownloadsPage(driver);
}
private ChromeDownloadsPage openUrl() {
driver.navigate().to(GlobalThings.downloadUrl);
return new ChromeDownloadsPage(driver);
}
private void openNewTab() {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
} catch (AWTException e) {
e.printStackTrace();
}
}
private void moveToDownloads() {
Robot r;
try {
for (int i = 0; i < 4; i++) {
r = new Robot();
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
}
} catch (AWTException e) {
e.printStackTrace();
}
}
public class ChromeObjects {
private String downloadFileName;
private String downloadStatus;
public String getDownloadFileName() {
return downloadFileName;
}
private void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public String getDownloadStatus() {
return downloadStatus;
}
private void setDownloadStatus(String downloadStatus) {
this.downloadStatus = downloadStatus;
}
}
}

------------------------------------------------------------------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages