首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebDriver:在Selenium测试用例中使用火狐开发工具

WebDriver:在Selenium测试用例中使用火狐开发工具
EN

Stack Overflow用户
提问于 2016-01-12 09:35:22
回答 1查看 4.8K关注 0票数 1

我想知道是否可以在Selenium/WebDriver测试用例中使用Firefox开发工具的功能。具体来说,我想导出由网站发送的HTTP请求作为HAR文件。

我知道使用Firefox扩展并将其添加到这样的配置文件中(在Java中):

代码语言:javascript
复制
FirefoxProfile.addExtension(java.io.File extensionToInstall)
FirefoxProfile.setPreference(java.lang.String key,
                 java.lang.String value)

但是为此,我需要DevTools作为XPI文件,而且我找不到任何关于它的信息。

我还知道设置了如下所需的功能(在Java中):

代码语言:javascript
复制
FirefoxDriver(Capabilities desiredCapabilities) 
Capabilities.setCapability(java.lang.String capabilityName, java.lang.String value)

但我不知道要设置哪些功能才能使用HAR文件导出功能。

对于类似的问题,我阅读了很多评论和答案,这些评论和回答归结为:

  • 使用另一个测试工具(Sikuli)
  • 使用另一种浏览器(Chrome)
  • 使用另一个副词(Firebug+Netexport,尼斯示例)

无论如何,我想知道一种使用Firefox开发工具的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-13 15:09:51

这个解决方案使用火狐的原生devtools和一个名为HAR出口触发器的插件。虽然是测试版,但它适用于:

  • 火狐:43.0.4
  • 硒:'2.48.2‘
  • os.name:“Linux”
  • os.version:‘3.13.0-74-泛型’
  • java.version:'1.7.0_91‘

这是一个简单的示例代码:

代码语言:javascript
复制
/**Test class for exporting HTTP requests of a web page to HAR files.*/
public class HarExportTest  {
    /**Absolute path needed because FF profile for Selenium is temporary.*/
    private final static String HARDIR = "/home/test/HAR_Demo/target";

    public static void main(String[] args) {   
        WebDriver driver = null;

        try {
            // Init
            driver = new FirefoxDriver(buildNetmonitorProfile());
            final File harDir = new File(HARDIR);
            final int numFiles = harDir.listFiles().length;
            System.out.println(harDir.getPath() + ": " + numFiles);         
            // Load test page
            driver.get("https://ixquick.com");
            // Wait for new file
            for (int c=0; c<180; c++) {
                if (harDir.listFiles().length > numFiles) {
                    System.out.println("added");
                    break;
                }
                Thread.sleep(1000L);
            }
            System.out.println(harDir.getPath() + ": " + harDir.listFiles().length);
        }
        catch (Exception exc) {
            System.err.println(exc);
        }

        if (driver != null) {
            driver.quit();
        }
    }

    private static FirefoxProfile buildNetmonitorProfile() throws IOException {
        FirefoxProfile profile = new FirefoxProfile();  

        // Load extensions         
        File harExport = new File("harexporttrigger-0.5.0-beta.7.xpi"); //adjust path as needed
        profile.addExtension(harExport);

        // Enable the automation without having a new HAR file created for every loaded page.
        profile.setPreference("extensions.netmonitor.har.enableAutomation", true);
        // Set to a token that is consequently passed into all HAR API calls to verify the user.
        profile.setPreference("extensions.netmonitor.har.contentAPIToken", "test");
        // Set if you want to have the HAR object available without the developer toolbox being open.
        profile.setPreference("extensions.netmonitor.har.autoConnect", true);

        // Enable netmonitor
        profile.setPreference("devtools.netmonitor.enabled", true);
        // If set to true the final HAR file is zipped. This might represents great disk-space optimization especially if HTTP response bodies are included.
        profile.setPreference("devtools.netmonitor.har.compress", false);
        // Default name of the target HAR file. The default file name supports formatters
        profile.setPreference("devtools.netmonitor.har.defaultFileName", "Autoexport_%y%m%d_%H%M%S");
        // Default log directory for generate HAR files. If empty all automatically generated HAR files are stored in <FF-profile>/har/logs
        profile.setPreference("devtools.netmonitor.har.defaultLogDir", HARDIR);
        // If true, a new HAR file is created for every loaded page automatically.
        profile.setPreference("devtools.netmonitor.har.enableAutoExportToFile", true);
        // The result HAR file is created even if there are no HTTP requests.
        profile.setPreference("devtools.netmonitor.har.forceExport", true);   
        // If set to true, HTTP response bodies are also included in the HAR file (can produce significantly bigger amount of data).
        profile.setPreference("devtools.netmonitor.har.includeResponseBodies", false);
        // If set to true the export format is HARP (support for JSONP syntax that is easily transferable cross domains)
        profile.setPreference("devtools.netmonitor.har.jsonp", false);
        // Default name of JSONP callback (used for HARP format)
        profile.setPreference("devtools.netmonitor.har.jsonpCallback", false);
        // Amount of time [ms] the auto-exporter should wait after the last finished request before exporting the HAR file.
        profile.setPreference("devtools.netmonitor.har.pageLoadedTimeout", "2500");

        return profile;
    }
}

产出如下:

/home/test/HAR_Demo/目标:9 已添加 /home/test/HAR_Demo/目标: 10

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34739969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档