Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium running headless Firefox browser in Windows

Is it possible to configure Selenium to use Firefox driver and run the browser headlessly within Windows?

I am aware of other drivers working so within Windows or under Linux but not in the particular case mentioned above. Any reference information (ad-hoc ways to achieve it, limitations, etc.) to read upon is highly appreaciated.

Regards,

like image 692
nmadzharov Avatar asked Jun 12 '13 09:06

nmadzharov


People also ask

How do I run Firefox in headless mode?

Firefox in headless mode, can be run once we configure the geckodriver path. We shall then use the FirefoxOptions class, and send the headless knowledge to the browser with setHeadless method and pass true as a parameter to it.

Does Selenium support headless browser?

Selenium supports headless browser testing using HtmlUnitDriver. HtmlUnitDriver is based on java framework HtmlUnit and is the one of the lightweight and fastest among all headless browser.

How do I run a Selenium script in headless mode?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.


2 Answers

It is possible to run browsers (Firefox, IE, ...) via dedicated virtual desktop which supported by OS Windows. One such known helper utility for that task is Headless Selenium for Windows.

like image 51
Nikolay Artamonov Avatar answered Oct 09 '22 16:10

Nikolay Artamonov


Here is the way we are running selenium using firefox driver in headless mode on windows.

Create a windows task schedule, you can either do this using the UI http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7

or with a command like this :

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

in our case, the automation is ant driven, so the automated_regression.bat has something like this

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

where the regression.xml has a the typical junit targets of a selenium java project

    <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>

you can use a logger to record your ant runtime eg.

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

using this you can follow what is going on in your headless automation.

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'

We have followed this approach and it's working, even screen shots are being taken and inserted in the ant-junit html report.

So the essence is that you need to run your selenium through windows Tasks Scheduler and it will run in headless mode. I think something similar can be done under linux using the cron, but i haven't tried it out to see if it works.

like image 22
Tiberiu Avatar answered Oct 09 '22 17:10

Tiberiu