【问题标题】:Selenium running headless Firefox browser in WindowsSelenium 在 Windows 中运行无头 Firefox 浏览器
【发布时间】:2013-06-08 09:15:18
【问题描述】:

是否可以将 Selenium 配置为使用 Firefox 驱动程序并在 Windows 中无头运行浏览器?

我知道其他驱动程序在 Windows 或 Linux 下也能正常工作,但在上述特定情况下却没有。非常感谢您阅读的任何参考信息(实现它的特殊方法、限制等)。

问候,

【问题讨论】:

    标签: firefox selenium selenium-webdriver


    【解决方案1】:

    可以通过操作系统 Windows 支持的专用虚拟桌面运行浏览器(Firefox、IE、...)。该任务的一个已知帮助实用程序是Headless Selenium for Windows

    【讨论】:

      【解决方案2】:

      这是我们在 Windows 上以无头模式使用 firefox 驱动程序运行 selenium 的方式。

      创建 Windows 任务计划,您可以使用 UI 执行此操作 http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7

      或者使用这样的命令:

      schtasks /Create /TN Automation /TR C:\automation\automated_regression.bat /SC ONSTART /RU Administrator /RP password /F /V1
      

      在我们的例子中,自动化是蚂蚁驱动的,所以automated_regression.bat 有这样的东西

      :myLoop
      cd c:\automation
      call ant_env.bat
      call ant -f regression.xml
      GOTO myLoop
      

      regression.xml 有一个 selenium java 项目的典型 junit 目标

          <property name="main.dir" location="./selweb" />
          <property name="src.dir" location="${main.dir}/src" />
          <property name="lib.dir" location="${main.dir}/lib" />
          <property name="build.dir" location="${main.dir}/build" />
          <property name="test.report" location="${main.dir}/testreport">
          </property>
          
          <path id="build.classpath">
              <fileset dir="${lib.dir}">
                  <include name="**/*.jar" />
              </fileset>
          </path>
          
          <target name="clean">
              <delete dir="${build.dir}" />
              <delete dir="${test.report}" />
          </target>
          
          <target name="make dir" depends="clean">
              <mkdir dir="${build.dir}" />
              <mkdir dir="${test.report}" />
          </target>
          
          <target name="compile" depends="clean, make dir">
              <javac srcdir="${src.dir}" destdir="${build.dir}" debug="true">
                  <classpath refid="build.classpath" />
              </javac>
          </target>
          
          <target name="junit" depends="clean, make dir,compile">
              <loadfile property="LATEST" srcFile="LATEST" />
              <junit printsummary="no" fork="true" haltonfailure="false" dir="${main.dir}">
                  <classpath>
                      <pathelement path="${build.dir}" />
                      <fileset dir="${lib.dir}">
                          <include name="**/*.jar" />
                      </fileset>
                  </classpath>
                  <formatter type="xml" />
                  <batchtest todir="${test.report}">
                      <fileset dir="${build.dir}">
                          <include name="**/tests/**/*.class" />
                      </fileset>
                  </batchtest>
              </junit>
              
              <junitreport todir="${test.report}">
                  <fileset dir="${test.report}">
                      <include name="**/*.xml"/>
                  </fileset>
                  <report format="noframes" todir="${test.report}/html" styledir="${main.dir}/style"> 
                  <param name="TITLE" expression="Selenium Test Results for build ${LATEST}"/>
                  </report>
                  <report format="frames" todir="${test.report}/html" styledir="${main.dir}/style"/>
              </junitreport>      
          </target>
      

      您可以使用记录器来记录您的 ant 运行时,例如。

      <record name="log\automation_${timestamp}.log" loglevel="verbose" append="false" />
      

      使用它,您可以跟踪无头自动化中正在发生的事情。

      The ' characters around the executable and arguments are
      not part of the command.
          [junit] Test com.yourtests ... FAILED
          [junit] Implicitly adding C:\automation\dep\apache-ant-1.8.4\lib\ant-launcher.jar;C:\automation\dep\apache-ant-1.8.4\lib\ant.jar;C:\automation\dep\apache-ant-1.8.4\lib\ant-junit.jar;C:\automation\dep\apache-ant-1.8.4\lib\ant-junit4.jar to CLASSPATH
      .....    
      'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
      'com.yourtests'
      'filtertrace=true'
      'haltOnError=false'
      'haltOnFailure=false'
      'showoutput=false'
      'outputtoformatters=true'
      'logfailedtests=true'
      'logtestlistenerevents=false'
      'formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,c:\automation\selweb\testreport\TEST-com.yourtests'
      'crashfile=c:\automation\junitvmwatcher2114698975676150832.properties'
      'propsfile=c:\automation\junit4190343520192991051.properties'
      

      我们采用了这种方法并且它正在工作,甚至正在拍摄屏幕截图并将其插入到 ant-junit html 报告中。

      所以本质是你需要通过windows Tasks Scheduler运行你的selenium,它将以无头模式运行。我认为使用 cron 在 linux 下可以完成类似的事情,但我还没有尝试过它是否有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-07
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多