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


Sunday, October 28, 2012

Convert Selenium 1 RemoteControl to Selenium 2 WebDriver




Here is a more complete reference guide to converting Java Remote Control commands into Selenium 2 WebDriver Java commands. This comes straight out of the class we teach at LMN Solutions. Selenium 2 classes in Reston, VA

Description
Remote Control – Selenium 1
WebDriver - Selenium 2 Command
Get access to driver
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.w3schools.com/");
selenium.start();
FirefoxDriver driver = new FireFoxDriver(); 
InternetExplorerDriver driver = new InternetExplorerDriver();
HtmlUnitDriver driver = new HtmlUnitDriver();
OperaDriver driver = new OperaDriver();
ChromeDriver driver = new ChromeDriver();
Page Title
selenium.getTitle();
driver.getTitle();
Navigate to page
selenium.open(“http://www.google.com”);
driver.get(“http://www.google.com”);
Find element by name

element = driver.findElement(By.name("first_name"));
Delete text from input field
selenium.type("");
element.clear();
Sets value of input field
selenium.type("Brad");
element.sendKeys("Brad");
Click on the element

selenium.click("submit");
element.click();
Note: If element is a link or button it will perform the click action
Return text between element tags
selenium.getText("//h3[2]");
element.getText(); 
Returns the value of the element
selenium.getValue("first_name");
element.getValue();
Returns true or false designating if the element is selected
selenium.isChecked("female");
element.isSelected();
Get the Select list value
selenium.getSelectedValue("cars");
Select select = new Select(element);
select.getFirstSelectedOption();
Where on the page is the top left-hand corner of the rendered element
n/a
element.getLocation();
Get the tag name of the element
n/a
element.getTagName();
Returns true or false designating if the element is displayed
selenium.isVisible("hidden_field");
element.isDisplayed();
Returns true or false designating if the element is enabled.  Will generally return true for everything but disabled input elements
selenium.isEditable("last_name");
element.isEnabled();
Width and height of the rendered element
Almost equivalent
selenium.getAttribute("first_name@maxlength");
element.getSize();
Returns the attribute
<input type="textvalue="Joename="FirstName">
selenium.getAttribute("FirstName@value");
element.getAttribute(“value”);
Submits the form if element is a form or within a form
selenium.submit("first_name");
element.submit();
Alerts
selenium.chooseOkOnNextConfirmation();
selenium.chooseCancelOnNextConfirmation();
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
Frames
selenium.selectFrame("relative=up");
selenium.selectFrame("showframe");
driver.switchTo.defaultContent();
driver.switchTo.frame("showframe);
Double Click
selenium.doubleClick("aButton");
Actions action = new Actions(driver);
action.doubleClick(addElement);
Element Present
selenium.isElementPresent();
Try {
   element = driver.findElement(By.name("name"));
} catch (NoSuchElementException e) {
   fail("name - no such element");
}


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






LMN Solutions - Find out more about us.
Selenium Training

Wednesday, September 19, 2012

How to run Selenium WebDriver with Console Output


How to run Selenium WebDriver with Console Output

I have been asked how to run Selenium 2 WebDriver in a mode that emulates the console output window that Selenium Remote Control has. WebDriver starts up the selenium server in the background and you cannot see any error messages that might be helpful.

First start up the selenium console as you did with Selenium 1 tests:

java -jar selenium-server-standalone.x.xx.x.jar

In your test code use this:


driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),   DesiredCapabilities.firefox());

instead of this:
        
driver = new FirefoxDriver();

but of course you need to import this:


import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

So you will get the helpful logging that Selenium provides below:






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






LMN Solutions - Find out more about us.
Selenium Training