Selenium在Windows中运行无头Firefox浏览器

nma*_*rov 15 firefox selenium selenium-webdriver

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

我知道其他驱动程序在Windows或Linux下工作,但不是在上面提到的特定情况下.任何参考信息(实现它的特殊方式,限制等)都是高度适用的.

问候,

Nik*_*nov 10

可以通过OS Windows支持的专用虚拟桌面运行浏览器(Firefox,IE,...).用于该任务的一种这样的已知辅助工具是用于Windows的Headless Selenium.


Tib*_*riu 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
Run Code Online (Sandbox Code Playgroud)

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

:myLoop
cd c:\automation
call ant_env.bat
call ant -f regression.xml
GOTO myLoop
Run Code Online (Sandbox Code Playgroud)

其中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>
Run Code Online (Sandbox Code Playgroud)

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

<record name="log\automation_${timestamp}.log" loglevel="verbose" append="false" />
Run Code Online (Sandbox Code Playgroud)

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

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'
Run Code Online (Sandbox Code Playgroud)

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

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