【问题标题】:does file_get_contents use the browser's cached image if i navigate to it's url with selenium?如果我使用 selenium 导航到它的 url,file_get_contents 是否使用浏览器的缓存图像?
【发布时间】:2016-11-03 22:19:53
【问题描述】:

所以我有这个脚本

<?php
    require_once('../_libTest/__init__.php');

    class cacheTest
    {
        private $driver;

        function __construct()
        {

            $host = 'http://localhost:4444/wd/hub'; // this is the default
            //$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'chrome');

            $capabilities = DesiredCapabilities::chrome();
            $options = new ChromeOptions();
            /*$options->addExtensions(array(
                                      '3.2.1_0.crx'
                                    ));*/
            $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

            $this->driver = RemoteWebDriver::create($host, $capabilities,86400000,86400000);

        }



        function run()
        {
            $url = "https://www.google.com.au/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
            $this->driver->get($url);
            $filename = explode("/",$url);
            $filename = end($filename);
            $image = file_get_contents($url);
            file_put_contents($filename,$image);

            //$this->driver->quit();
        }
    }


    $app = new cacheTest();


    $app->run();
?>

使用 Selenium 独立服务器、Chrome Webdriver 和 this PHP Web Driver(已编辑以删除所有命名空间)以及包含在 __init__.php 文件中的所有文件,我可以打开 Chrome 浏览器窗口,转到然后使用file_get_contentsfile_put_contents 下载谷歌徽标图片

但是我想知道,因为为了节省时间,当我运行 Selenium 并直接导航到图像时,浏览器通常会缓存图像(因此 Chrome 的清除浏览历史记录中的选项“缓存图像和文件”) ,当我使用file_get_contents 时,这是获取浏览器的缓存图像还是从服务器获取新的图像?

根据herehere 的答案,答案可能是否定的,但在我的情况下,我首先通过-&gt;get($url) 加载页面

【问题讨论】:

    标签: php selenium caching


    【解决方案1】:

    我认为实现此目的的最佳方法是创建一个自定义浏览器配置文件,该配置文件已禁用缓存,并告诉 selenium 在您启动它时使用该配置文件。在任何情况下,使用自定义配置文件进行测试都比打开默认配置文件更好,因为您可以更好地控制它的设置。

    firefox 示例(windows box):

    1. In a shell, run firefox.exe -p, name profile selenium_tester and place in a folder of choice
    2. start firefox with profile
    3. about:config
    4. network.automatic-ntlm-auth.trusted-uris AND network.negotiate-auth.trusted-uris -- URLs of sites to bypass for selenium (localhost, etc)
    5. network.automatic-ntlm-auth.allow-non-fqdn TRUE
    6. browser.cache.disk.enable SET TO FALSE
    

    然后,启动 selenium:

    IE、Chrome 和 Firefox(为了便于阅读,我用 \ 分隔,但在提示窗口中运行时将其删除)

    START java -DfirefoxProfileTemplate=E:\FIREFOX_PROFILE_FOLDER\selenium_tester \
    -Dwebdriver.firefox.profile=selenium_tester \
    -Dwebdriver.ie.driver=E:\DRIVERS_FOLDER\IEDriverServer.exe \
    -Dwebdriver.chrome.driver=E:\DRIVERS_FOLDER\chromedriver.exe \
    -Dwebdriver.gecko.driver=E:\DRIVERS_FOLDER\geckodriver.exe -jar \
    E:\SELENIUM_LOCATION\selenium-server-standalone-3.0.1.jar
    

    在 chrome 中创建配置文件的资源:@​​987654321@

    在 chrome 中禁用缓存:(看起来更像是 PITA,而不是 Firefox) Disabling Chrome cache for website development

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-23
      • 2013-08-08
      • 2012-07-27
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      相关资源
      最近更新 更多