2

I am a beginner for Selenium Webdriver, I have written a script in Java to test a functionality, and it is working fine. Sometimes I faces an issue.

Suppose I just click on create button to create something (let suppose customer) and after this I need to do some work with a screen which comes after create successfully customer. Sometimes due to slow response from server, my script get failed due to search a DOM element which comes after create customer.

If response come with in predefined time in my code, no issue, if not come then script failed (it search a element which has not rendered yet).

1) click on button

try{
                    // let suppose creatButtonElement is the web element of Create Button.
                    createButtonElement.click();
                }catch(Exception e){
                    throw new Exception("Unable To Click on element [ " + element + " ] , plz see screenshot [ UnableToClick_" + element);
                }

Expecting: after click on create button my script is expecting success message for assertion.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ghanshyam
  • 184
  • 8

2 Answers2

2

I had faced this issue once but I had handled this manually. This will work if after click button loader is appearing. It waits for one minute. Please use the following code to wait manually to get response of server. It check the visibility of loader to know the response each second.

public static void loading_wait() throws Throwable {
            int i = 0;
            int maxloopDependOnBrowser = 60;
            int totalSecond=0;
            boolean loadinImageTakingMuchTime=false;
            try{
                  while (true) {
                        i++;
                        if( !(loaderDisplayed(APP_LoadingImage_xpath)) ){
                              totalSecond+=i;
                              break;
                        } 

                        if (i > maxloopDependOnBrowser) {
                              totalSecond=maxloopDependOnBrowser + 1;
                              loadinImageTakingMuchTime=true;
                              break;
                        } else {
                              totalSecond=i;
                              Thread.sleep(1000);
                        }
                  }
                  Thread.sleep(1000);

                  if(loadinImageTakingMuchTime){
                        throw new Throwable();
                  }

            }catch (Throwable t) {
                  throw new Exception("FAILED:>>> Loading image is taking too much time :>>"+Throwables.getStackTraceAsString(t));

            }
      }

XpathKey :- to find the loader element

public static boolean loaderDisplayed(String XpathKey) {
            int i = 0;
            try {
                  driver.manage().timeouts().implicitlyWait(5, TimeUnit.MILLISECONDS);    
                  List<WebElement> elementList = getORObject_list(XpathKey/*, 0, 500*/);
                  for (Iterator<WebElement> iterator = elementList.iterator(); iterator.hasNext();) {
                        WebElement webElement = (WebElement) iterator.next();
                        if (webElement.isDisplayed()) {
                              i = 1;
                              break;
                        }
                  }

            } catch (Throwable t) {
            }

            finally {
                driver.manage().timeouts().implicitlyWait(Long.parseLong(1), TimeUnit.SECONDS);
            }
            if (i == 0) {
                  return 
            } else {
                  return true;
            }

      }
Sachin Kumar
  • 1,055
  • 9
  • 15
1

You can join waiting

WebDriverWait wait = new NWebDriverWait(driver, 10); //10 second

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("btn1")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("btn1")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("btn1")));
Ruyut
  • 151
  • 11