2015-11-13 89 views
0

我是新的使用硒,我試圖測試一個網頁。這個頁面有iframe和閱讀我需要做一個開關,我做的開關是: wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame)); 但它不起作用,也許有人應該幫助我?這是我的代碼。這個代碼我的網頁:切換到在硒的iframe java

public class MainPage extends BasePageObject{ 
    @FindBy(id = "cc-sa-item-logo") 
    WebElement iconLogin; 
    private By frame = By.xpath("//iframe[@id='cms']"); 

    public MainPage() { 
     PageFactory.initElements(driver, this); 
     waitUntilPageObjectIsLoaded(); 
    } 

    @Override 
    public void waitUntilPageObjectIsLoaded() { 
     wait.until(ExpectedConditions.visibilityOf(iconLogin)); 
    } 

    public boolean logOutExists() { 
     boolean res = true; 
     String parentWindow = driver.getWindowHandle(); 
     try{   wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame)); 
      res = iconLogin.isDisplayed(); 
     }finally { 
      driver.switchTo().window(parentWindow); 
     } 
     return res; 
    } 

,這是頁面的代碼測試:

<iframe id="cms" class="cms" src="/app/s9cea6fcedc36dd82/p430c9b54081c966d/" onload="this.loaded = true;" name="cms"> 
<!DOCTYPE html> 
<html lang="es-ES"> 
<head> 
<body id="page-2281940529" class="body cc-page cc-page-index cc-indexpage j-m-flash-styles j-m-gallery-styles j-m-video-styles j-m-hr-styles j-m-header-styles j-m-text-styles j-m-emotionheader-styles j-m-htmlCode-styles j-m-rss-styles j-m-form-styles-disabled j-m-table-styles j-m-textWithImage-styles j-m-downloadDocument-styles j-m-imageSubtitle-styles j-m-flickr-styles j-m-googlemaps-styles j-m-blogSelection-styles-disabled j-m-comment-styles j-m-jimdo-styles j-m-profile-styles j-m-guestbook-styles j-m-promotion-styles j-m-twitter-styles j-m-hgrid-styles j-m-shoppingcart-styles j-m-catalog-styles j-m-product-styles-disabled j-m-facebook-styles j-m-sharebuttons-styles j-m-externalSource-styles j-m-formnew-styles-disabled j-m-callToAction-styles j-m-turbo-styles j-m-spacing-styles j-m-googleplus-styles j-m-dummy-styles j-footer-styles cc-content-parent" style="background-position: -186px 0px;"> 
<div class="cc-bs cc-alert-container" ng-controller="cms.common.AlertCtrl"> </div> 
<div id="cc-eh" data-display="cms-only"> 
<div id="cc-nav" class="cc-jimdo ui-draggable" data-display="cms-only"> 
<script> 
<div id="cc-clipboard" class="cc-clipboard-scrolled-left cc-clipboard-scrolled-right cc-clipboard-empty" data-display="cms-only" style="right: 186px; display: block;"> 
<div id="cc-inner" class="cc-content-parent" style="padding-right: 186px; padding-top: 0px;"> 
<div id="cc-sa" data-display="cms-only" style="display: block;"> 
<div id="cc-sa-sidebar" ng-controller="cms.sa.SidebarCtrl"> 
<div class="cc-sa-sidebar-group cc-sa-sidebar-group--top"> 
<div class="cc-sa-sidebar-logo-open clear"> 
<span id="cc-sa-item-logo" class="cc-sa-sidebar-logo" data-params="/app/siteadmin/upgrade/index/,true" data-action="sidebarItem"> </span> 

我需要找到span標籤

回答

0

這裏的問題的要素是第一件事情在初始化FindBy註釋字段後,您確實是waitUntilPageObjectIsLoaded,它隱含地調用driver.findElement(By.id("cc-sa-item-logo"))。 在這裏它失敗了,因爲在這一點上你還沒有切換到框架,不幸的是PageFactory無法處理它。

基本上你需要用switchTo().frame/switchTo().defaultContent()來包裝每一個對FindBy註釋字段的調用。就像你在logOutExists中做的一樣。

爲了方便起見,我將在一個單獨的頁面對象(類)中對iframe建模,以便來回封裝(並自動化)切換。

public class CmsIframe extends BasePageObject implements AutoCloseable { 

    @FindBy(id = "cc-sa-item-logo") 
    WebElement iconLogin; 

    private By frame = By.xpath("//iframe[@id='cms']"); 

    public CmsIframe() { 
     wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame)); 
     PageFactory.initElements(driver, this); 
     waitUntilPageObjectIsLoaded(); 
    } 

    @Override 
    public void waitUntilPageObjectIsLoaded() { 
     wait.until(ExpectedConditions.visibilityOf(iconLogin)); 
    } 

    public boolean logOutExists() { 
     return iconLogin.isDisplayed(); 
    } 

    @Override 
    public void close() { 
     driver.switchTo().defaultContent(); 
    } 

} 

的MainPage然後可重新寫爲如下:

public class MainPage extends BasePageObject{ 
    private By frame = By.xpath("//iframe[@id='cms']"); 

    public MainPage() { 
     PageFactory.initElements(driver, this); 
    } 


    public boolean logOutExists() { 
     try (CmsIframe cmsIframe = new CmsIframe()) { 
      return cmsIframe.logOutExists(); 
     } // when exiting this try block, the driver will automatically be switched back out of the iframe (as part of CmsIframe#close) 
    } 
} 

請注意,由於implements AutoCloseable,CmsIframe可以以try-with-resources Statement(Java> 7只),其中各種使用自動化的切換回默認的內容。 PS:如果你可以發佈堆棧跟蹤,但你得到的例外,這將有助於你。

+0

我試過實施解決方案,建議但結果是一樣的。消息錯誤是:元素信息:{使用= id,值=註銷} at sun.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java。 lang.reflect.Constructor.newInstance(Constructor.java:422) –

0

我終於解決了我的問題,你有理由的問題是,我發現在不正確的地方開關。解決方案是:

@Override 
    public void waitUntilPageObjectIsLoaded() { 
     String parentWindow = driver.getWindowHandle(); 
     try{ 
      wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame)); 
      wait.until(ExpectedConditions.visibilityOf(iconLogin)); 
     }finally { 
      driver.switchTo().window(parentWindow); 
     } 
    } 

    public boolean logOutExists() { 
     boolean res = true; 
     String parentWindow = driver.getWindowHandle(); 
     try{ 
      wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame)); 
      res = iconLogin.isDisplayed(); 
     }finally { 
      driver.switchTo().window(parentWindow); 
     } 
     return res; 
    }