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

时间:2013-06-12 09:49:19

标签: firefox selenium selenium-webdriver

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

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

此致

3 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分: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>

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

<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,它将以无头模式运行。我认为类似的东西可以在linux下使用cron完成,但我还没有尝试过看看它是否有效。

答案 2 :(得分:0)

根据最近的发展情况,可以在没有任何外部黑客攻击的情况下以无头模式运行Chrome和Firefox。

对于Chrome:

https://www.youtube.com/watch?v=0gSDYRbtwmw

对于Firefox:

https://www.youtube.com/watch?v=D0xcg5nviso

相关问题