Showing posts with label Selenium RC. Show all posts
Showing posts with label Selenium RC. Show all posts

Monday, January 3, 2011

xUnit.Net – Running the tests (ClassInitialize – ClassCleanup)

I started using xUnit.Net few weeks ago. My first question was how to do the things I was used to do in other testing frameworks like MSTest or NUnit, specially when using these frameworks not only for unit testing but for higher level tests like Selenium RC web tests. So far, the framework seems to be very good and extensible.

I am going to show some scenarios I have run into converting MSTest to xUnit.Net, having the following class

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass] public class AdminPageTest { static SeleniumWebTestContext test = new SeleniumWebTestContext(); [ClassInitialize()] public static void ClassInitialize() { new LoginPage(test).LoginUser(“maria”, “******”); } [ClassCleanup()] public static void ClassCleanup() { new AdminPage(test.Driver).Logout(); test.StopWebTestContext(); } [TestCategory("WebTest"), TestMethod] public void TestViewMyProfile() { var profileWindow = new AdminPage(test.Driver).SelectMyProfile(); Assert.IsTrue(profileWindow.CheckFirstName(“Maria”)); profileWindow.Close(); } [TestCategory("WebTest"), TestMethod] public void TestAdminSearchUser() { var userWindow = new AdminPage(test.Driver).SelectUserManagement(); userWindow.Search("Marcano Maria"); Assert.IsTrue(adminTab.VerifyEmail("my-email@domain.com")); } }

Note that SeleniumWebTestContext is holding information about Selenium RC and starts the server.

ClassInitialize – ClassCleanup / IUseFixture<T>

Sometimes we require sharing information among the methods defined in a class, for example in web tests we want to share the logged in the user information, execute different actions and validations and log out at the end:

using Xunit;

public class AdminPageTest : IUseFixture<LoggedUserContext> { SeleniumWebTestContext test; public AdminPageTest() { } public void SetFixture(LoggedUserContext usrContext) { test = usrContext.Test; } ... }


and the LoggedUserContext class will look like this:
public class LoggedUserContext : IDisposable
{    
    public SeleniumWebTestContext Test;    
    public LoggedUserContext()    
    {        
        Test = new SeleniumWebTestContext();        
        new LoginPage(Test).LoginUser(“maria”, “******”);    
    }    

    public void Dispose()    
    {       
        new AdminPage(Test.Driver).Logout();
        Test.StopWebTestContext();
    }
}

Saturday, November 27, 2010

Selenium’s How-To

Recently I ran into a couple of issues that started affecting some selenium automated tests I was developing, below the issues and solutions implemented:

XHR ERROR: Response_Code = –1 Request Error

When executing the open command the tests started failing with the following error

XHR ERROR: URL = https:// Response_Code = -1 Error_Message = Request Error. on Firefox version 3.6.12 and Selenium RC version 1.0.3

I found that the issue was reported by other users (issue 408 on selenium project page). The open command started working again after making the suggested workaround on comment 4,  calling the open command and passing the second parameter true:

public void CustomOpen(string url)
{     
    commandProcessor.DoCommand("open", new String[] { url, "true" });
}

According to comment 4, the issue was caused by a feature that was supposed to be disabled.

The issue seems to be related to that second parameter (ignoreResponseCode) on the open command and the default value assigned when it is null or empty, there is more details about the fix on commend 14. The fix hasn't been released yet, so if you run into this issue you can use this workaround.

Https pages

When running selenium on development / test environment, usually there is a need to deal with the certificate exceptions thrown by the browser.

I was experiencing the following behavior when running the tests using *firefoxproxy mode, this mode will remember the certificate exception added but if the site is redirected automatically to other location, Selenium throws permission denied exception (I supposed this is related to Same Origin Policy restriction).

Reading a little further, *firefoxproxy mode is supported only for backward compatibility and *firefox mode should be use in Selenium-RC 1.0 beta 2 and later.

I tried the following solutions mentioned here running selenium using *firefox mode:

Both approaches worked, but the test execution experiences a small delay when using RCE.

Saturday, October 23, 2010

Automating MyAppInFlex.swf – Useful FlexPilot commands

I continue automating my Flex application with FlexPilot. One clear advantage I found when using FlexPilot is that I was able to access more of the UI elements of a complex Flex interface. The other tools I tried weren’t picking all those elements, but I must admit that I didn’t investigate further.

An interesting feature is FlexPilot’s chain syntax, that allows to access flex UI elements by different properties, like id, label or text. Another feature I liked is easier integration to work with selenium RC and selenium IDE.

Below some usage examples to create automated tests for MyAppInFlex.swf, and also serve as a quick reference on how to interact/access the flex UI.

Click on a Button

The easiest way to access an element is using the element’s id, for example click on a button:
selenium.flexClick("id=MyAppInFlex", "chain=id:okButton");

If the button you are trying to access doesn't have an id, you are able to find it using the label property:

selenium.flexClick("id=MyAppInFlex", "chain="id:buttonBar/label:Search");

Access an element on a Grid

Click an element that has an specific value on a Grid
selenium.flexClick("id=MyAppInFlex", "chain="id:userGrid/name:AdvancedListBaseContentHolder*/name:AdvancedListBaseContentHolder*/text:Maria Marcano");


Check the value on a TextInput

Check a textbox has a specific value
selenium.flexAssertProperty("id=MyAppInFlex", "chain="id:emailTextBox, validator=text|emailvalue@domain.com", );

To improve the application testability, add id’s to the objects you want to access. This also applies for web application in general (accessing elements by id’s is faster than processing xpath expressions).

Some limitations

Elements on a grid can’t be accessed by position, for example click on the first/second element on a grid.

I’ve been experiencing some issues with some application builds that prevent the use of the flex explorer and recorder (reported the bug, hopefully it gets fixed soon). You can still run the tests, just not using those tools to capture the elements.