2015-10-04 73 views
0

即時通訊使用proxys連接到一個網站使用硒和測試一些東西,問題是一些proxys速度非常慢,它使事情真正低效,但另一個問題是我無法捕捉到錯誤,不管我做了什麼。試圖從其他堆棧帖子的各種建議,但無濟於事。硒超時等待頁面加載與proxys

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); 
    proxy.setHttpProxy(ip+":"+port); 
    proxy.setSslProxy(ip+":"+port); 
    proxy.setFtpProxy(ip+":"+port); 


    DesiredCapabilities dc = DesiredCapabilities.firefox(); 
    dc.setCapability(CapabilityType.PROXY, proxy); 

    WebDriver driver = new FirefoxDriver(dc); 

      try { 


       driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS); 
       driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS); 

       driver.navigate().to("http://foo.bar"); 


      } catch (Exception ex) { 
       ex.printStackTrace(); 
       driver.quit(); 
       driver = null; 
       break; 
      } 

我的超時設置爲15秒,因爲它通過幾頁導航和一些proxys的花費30-40秒只加載一個頁面,以便它會相當長的時間,有沒有實際的方式趕上錯誤

Timed out waiting for page load 

我的另一個問題是,是否會更容易使用硒的替代品?到現在爲止Iv'e曾與硒,似乎已經有一段時間了被別人判斷的幾個問題民族職位

回答

0

scriptss,

它看起來像你對我有需要ExplicitWaits來驗證你的導航。取決於您的環境,ExplicitWaits不是在驅動程序中使用隱式配置,而是允許更靈活的解決方案。根據我的經驗,WebDriverWait類與ExpectedConditions結合解決了我的大部分問題。我還沒有進入驅動程序的代理支持,但它似乎與我已獲得的信息一致。

這就是說,我不完全確定你爲什麼沒有看到拋出異常。當我遇到類似的問題時,我會將WebDriver包裝在EventFiringWebDriver中,並查看在這些情況下WebDriverEventListener的onException消息是否被擊中。

在下面的示例中,您需要填寫代理IP和端口的常量以及要轉到的實際URL。在createParameters方法中,您還需要更新標題和部分url的字符串以適合您的用例。

內置了Java 8和硒v 2.47.2

import java.util.List; 
import java.util.concurrent.TimeUnit; 
import java.util.logging.Level; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 
import org.junit.runners.Parameterized.Parameters; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.support.events.AbstractWebDriverEventListener; 
import org.openqa.selenium.support.events.EventFiringWebDriver; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

import com.google.common.collect.Lists; 

@RunWith(Parameterized.class) 
public class ExplicitWaitNavigationTest { 
    /** IP configuration for the proxy.*/ 
    private static final String PROXY_IP = ""; 
    /** Port configuration for the proxy.*/ 
    private static final int PROXY_PORT = 8080; 

    /** The url the test will navigate to.*/ 
    private static final String TEST_URL = "http://the-internet.herokuapp.com/dropdown"; 
    /** Maximum time to wait for a page to load.*/ 
    private static final int PAGE_LOAD_SECONDS = 15; 
    /** Maximum time to wait for javascript to complete.*/ 
    private static final int SCRIPT_LOAD_SECONDS = 15; 

    /** ExpectedCondition used to validate the navigation state.*/ 
    private ExpectedCondition<Boolean> navTestCondition; 
    /** The URL to be tested in this instance.*/ 
    private String url; 
    /** WebDriver reference used for testing navigation through proxy.*/ 
    private WebDriver webDriver; 

    /** 
    * Assembles the Constructor arguments for testing. 
    * Due to test parameterization, each pair is run as a separate test. 
    * @return Object array of test parameters. 
    */ 
    @Parameters 
    public static Object[] createParameters() { 
     List<Object[]> params = Lists.newArrayList(); 
     String url = TEST_URL; 

     //Test that url exactly matches 
     ExpectedCondition<Boolean> test = ExpectedConditions.urlToBe(url); 
     params.add(new Object[]{test, url}); 

     //Test if the url matches a regex? 
     test = ExpectedConditions.urlMatches(url); //regex match? I'm not great with regex 
     params.add(new Object[]{test, url}); 

     //Test if the url contains the base site, or other fragment information 
     test = ExpectedConditions.urlContains("dropdown"); 
     params.add(new Object[]{test, url}); 

     // Test if the page title exactly matches expectation. 
     test = ExpectedConditions.titleIs("The Internet"); 
     params.add(new Object[]{test, url}); 

     //Test if the page title somewhat matches expectation 
     test = ExpectedConditions.titleContains("Internet"); 
     params.add(new Object[]{test, url}); 


     return params.toArray(new Object[]{});   
    } 

    /** 
    * Turns off noisy logging that is on by default in the Selenium structure. 
    */ 
    @BeforeClass 
    public static void disableHttpUnitOutput() { 
     java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
     System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 
    } 

    /** 
    * Constructor. 
    * @param waitCondition Expected condition to validate in this effort. 
    * @param url URL being navigated to. 
    */ 
    public ExplicitWaitNavigationTest(ExpectedCondition<Boolean> waitCondition, String url) { 
     this.navTestCondition = waitCondition; 
     this.url = url; 
    } 

    /** 
    * Configures the Driver for this test instance. 
    */ 
    @Before 
    public void setupDriver() { 
     org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); 
     String proxyConfig = PROXY_IP + ":" + PROXY_PORT; 
     proxy.setHttpProxy(proxyConfig); 
     proxy.setSslProxy(proxyConfig); 
     proxy.setFtpProxy(proxyConfig); 


     DesiredCapabilities dc = DesiredCapabilities.firefox(); 
     dc.setCapability(CapabilityType.PROXY, proxy); 

     FirefoxDriver ffd = new FirefoxDriver(dc); 
     ffd.manage().timeouts().pageLoadTimeout(PAGE_LOAD_SECONDS, TimeUnit.SECONDS); 
     ffd.manage().timeouts().setScriptTimeout(SCRIPT_LOAD_SECONDS, TimeUnit.SECONDS); 

     //XXX EventFiringWebDriver decoration 
     EventFiringWebDriver efwd = new EventFiringWebDriver(ffd); 
     efwd.register(new AbstractWebDriverEventListener() {    
      @Override 
      public void onException(Throwable throwable, WebDriver driver) { 
       System.out.println("Exception thrown from within the EventFiringWebDriver"); 
       throwable.printStackTrace(); 
      }    
      @Override 
      public void beforeNavigateTo(String url, WebDriver driver) { 
       System.out.println("Before NavigateTo :: " + url); 
      }    
      @Override 
      public void afterNavigateTo(String url, WebDriver driver) { 
       System.out.println("After NavigateTo :: " + url); 
      } 

     }); 

     webDriver = efwd; 

    } 

    /** 
    * Test instance which attempts to navigate to the configured url and establishes an ExplicitWait to confirm 
    * navigation before continuing with the test event. 
    */ 
    @Test 
    public void testPageLoadFeedback() { 
     webDriver.navigate().to(url); 
     /* 
     * FIXME: How long are you actually willing to wait. 
     * 
     * It's my understanding that if the wait value is less than the implicit wait, then the looping capabilities of 
     * the ExplicitWait are never really leveraged. 
     * 
     * Note that the time value is the maximum time to wait. If the condition validates before the time specified 
     * then the process will continue. 
     */ 

     WebDriverWait wait = new WebDriverWait(webDriver, PAGE_LOAD_SECONDS * 2); 
     wait.pollingEvery(500, TimeUnit.MILLISECONDS); 
     wait.until(navTestCondition); 
     // If we get to this line then the test passed and we should be on the page. 
     System.out.println("Navigation Succeeded!"); 
    }  

    /** 
    * Cleanup method to close any browsers used by the test session. 
    */ 
    @After 
    public void cleanupTest() { 
     //XXX: To leave the browser open, comment out the @After of this method. 
     for (String handle : webDriver.getWindowHandles()) { 
      webDriver.switchTo().window(handle); 
      webDriver.close(); 
     } 
    } 

} 

祝你好運,我希望這有助於。

+0

工作完美,感謝您的幫助:) – scriptss