From ddad985d0ea30b1820402206ce0a11dc13da77ce Mon Sep 17 00:00:00 2001 From: sebv Date: Fri, 18 Apr 2014 18:35:05 +0800 Subject: [PATCH] New dotnet code sample --- .../dotnet/AppiumDriverDemo/.gitignore | 4 + .../AppiumDriverDemo.csproj} | 104 ++++++++++-------- .../AppiumDriverDemo/AppiumDriverDemo.sln | 20 ++++ .../dotnet/AppiumDriverDemo/Program.cs | 12 ++ .../dotnet/AppiumDriverDemo/ProgramTest.cs | 94 ++++++++++++++++ .../Properties}/AssemblyInfo.cs | 29 ++--- .../dotnet/AppiumDriverDemo/packages.config | 7 ++ .../examples/dotnet/SimpleTest/SimpleTest.sln | 23 ---- .../dotnet/SimpleTest/SimpleTest/Main.cs | 92 ---------------- .../SimpleTest/SimpleTest/packages.config | 4 - .../SimpleTest/packages/repositories.config | 4 - 11 files changed, 206 insertions(+), 187 deletions(-) create mode 100644 sample-code/examples/dotnet/AppiumDriverDemo/.gitignore rename sample-code/examples/dotnet/{SimpleTest/SimpleTest/SimpleTest.csproj => AppiumDriverDemo/AppiumDriverDemo.csproj} (53%) mode change 100755 => 100644 create mode 100644 sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.sln create mode 100644 sample-code/examples/dotnet/AppiumDriverDemo/Program.cs create mode 100644 sample-code/examples/dotnet/AppiumDriverDemo/ProgramTest.cs rename sample-code/examples/dotnet/{SimpleTest/SimpleTest => AppiumDriverDemo/Properties}/AssemblyInfo.cs (60%) mode change 100755 => 100644 create mode 100644 sample-code/examples/dotnet/AppiumDriverDemo/packages.config delete mode 100755 sample-code/examples/dotnet/SimpleTest/SimpleTest.sln delete mode 100755 sample-code/examples/dotnet/SimpleTest/SimpleTest/Main.cs delete mode 100755 sample-code/examples/dotnet/SimpleTest/SimpleTest/packages.config delete mode 100755 sample-code/examples/dotnet/SimpleTest/packages/repositories.config diff --git a/sample-code/examples/dotnet/AppiumDriverDemo/.gitignore b/sample-code/examples/dotnet/AppiumDriverDemo/.gitignore new file mode 100644 index 000000000..9ebf3446f --- /dev/null +++ b/sample-code/examples/dotnet/AppiumDriverDemo/.gitignore @@ -0,0 +1,4 @@ +bin/ +obj/ +packages/ + diff --git a/sample-code/examples/dotnet/SimpleTest/SimpleTest/SimpleTest.csproj b/sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.csproj old mode 100755 new mode 100644 similarity index 53% rename from sample-code/examples/dotnet/SimpleTest/SimpleTest/SimpleTest.csproj rename to sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.csproj index 1c30e64c6..2e50b216c --- a/sample-code/examples/dotnet/SimpleTest/SimpleTest/SimpleTest.csproj +++ b/sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.csproj @@ -1,48 +1,58 @@ - - - - Debug - x86 - 9.0.21022 - 2.0 - {3C18D9A2-C814-408E-932F-00577CB8491B} - Exe - SimpleTest - SimpleTest - - - True - full - False - bin\Debug - DEBUG; - prompt - 4 - x86 - True - - - none - True - bin\Release - prompt - 4 - x86 - True - - - - - - ..\packages\Selenium.WebDriver.2.31.2\lib\net40\WebDriver.dll - - - - - - - - - - + + + + Debug + x86 + 10.0.0 + 2.0 + {7ED8B080-7820-4D7A-95D2-B53F55028F59} + Exe + AppiumDriverDemo + AppiumDriverDemo + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + packages\Appium.WebDriver.0.1\lib\net40\appium-dotnet-driver.dll + + + packages\Selenium.WebDriver.2.41.0\lib\net40\WebDriver.dll + + + + packages\Newtonsoft.Json.6.0.2\lib\net40\Newtonsoft.Json.dll + + + packages\NUnit.2.6.3\lib\nunit.framework.dll + + + + + + + + + + + \ No newline at end of file diff --git a/sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.sln b/sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.sln new file mode 100644 index 000000000..3fd5f5ad0 --- /dev/null +++ b/sample-code/examples/dotnet/AppiumDriverDemo/AppiumDriverDemo.sln @@ -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 diff --git a/sample-code/examples/dotnet/AppiumDriverDemo/Program.cs b/sample-code/examples/dotnet/AppiumDriverDemo/Program.cs new file mode 100644 index 000000000..a9445a60d --- /dev/null +++ b/sample-code/examples/dotnet/AppiumDriverDemo/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace AppiumDriverDemo +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Run the unit tests!"); + } + } +} diff --git a/sample-code/examples/dotnet/AppiumDriverDemo/ProgramTest.cs b/sample-code/examples/dotnet/AppiumDriverDemo/ProgramTest.cs new file mode 100644 index 000000000..ff9fd61c5 --- /dev/null +++ b/sample-code/examples/dotnet/AppiumDriverDemo/ProgramTest.cs @@ -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 addends = new List(); + 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")); + } + /// retrieves the path of the locally installed app + /// the path to the Test App + 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"; + } + + } + +} + diff --git a/sample-code/examples/dotnet/SimpleTest/SimpleTest/AssemblyInfo.cs b/sample-code/examples/dotnet/AppiumDriverDemo/Properties/AssemblyInfo.cs old mode 100755 new mode 100644 similarity index 60% rename from sample-code/examples/dotnet/SimpleTest/SimpleTest/AssemblyInfo.cs rename to sample-code/examples/dotnet/AppiumDriverDemo/Properties/AssemblyInfo.cs index dbbaed68d..b4a4f289a --- a/sample-code/examples/dotnet/SimpleTest/SimpleTest/AssemblyInfo.cs +++ b/sample-code/examples/dotnet/AppiumDriverDemo/Properties/AssemblyInfo.cs @@ -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("")] diff --git a/sample-code/examples/dotnet/AppiumDriverDemo/packages.config b/sample-code/examples/dotnet/AppiumDriverDemo/packages.config new file mode 100644 index 000000000..9acbe5389 --- /dev/null +++ b/sample-code/examples/dotnet/AppiumDriverDemo/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sample-code/examples/dotnet/SimpleTest/SimpleTest.sln b/sample-code/examples/dotnet/SimpleTest/SimpleTest.sln deleted file mode 100755 index 8d7f1d86e..000000000 --- a/sample-code/examples/dotnet/SimpleTest/SimpleTest.sln +++ /dev/null @@ -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 diff --git a/sample-code/examples/dotnet/SimpleTest/SimpleTest/Main.cs b/sample-code/examples/dotnet/SimpleTest/SimpleTest/Main.cs deleted file mode 100755 index 4eb54c3ed..000000000 --- a/sample-code/examples/dotnet/SimpleTest/SimpleTest/Main.cs +++ /dev/null @@ -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 addends = new List(); - 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(); - } - - /// uses spotlight to find the path to the compiled test app - /// the path to the Test App - 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; - } - - /// logs statements in green if they pass and red if they fail - /// true if the message is related to a test passing, false otherwise - /// message to log - 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; - } - } -} diff --git a/sample-code/examples/dotnet/SimpleTest/SimpleTest/packages.config b/sample-code/examples/dotnet/SimpleTest/SimpleTest/packages.config deleted file mode 100755 index 4aa54e947..000000000 --- a/sample-code/examples/dotnet/SimpleTest/SimpleTest/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/sample-code/examples/dotnet/SimpleTest/packages/repositories.config b/sample-code/examples/dotnet/SimpleTest/packages/repositories.config deleted file mode 100755 index fea860567..000000000 --- a/sample-code/examples/dotnet/SimpleTest/packages/repositories.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file