selenium使用代理

时间:2015-10-04 14:58:02

标签: java selenium selenium-webdriver

我正在使用代理连接到使用selenium的网站并测试一些东西,问题是一些代理速度非常慢并且使得事情效率非常低,但另一个问题是我无法捕获错误无论我做什么都继续下去。尝试过其他堆叠帖子的各种建议,但无济于事。

    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秒,因为它必须导航几页并且一些代理需要30-40秒才加载页面所以需要相当长的时间,是否真的有办法捕获错误

Timed out waiting for page load

我的另一个问题是,使用硒替代品会更容易吗?到目前为止,我有一些硒的问题似乎已经存在了一段时间,其他人的帖子判断

1 个答案:

答案 0 :(得分:1)

scriptss,

  在我看来,您需要使用ExplicitWaits来验证您的导航。 ExplicitWaits不是在驱动程序中使用隐式配置,而是允许更灵活的解决方案,具体取决于您的环境。根据我的经验,WebDriverWait类与ExpectedConditions一起解决了我的大多数问题。我尚未获得对驱动程序的代理支持,但它似乎与我迄今为止获得的信息一致。

话虽如此,我不完全确定为什么你没有看到被抛出的异常。当我遇到类似的问题时,我会将我的WebDriver包装在EventFiringWebDriver中,并查看WebDriverEventListener的onException消息是否在这些情况下被触发。

在下面的示例中,您需要填写代理IP和端口的常量,以及要转到的实际URL。在createParameters方法中,您还需要更新标题和部分URL的字符串以适合您的用例。

使用Java 8和Selenium 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();
        }
    }

}
祝你好运,我希望这会有所帮助。