【问题标题】:Is it possible to pass an argument in command line for a user to choose to run Selenium headed or headless?是否可以在命令行中传递参数让用户选择运行 Selenium 有头或无头?
【发布时间】:2023-03-10 16:01:01
【问题描述】:

我知道如何无头运行我的 Selenium 测试,但我还需要选择使用浏览器和 gui 运行。现在我通过命令行传递“-headless”,而不是在 Selenium 代码中将 firefoxOptions headless boolean 设置为 true。

我不熟悉通过命令行传递参数,但我知道如何循环它们。我想知道我是否可以使“无头”成为条件。

【问题讨论】:

    标签: java selenium shell command-line


    【解决方案1】:
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog(null, "Would you want to run this test in headless mode?",null, dialogButton);
        
        ChromeOptions options = new ChromeOptions();
        if(dialogResult == JOptionPane.YES_OPTION){
              // Saving code hereoptions
            options.addArguments("--headless");
            }
    

    使用 Joptionplaneframe java swing frame 工作

    这将打开一个促销,

    如果您不想提示,请使用扫描仪:

            Scanner scanner = new Scanner(System.in);
    
            //  prompt for the user's name
            System.out.print("Enter yes or no: ");
    
            // get their input as a String
            String input= scanner.next();
            
            System.out.print(input);
    

    【讨论】:

      【解决方案2】:

      您可能应该使用一些构建工具,例如 gradle 或 maven。这将允许您使用 -D 参数传递属性,如下所示:./gradlew clean test -Dparam=val -Dparam2=val2。因此,您可以使用 System.getProperties()System.getPropery(prop) 在代码中检索道具。

      也可以使用环境变量并通过System.getEnv()System.getEnv(varName) 检索它们。但我建议对这个特定任务使用属性。

      所以想法是您将 args 传递给作业,在代码中检索它们并在调用驱动程序之前将它们传递给 firefoxOptions。

      我不建议在运行期间提示用户提供任何信息,因为这在持续集成过程 (CI) 中不起作用。 您应该选择无头参数是强制性的(每次都必须显式传递参数),还是可选的带有一些默认值。例如,如果没有提供参数,测试将默认以无头模式运行。

      ./gradlew clean test -Dbrowser=firefox -Dheadless=true

      【讨论】:

      • 嗨 Vitaliy,我明白你在说什么,但需要澄清一下。 -D 参数是运行测试的人可以在命令行中传递的吗?我已经有一个带有初始规范的automation.sh 文件。因此,如果他们要运行测试,会不会是类似 ./automation -Dheadless = true??
      • 如果你有 sh 文件,你必须先将参数传递给 sh 文件,然后从那里将参数传递给 gradle。或者您可以为 sh 文件编写自定义参数,它可以是一个布尔标志,并在 sh 文件中创建一个通用的 IF 语句,该语句执行带有无头或无参数的命令您可以参考unix.stackexchange.com/questions/31414/…
      猜你喜欢
      • 2019-11-29
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多