2017-07-28 88 views
2

目標:警報彈出。無論是否顯示,我希望它繼續。如果顯示,則必須選中複選框,然後按繼續。如果不是,則忽略。硒的Java處理對象(警報對話框)異常而不會延遲。 (預見的Java彈出)

攔截器:如果警報顯示,它將處理該操作並且對話框將被關閉。但是當硒沒有顯示時,硒懸掛在那裏,沒有處理條件。

背景:我之前使用UFT,也許我的邏輯可能是錯誤的。 彈出的應用程序的警報(不繫統),所以假定「切換到(),接受()/駁回()將無法工作。我將添加手柄警報登錄之後,和下方的登錄方法中。

硒框架背景:我們使用硒行家框架,平靜BDD對象被設置在頁面的開頭和serenity.properties處理超時默認等

。彈出對象(如果出現的話):

@FindBy(xpath = "//input[@id='notification-ack']") 
private WebElement PcoNoticeChbx; //this is a check box, needs to be checked 
@FindBy(xpath = "//button[contains(.,'Continue')]") 
private WebElement PcoNoticeContinueBtn;//button to close the alert 

*登錄方法*

public void loginIntoExtApplication(String baseURL, String loginURL, String uId, String pwd, String extAppEnv)throws Exception { 
      gotoExtLoginPage(baseURL, loginURL);   
      enterLoginCredential(uId, pwd); 
    openAt("https://" + extAppEnv + ".programportaltest.hrsa.gov/sdms- 
    extranet/index.xhtml"); 

我研究:

// 1。

 if (PcoNoticeChbx!=null) { 
      PcoNoticeChbx.click(); 
      PcoNoticeContinueBtn.click(); 
     } else { 
      System.out.println("Dialog didn't display, no need any action"); 
     } 

// 2。登錄後在此處掛起。

 if(!getDriver().findElements(By.xpath("//*[@id='submit']")).isEmpty()){ 
       PcoNoticeChbx.click(); 
       PcoNoticeContinueBtn.click(); 
      } 
      else { 
       System.out.println("Dialog didn't display, no need any action"); 
      } 

// 3。 添加到監視不工作,它顯示掛起,下面的代碼也失敗了。我在Junit中運行我的Maven作爲調試模式。它用於正常工作。但手錶的元素始終顯示(待定)..

 boolean isPresent = getDriver().findElements(By.id("noticebox")).size() >0 
     System.out.println("the diaolog exist= " + isPresent); 

// 4。甚至嘗試了try catch方法。

try{  
      PcoNoticeChbx.click(); 
      PcoNoticeContinueBtn.click(); 
     }catch (Exception e){ 
      // Printing logs for my report 
      Log.error("Report Category button element is not found."); 

      // After doing my work, now i want to stop my test case 
      throw(e); 
     } 
    return; 
    } 

// 5。試圖列表webelemets:

列表臨時= webdriver.findElements(org.openqa.selenium.By.id( 「noticebox」));

if (temp.Count > 0 && temp[0].Displayed) { 
        // script to execute if element is found 
       } else { 
        // continue the test 
     } 

// 6。並低於

if (!WebDriver.findElements(By.xpath("//*[@id='submit']")).isEmpty()==true); 


     { 
             //handle the dialog 
         } 
      else{ 
       //continue 
      } 

// 7。試圖用布爾值,而且還掛在這裏的第一個步驟

boolean Nbox = PcoNoticeChbx.isDisplayed(); { 




    if (Nbox==false) 
       { 
      System.out.println("Dialog didn't display, no need any action"); 
       } 
      else if (Nbox==true) { 
      PcoNoticeChbx.click() ; 
      PcoNoticeContinueBtn.click(); 
      } 
+0

當使用try catch代碼時,它採用了:元素不可點擊。 –

+0

public void checkAlert(){ \t try { \t WebDriverWait wait = new WebDriverWait(getDriver(),2); \t System.out.println(「TRYing steps」); \t wait.until(ExpectedConditions.alertIsPresent()); \t org.openqa.selenium.Alert alert = getDriver()。switchTo()。alert(); \t alert.accept(); \t \t // \t}趕上(例外五){ \t //異常處理 \t \t System.out的。println(「catch exception steps」); \t} \t \t} \t } –

回答

0

如果是這樣我已經處理了彈出窗口與他們的工作是這樣的:

  1. 客戶來到網站,該網站檢查是否存在cookie。如果該cookie存在,彈出窗口永遠不會啓動(理想狀態)。如果該cookie不存在(典型狀態),則在指定的時間段之後出現彈出窗口。
  2. 客戶駁回了彈出式窗口,一個cookie與到期時間
  3. 一旦到期時間的推移,Cookie過期和彈出窗口將再次觸發

你需要做一些調查,發現該cookie創建這是在彈出窗口被解除後創建的。清除緩存,瀏覽到該網站,並記下所有存在的cookie。等待彈出窗口並將其解除。現在找到剛創建的cookie並檢查它。這是您需要創建的cookie。有很多關於創建cookie的教程。這非常簡單。

一旦你學會了如何創建的cookie,你將它添加到你的腳本如下所述:

  1. 瀏覽一些網頁上,你知道不存在的域名,例如www.domain.com/some404page。我們這樣做是因爲它不會觸發彈出倒計時,我們需要在域上創建cookie。
  2. 創建的cookie
  3. 做你的正常測試

沒有更多的彈出窗口。

+0

嗨,JeffC。這對我來說實際上是一個新想法。它將適用於我的一些與您的測試類似的測試。 –

+0

加上這不是基於cookie的警告,而是DB。 –

0

解決方案找到我的情況。

這可能很容易。但需要一些時間來研究。希望它會幫助你。

使用了很多方法後,爲這個javascripts確認提醒。我用下面的方法。所有的.CurrentlyVisible()方法的幫助。因爲這一個,我猜只有這個會給你結果,即使當元素不存在或爲空時。

if (element(NoticeContinueBtn).**isCurrentlyVisible**()==true) { 

    PcoNoticeChbx.click(); 
    PcoNoticeContinueBtn.click(); 
    //else just continue 
}