New dotnet code sample

This commit is contained in:
sebv
2014-04-18 18:35:05 +08:00
parent 9e97f76039
commit ddad985d0e
11 changed files with 206 additions and 187 deletions

View File

@@ -0,0 +1,4 @@
bin/
obj/
packages/

View File

@@ -1,48 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3C18D9A2-C814-408E-932F-00577CB8491B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>SimpleTest</RootNamespace>
<AssemblyName>SimpleTest</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="WebDriver">
<HintPath>..\packages\Selenium.WebDriver.2.31.2\lib\net40\WebDriver.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{7ED8B080-7820-4D7A-95D2-B53F55028F59}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>AppiumDriverDemo</RootNamespace>
<AssemblyName>AppiumDriverDemo</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="appium-dotnet-driver">
<HintPath>packages\Appium.WebDriver.0.1\lib\net40\appium-dotnet-driver.dll</HintPath>
</Reference>
<Reference Include="WebDriver">
<HintPath>packages\Selenium.WebDriver.2.41.0\lib\net40\WebDriver.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="Newtonsoft.Json">
<HintPath>packages\Newtonsoft.Json.6.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProgramTest.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppiumDriverDemo", "AppiumDriverDemo.csproj", "{7ED8B080-7820-4D7A-95D2-B53F55028F59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7ED8B080-7820-4D7A-95D2-B53F55028F59}.Debug|x86.ActiveCfg = Debug|x86
{7ED8B080-7820-4D7A-95D2-B53F55028F59}.Debug|x86.Build.0 = Debug|x86
{7ED8B080-7820-4D7A-95D2-B53F55028F59}.Release|x86.ActiveCfg = Release|x86
{7ED8B080-7820-4D7A-95D2-B53F55028F59}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = AppiumDriverDemo.csproj
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,12 @@
using System;
namespace AppiumDriverDemo
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Run the unit tests!");
}
}
}

View File

@@ -0,0 +1,94 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Text.RegularExpressions;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium;
namespace AppiumDriverDemo
{
[TestFixture ()]
public class ProgramTest
{
private AppiumDriver driver;
[TestFixtureSetUp]
public void beforeAll(){
// find the test application
Console.WriteLine("Finding Test App");
string appPath = _GetTestAppPath();
Console.WriteLine("Using Test App @ \"" + appPath + "\"");
// set up the remote web driver
Console.WriteLine("Connecting to Appium server");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("device", "iPhone Simulator");
capabilities.SetCapability("deviceName", "iPhone Retina (4-inch 64-bit)");
capabilities.SetCapability("platform", "ios");
capabilities.SetCapability("version", "7.1");
capabilities.SetCapability("app", appPath);
driver = new AppiumDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
}
[TestFixtureTearDown]
public void afterAll(){
// shutdown
driver.Quit();
}
[Test ()]
public void RegularWebdriverMethodsTestCase ()
{
// enter random numbers in all text fields
Console.WriteLine("Entering addends");
List<int> addends = new List<int>();
Random randomNumberGenerator = new Random();
var elements = driver.FindElementsByTagName("textField");
foreach(var element in elements)
{
int randomNumber = randomNumberGenerator.Next(0,10);
element.SendKeys(randomNumber.ToString());
addends.Add(randomNumber);
}
// calculate the expected result
int expectedResult = 0;
foreach(int i in addends)
expectedResult += i;
Console.WriteLine("Submitting the form");
// submit for computation
var buttons = driver.FindElementsByTagName("button");
buttons[0].Click();
// validate the computation
var staticTexts = driver.FindElementsByTagName("staticText");
int actualResult = int.Parse(staticTexts[0].Text);
Assert.AreEqual (actualResult.ToString(), expectedResult.ToString());
}
[Test ()]
public void AppiumDriverMethodsTestCase ()
{
// Using appium extension methods
AppiumWebElement el = (AppiumWebElement) driver.FindElementByIosUIAutomation(".elements()");
el.SetImmediateValue ("abc");
Assert.False (driver.IsAppInstalled("RamdomApp"));
}
/// <summary>retrieves the path of the locally installed app</summary>
/// <returns>the path to the Test App</returns>
private static string _GetTestAppPath()
{
string appiumDir = Regex.Replace(System.Reflection.Assembly.GetExecutingAssembly().Location, "/appium/.*$", "/appium");
return appiumDir + "/sample-code/apps/TestApp/build/Release-iphonesimulator/TestApp.app";
}
}
}

View File

@@ -1,27 +1,22 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("SimpleTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("danc")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyTitle ("AppiumDriverDemo")]
[assembly: AssemblyDescription ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("")]
[assembly: AssemblyCopyright ("baba")]
[assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture ("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
[assembly: AssemblyVersion ("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Appium.WebDriver" version="0.1" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.2" targetFramework="net40" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="Selenium.WebDriver" version="2.41.0" targetFramework="net40" />
</packages>

View File

@@ -1,23 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleTest", "SimpleTest\SimpleTest.csproj", "{3C18D9A2-C814-408E-932F-00577CB8491B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3C18D9A2-C814-408E-932F-00577CB8491B}.Debug|x86.ActiveCfg = Debug|x86
{3C18D9A2-C814-408E-932F-00577CB8491B}.Debug|x86.Build.0 = Debug|x86
{3C18D9A2-C814-408E-932F-00577CB8491B}.Release|x86.ActiveCfg = Release|x86
{3C18D9A2-C814-408E-932F-00577CB8491B}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = SimpleTest\SimpleTest.csproj
EndGlobalSection
EndGlobal

View File

@@ -1,92 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Remote;
namespace SimpleTest
{
class MainClass
{
public static void Main (string[] args)
{
// find the test application
Console.WriteLine("Finding Test App");
string appPath = _GetTestAppPath();
Console.WriteLine("Using Test App @ \"" + appPath + "\"");
// set up the remote web driver
Console.WriteLine("Connecting to Appium server");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("browserName", "");
capabilities.SetCapability("platform", "MAC");
capabilities.SetCapability("version", "6.1");
capabilities.SetCapability("app", appPath);
RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
// enter random numbers in all text fields
Console.WriteLine("Entering addends");
List<int> addends = new List<int>();
Random randomNumberGenerator = new Random();
var elements = driver.FindElementsByTagName("textField");
foreach(var element in elements)
{
int randomNumber = randomNumberGenerator.Next(0,10);
element.SendKeys(randomNumber.ToString());
addends.Add(randomNumber);
}
// calculate the expected result
int expectedResult = 0;
foreach(int i in addends)
expectedResult += i;
Console.WriteLine("Submitting the form");
// submit for computation
var buttons = driver.FindElementsByTagName("button");
buttons[0].Click();
// validate the computation
var staticTexts = driver.FindElementsByTagName("staticText");
int actualResult = int.Parse(staticTexts[0].Text);
bool pass = expectedResult == actualResult;
_LogWithColor(pass, "EXPECTED: " + expectedResult.ToString() + " ACTUAL: " + actualResult.ToString());
// shutdown
driver.Quit();
}
/// <summary>uses spotlight to find the path to the compiled test app</summary>
/// <returns>the path to the Test App</returns>
private static string _GetTestAppPath()
{
ProcessStartInfo appFinderStartInfo = new ProcessStartInfo("/usr/bin/mdfind", "-name \"TestApp.app\"");
appFinderStartInfo.RedirectStandardOutput = true;
appFinderStartInfo.UseShellExecute = false;
Process appFinder = Process.Start(appFinderStartInfo);
appFinder.WaitForExit();
string appPath = null;
foreach(string path in appFinder.StandardOutput.ReadToEnd().Split(new char[] {'\n','\r'}))
{
if (!path.Trim().EndsWith(".dSYM") && path.ToLower().Contains("simulator"))
{
appPath = path.Trim();
break;
}
}
return appPath;
}
/// <summary>logs statements in green if they pass and red if they fail</summary>
/// <param name="pass"><c>true<c/c> if the message is related to a test passing, <c>false</c> otherwise</param>
/// <param name="message">message to log</param>
private static void _LogWithColor(bool pass, string message)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = pass ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = originalColor;
}
}
}

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Selenium.WebDriver" version="2.31.2" targetFramework="net40" />
</packages>

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\SimpleTest\packages.config" />
</repositories>