簡體   English   中英

如何處理 Selenium Webdriver 中的服務器響應時間導致的腳本失敗?

[英]How to handle script failure due to server response time in Selenium Webdriver?

我是 Selenium Webdriver 的初學者,我在 Java 中編寫了一個腳本來測試一個功能,它工作正常。 有時我會遇到問題。

假設我只是單擊創建按鈕來創建一些東西(假設是客戶),然后我需要對成功創建客戶后出現的屏幕做一些工作。 有時由於服務器響應緩慢,我的腳本由於搜索創建客戶后出現的 DOM 元素而失敗。

如果響應在我的代碼中的預定義時間出現,則沒有問題,如果沒有出現則腳本失敗(它搜索尚未呈現的元素)。

1) 點擊按鈕

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);
                }

期待:點擊創建按鈕后,我的腳本期待斷言成功消息。

我曾經遇到過這個問題,但我是手動處理的。 如果單擊按鈕加載程序出現后,這將起作用。 它等待一分鍾。 請使用以下代碼手動等待以獲得服務器的響應。 它檢查加載程序的可見性以了解每秒的響應。

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:- 查找加載器元素

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;
            }

      }

你可以加入等待

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")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM