Thursday, August 8, 2013

Help! My tests take more than 8 hours to run.

Help! My Selenium tests take more than 8 hours to run.

We have a problem with too many tests that take too long to run. The Ant build script documented in a previous post only runs tests in sequence. 

We could try running more than one at the same time.
Spinning trophy lids.
Image recorded at the Warner Bros. Studio Tour London The Making of Harry Potter

Selenium has support to run Firefox tests in parallel. Selenium server is able to open multiple Firefox browsers and keep track of each of them in a separate session. Unfortunately there are issues with Internet Explorer that cause it not to be able to run in parallel. 

Thanks to Solutions Daily blog http://solutionsdaily.blogspot.com/2010/06/making-junit-tests-run-parallely.html we now have a way to launch more than one Firefox test at a time.

The key is adding another ant jar called ant-contrib into your library. I downloaded it from here.

Then it has to be referenced in your build.xml file for Ant. 

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
   <pathelement location="C:/ant/apache-ant-1.8.4/lib/ant-contrib-1.0b3.jar"/>
 </classpath>
</taskdef>

This gives you the command called foreach which allows for a loop. This is used to start each test individually in rapid succession. The old way involved making a batch of all of the tests and running them one by one, waiting for each to finish.

Now the target for running Firefox tests needs to be set in a way to implement the foreach.

Mine looks like this now. This is the part that runs the foreach and passes each test name to the execute.test target. The inheritrefs part of foreach is what passes the data to the next target. Notice that I set maxthreads to 5. That is the most I have run at one time so far and is the default number set in Selenium Grid.

<target name="run-ff-tests" depends="compile" description="run your test suite in firefox" >
  
<foreach 
              target="execute.test" 
              maxthreads="5" 
              inheritall="true" 
              inheritrefs="true" parallel="true" param="test.source.absolute">
<path>
<fileset dir="${src}">
      <include name="**/*Test*.java"/>
    </fileset>
  </path>
</foreach>

   </target>

This is the target that runs one test at a time.
<target name="execute.test">

 <!-- we need to have relative path -->
 <pathconvert property="test.source.relative">
   <fileset file="${test.source.absolute}" />
   <map from="${src}/" to="" />
 </pathconvert>

 <!-- run one particular test -->
 <junit fork="false" printsummary="true" haltonfailure="false" showoutput="true">

  <classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
  <sysproperty key="browser.property" value="firefox" />

   <formatter type="xml" />

   <batchtest todir="${reports}/ff-raw/">
     <fileset dir="${src}">
       <filename name="${test.source.relative}" />
     </fileset>
   </batchtest>
 </junit>

</target>


Now we have been able to run more tests at one time for Firefox at least. To save time in running Internet Explorer tests we are turning to Selenium Grid. But that will wait until the next post.

By the way, this is one way to run unit tests in parallel using Ant also. Under certain conditions JUnit tests can be run in parallel and saves time in waiting for tests to finish after each build.

Take a look at our training at http://lmnsolutions.com/training.html

© Copyright 2013 LMN Solutions
If you  have a question e-mail us or add a comment.


LMN Solutions - Find out more about us.
Selenium Training
selenium@lmnsolutions.com

Friday, June 7, 2013

Selenium 2 asks, What Browser are you?

Selenium 2 asks, "What Browser are you?"

If you have a test that is curious about what browser type or version is running it is possible to have it ask the browser what it is.

Perhaps the layout of the page changes or the content based on the version of the browser and different locators need to be built for different types or versions.

WebDriver driver = new InternetExplorerDriver();
String userAgent = (String) ((JavascriptExecutor)driver).executeScript("return navigator.userAgent;");

System.out.println(userAgent);

Now that you have the user agent, you need to interpret the user agent into the proper action.


© Copyright 2013 LMN Solutions
If you  have a question e-mail us or add a comment.


LMN Solutions - Find out more about us.
Selenium Training
selenium@lmnsolutions.com

Random data for tests


Random data for tests

Sometimes tests need random data. It could be that a test cannot register a user more than once.

This is a quick command to generate a random series of characters. Give it a number of how many characters you want.

String name = RandomStringUtils.randomAlphabetic(20);

It does require the Apache Commons Lang library. http://commons.apache.org/proper/commons-lang/



© Copyright 2013 LMN Solutions
If you  have a question e-mail us or add a comment.


LMN Solutions - Find out more about us.
Selenium Training
selenium@lmnsolutions.com

Tuesday, February 19, 2013

Automating Selenium with Ant

Automating Selenium with Ant

or How to Enable Multi Browser Testing with JUnit Reports


This is a topic from the Introduction to Selenium class at LMN Solutions.

In order to pass in the browser selection from the Ant script code like this is required in your JUnit Selenium Web Driver tests. I put this code into the parent class that all my Selenium JUnit tests extend.

@BeforeClass
public static void startSelenium() throws Exception {
Properties sysProps = System.getProperties();
                String     browser = sysProps.getProperty("browser.property");
                if (browser == null) {
             driver = new InternetExplorerDriver();
                } else if (browser == "*firefox") {
             driver = new FirefoxDriver();
                }
}

In the Ant build.xml file you will find the code:

<sysproperty key="browser.property" value="*iexplore" />

Which tells the JUnit code which browser to launch.

You will also see:


<junitreport todir="${reports}">
    <fileset dir="${reports}/ie-raw/">
         <include name="TEST-*.xml"/>
     </fileset>
     <report format="frames" todir="${reports}\ie-html\"/>
</junitreport>

This tells JUnit to build a html report of the test results when finished.

Then I have build an Ant build.xml file that you can download. See if you can get this set up and running while I write up some step by step directions. Please send me any questions you have and make sure to check out our classes at http://lmnsolutions.com/training.html.


© Copyright 2013 LMN Solutions
If you  have a question e-mail us or add a comment.






LMN Solutions - Find out more about us.
Selenium Training