Sunday, January 13, 2013

WebElement Form Extensions for WebDriver

I really like Selenium 2.0!

I've been working a lot on a side project involving WebDriver, and thus I have been creating a few extension methods for working with form data that I thought others might find helpful:

  • bool IsSelected(this IWebElement element)
  • bool IsChecked(this IWebElement element)
  • void SetChecked(this IWebElement element)
  • void SetUnchecked(this IWebElement element)
  • IWebElement GetOptionByValue(this IWebElement element, string value)
  • IWebElement GetOptionByText(this IWebElement element, string text)

Extension Methods

public static class WebElementExtensions
{
    private static readonly StringComparer DefaultComparer =
        StringComparer.InvariantCultureIgnoreCase;
 
    private static readonly string[] Selected = new[] { "true", "selected" };
 
    public static bool IsSelected(this IWebElement element)
    {
        var attribute = element.GetAttribute("selected");
        return Selected.Contains(attribute, DefaultComparer);
    }
 
    private static readonly string[] Checked = new[] { "true", "checked" };
 
    public static bool IsChecked(this IWebElement element)
    {
        var attribute = element.GetAttribute("checked");
        return Checked.Contains(attribute, DefaultComparer);
    }
 
    public static void SetChecked(this IWebElement element)
    {
        if (!element.IsChecked())
            element.Click();
    }
 
    public static void SetUnchecked(this IWebElement element)
    {
        if (element.IsChecked())
            element.Click();
    }
 
    private const StringComparison DefaultComparison = 
        StringComparison.InvariantCultureIgnoreCase;
 
    public static IWebElement GetOptionByValue(
        this IWebElement element, 
        string value)
    {
        return element.GetOption(
            o => value.Equals(o.GetAttribute("value"), DefaultComparison));
    }
 
    public static IWebElement GetOptionByText(
        this IWebElement element, 
        string text)
    {
        return element.GetOption(o => text.Equals(o.Text, DefaultComparison));
    }
 
    private static IWebElement GetOption(
        this IWebElement element, 
        Func<IWebElement, bool> predicate)
    {
        return element
            .FindElements(By.CssSelector("option"))
            .FirstOrDefault(predicate);
    }
}

Unit Tests

public class WebElementExtensionTests : IDisposable
{
    public IWebDriver Driver { get; private set; }
 
    public WebElementExtensionTests()
    {
        Driver = new ChromeDriver("C:/Code/Drivers");
        Driver.Url = "file:///C:/Code/WebDriverTests/TestPage.htm";
    }
 
    public void Dispose()
    {
        Driver.Dispose();
    }
 
    [Fact]
    public void RadioTests()
    {
        var e1 = Driver.FindElement(By.Id("r1"));
        var e2 = Driver.FindElement(By.Id("r2"));
 
        IsNotSelected(e1);
        IsSelected(e2);
 
        e1.Click();
        IsSelected(e1);
 
        e2.Click();
        IsNotSelected(e1);
    }
 
    protected void IsSelected(IWebElement element)
    {
        Assert.True(element.IsSelected());
    }
 
    protected void IsNotSelected(IWebElement element)
    {
        Assert.False(element.IsSelected());
    }
 
    [Fact]
    public void CheckboxTests()
    {
        var e1 = Driver.FindElement(By.Id("c1"));
        var e2 = Driver.FindElement(By.Id("c2"));
 
        IsNotChecked(e1);
        IsChecked(e2);
 
        e1.Click();
        IsChecked(e1);
 
        e1.Click();
        IsNotChecked(e1);
    }
 
    protected void IsChecked(IWebElement element)
    {
        Assert.True(element.IsChecked());
    }
 
    protected void IsNotChecked(IWebElement element)
    {
        Assert.False(element.IsChecked());
    }
 
    [Fact]
    public void OptionTests()
    {
        var e1 = Driver.FindElement(By.Id("s1"));
 
        var option1 = e1.GetOptionByValue("a");
        var option2 = e1.GetOptionByValue("b");
 
        IsNotSelected(option1);
        IsSelected(option2);
 
        option1.Click();
 
        IsSelected(option1);
        IsNotSelected(option2);
    }
 
    [Fact]
    public void ToggleCheckedTests()
    {
        var e1 = Driver.FindElement(By.Id("c1"));
        IsNotChecked(e1);
 
        e1.SetChecked();
        IsChecked(e1);
 
        e1.SetChecked();
        IsChecked(e1);
 
        e1.SetUnchecked();
        IsNotChecked(e1);
 
        e1.SetUnchecked();
        IsNotChecked(e1);
    }
}

TestPage.htm

<html> <body> <p> <select id="s1"> <option value="a">Ant</option> <option value="b" selected="selected">Bird</option> </select> </p> <p> <input id="r1" type="radio" name="r" value="c" />Cat<br> <input id="r2" type="radio" name="r" value="d" checked="checked" />Dog </p> <p> <input id="c1" type="checkbox" name="vehicle" value="e" />Elephant<br> <input id="c2" type="checkbox" name="vehicle" value="f" checked="checked" />Fox </p> </body> </html>
Shout it

Enjoy,
Tom

Thursday, January 3, 2013

How To: Play GOG Games on Android with DosBox Turbo

Good Old Games

If you like classic games, then GOG is the online game retailer for you. Not only do they have a great selection of retro and modern games, but best of all they are provided DRM free.

Many of the old DOS games that GOG sells come preconfigured to run DOSBox behind the scenes. For those of you not familiar with DOSBox, it is the leading open source DOS emulator.

This is great because GOG can offer the original game experience without having to modify any of the game files or their content. Also, this allows users to run those same games on any platform that has a port of DOSBox available!

DosBox Turbo for Android

There are three DOS emulators available on Android, the best of which (by far) is DosBox Turbo. It costs approximately $3 dollars, but I assure you that it is worth every penny. It is feature rich, stable, and fast.

DosBox Turbo on the Google Play Store

Additionally there is a free app called DosBox Manager that helps manage your DosBox configuration profiles for game or program that you wish to run. This is extremely useful and I strongly suggest that you get it, especially because (as mentioned before) it is free!

DosBox Manager on the Google Play Store

Installation & Configuration

  1. Install Android Applications

Simply install the two applications, DosBox Turbo amd DosBox Manager, from the Google Play store.

  1. Get GOG Games

Purchase, download, and install your desired GOG games onto your computer. Not all GOG games use DOSBox, thus not all GOG games will run on Android; here is a list of games GOG games that use DOSBox.

  1. Copy GOG Games to Android

Connect your android device to your computer, create a DOS folder at the root of your storage, and then copy your GOG games to there. Note: You do not have to copy the DOSBox folder.

Here is an example of where I installed Master of Orion on my computer, and which folders I copied to my ASUS Transformer tablet.

  1. Configure DosBox Manager

If you are using DosBox Manager, then you will never have to launch the DosBox Turbo application directly. Once your games are copied over, launch DosBox Manager so that you can create profiles for your games.

  1. Copy DosBox Profile

Copy the "Default" DosBox configuration by clicking and holding down on the icon. When prompted, select "Copy Profile" and enter the new name of your game; in my example this would be "Master of Orion".

  1. Configure DosBox Profile

This is by far the most complicated step. Click and hold down on the new configuration file, and when prompted select "Edit Configuration". Each game configuration could be different, but there are at least 2 or 3 steps that will always need to be updated.

First, scroll down in the configuration and enable "Screen Scaling". This will allow your screen resolution to scale up to the full screen size of your Android device.

Second, for a game like Master of Orion, I would suggest setting your "Mouse Tracking" option to "Absolute (experimental)". This will allow you to click on the screen like a touch screen instead of moving the mouse around like a touch pad. Note: This may not be ideal for all gaming experiences.

Third, and this is the most important step, we must edit the Autoexec to launch your game! Select the "DosBox Settings" sub-menu, and then select "Autoexec". Here we can enter a series of commands that will be automatically executed whenever this DosBox profile launches. Here are the commands to launch Master of Orion from the directory that I copied it's files to earlier:

mount c: /storage/sdcard0/
c:
cd: dos/orion
orion.exe

That's it, you are done! Click "OK" and back out of the menu until you return to the DosBox Manager home screen.

 

  1. Set DosBox Profile Icon

This step is completely optional, but I like to set Profile Icons for my games. Click and hold down again on your game profile, select "Change Icon", and then navigate to and select whichever image you would like to be your profile icon.

  1. Launch Your Game

You are now ready to play GOG games on your Android device. Simply click on the game Profile that you would like to play, and have fun!

 

Enjoy,
Tom

Real Time Web Analytics