如何使用geckodriver检索Firefox的崩溃数据(Java) - java

我被要求提供对Firefox崩溃数据的分析,因此我尝试遵循此Firefox docs中的步骤。

我必须在我自己的Java测试代码之前添加此Python代码:

import tempfile

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# Custom profile folder to keep the minidump files
profile = tempfile.mkdtemp(".selenium")
print("*** Using profile: {}".format(profile))

# Use the above folder as custom profile
opts = Options()
opts.add_argument("-profile")
opts.add_argument(profile)
opts.binary = "/Applications/Firefox.app/Contents/MacOS/firefox"

driver = webdriver.Firefox(options=opts,
    # hard-code the Marionette port so geckodriver can connect
    service_args=["--marionette-port", "2828"])

# Your test code which crashes Firefox

所以我这样写:

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;


final Path basedir = FileSystems.getDefault().getPath("/tmp");
final String tmp_dir_prefix = ".selenium";
final Path tmp_dir = Files.createTempDirectory(basedir, tmp_dir_prefix);

File firefoxProfileFolder = new File(tmp_dir.toString());
FirefoxProfile customProfile = new FirefoxProfile(firefoxProfileFolder);

File pathToBinary = new File("/usr/bin/firefox-trunk");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathToBinary);

FirefoxOptions options = new FirefoxOptions();
options.setBinary(firefoxBinary);
options.setProfile(customProfile);

WebDriver driver = new FirefoxDriver(options);

但是我完全不知道如何在我的最后一个Java行中集成以下python代码:

driver = webdriver.Firefox(options=opts,
    # hard-code the Marionette port so geckodriver can connect
    service_args=["--marionette-port", "2828"])

任何的想法 ?

参考方案

对于任何使用Java进行编程的人来说,这段代码对我来说都是有效的:

File pathToGeckoDriver = new File("/path/to/geckodriver/executable");
File pathToFirefoxBinary = new File("/path/to/firefox/executable");

# Custom profile folder to keep the minidump files
Path basedir = FileSystems.getDefault().getPath("/tmp");
String tmp_dir_prefix = ".selenium";
Path tmp_dir = Files.createTempDirectory(basedir, tmp_dir_prefix);

# Use the above folder as custom profile
FirefoxBinary ffBinary = new FirefoxBinary(pathToFirefoxBinary);
ffBinary.addCommandLineOptions("-profile");
ffBinary.addCommandLineOptions(tmp_dir.toString()); # Use the above folder as custom profile

WebDriver driver = new FirefoxDriver(
           new GeckoDriverService.Builder()
           .usingFirefoxBinary(ffBinary)
           .usingPort(2828) # hard-code the Marionette port so geckodriver can connect
           .usingDriverExecutable(pathToGeckoDriver))
           .build()
);

# Your test code which crashes Firefox

Selenium Grid显示WebDriverException错误 - java

我的Selenium网格显示错误:org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property;但我已经完美地指定了它(据我所知)System.out.pr…

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

Java Scanner读取文件的奇怪行为 - java

因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…

Java:正则表达式模式匹配器是否有大小限制? - java

我的模式类似于OR:“word1 | word2 | word3”我大约有800个字。可能有问题吗? 参考方案 您仅受记忆和理智的限制。 :)

Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …