Skip to content

Commit

Permalink
Include Asynchronous Server Socket and RestSharp.api
Browse files Browse the repository at this point in the history
  • Loading branch information
fangdatto committed Jul 22, 2024
1 parent 07bc3c9 commit 4542757
Show file tree
Hide file tree
Showing 42 changed files with 1,887 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

env:
BUILD_CONFIG: 'Release'
SOLUTION: 'Solution1/UnitTest.sln'
SOLUTION: 'Solution/UnitTest.sln'

runs-on: ubuntu-latest
permissions:
Expand Down
20 changes: 20 additions & 0 deletions Solution/AppiumTest/AppiumTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>

<OutputType>WinExe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.4.5" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="6.0.13" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

</Project>
124 changes: 124 additions & 0 deletions Solution/AppiumTest/Remote.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System;

namespace AppiumTest
{
public class TestRemote
{
private AndroidDriver<AndroidElement> _driver;
private AndroidElement WE;
private static String XID = "//android.widget.";
private static String PID = "com.soonr.apps.go.production:id/";
private static String AID = "android:id/";
public By getBy(String txt)
{
if (txt.StartsWith("//")) return By.XPath(txt);
By by = By.XPath(XID + txt); // default xpath
if (txt.StartsWith("#")) return By.Id(PID + txt.Substring(1));
if (txt.StartsWith("$")) return By.Id(AID + txt.Substring(1));
if (txt.StartsWith("@")) return By.XPath(XID + "TextView[@text='" + txt.Substring(1) + "']"); //@Projects
if (txt.StartsWith("&"))
{
if (txt.Equals("&+")) return By.Id(PID + "fab_button");
if (txt.Equals("&UP")) return By.XPath(XID + "ImageButton[@content-desc='Navigate up']");
if (txt.Equals("&Allow")) return By.Id("com.android.permissioncontroller:id/permission_allow_button");
if (txt.Equals("&Strike")) return By.XPath(XID + "ImageButton[@content-desc='Strike Out']");
}

return by;
}
public Boolean wait4(String txt, int n)
{
try
{
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(n));
wait.Until(ExpectedConditions.ElementIsVisible(getBy(txt)));

Check warning on line 41 in Solution/AppiumTest/Remote.cs

View workflow job for this annotation

GitHub Actions / Build_and_Test

'ExpectedConditions' is obsolete: 'The ExpectedConditions implementation in the .NET bindings is deprecated and will be removed in a future release. This portion of the code has been migrated to the DotNetSeleniumExtras repository on GitHub (https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras)'
return true;
}
catch (Exception t)

Check warning on line 44 in Solution/AppiumTest/Remote.cs

View workflow job for this annotation

GitHub Actions / Build_and_Test

The variable 't' is declared but never used
{
Console.WriteLine("can not find element " + txt);
return false;
}
}
public Boolean has(String txt)
{
try
{
WE = _driver.FindElement(getBy(txt));
return WE != null;
}
catch (Exception t)

Check warning on line 57 in Solution/AppiumTest/Remote.cs

View workflow job for this annotation

GitHub Actions / Build_and_Test

The variable 't' is declared but never used
{
return false;
}
}
public String getList()
{
//return text under id:list
System.Threading.Thread.Sleep(2000);
if (!has("$list")) return "";
String list = "";
if (has("#emptyText")) return WE.GetAttribute("text");
if (wait4("#label",10))
{
foreach (AndroidElement w in _driver.FindElements(getBy("#label")))
{
String name = w.GetAttribute("text");
if (list.Length==0) list = name;
else list = list + ":" + name;
}
}
Console.WriteLine(list);
return list;
}
[OneTimeSetUp]
public void SetUp()
{
//using Appium on remote machine
var serverUri = new Uri("http://uk.35cloud.com:4723/wd/hub");
var driverOptions = new AppiumOptions();
driverOptions.AddAdditionalCapability("platformName", "Android");
driverOptions.AddAdditionalCapability("automationName", "UIAutomator2");
driverOptions.AddAdditionalCapability("udid", "emulator-5554");
driverOptions.AddAdditionalCapability("deviceName", "PXL");
driverOptions.AddAdditionalCapability("platformVersion", "10");
driverOptions.AddAdditionalCapability("app", "C:\\Auto\\a\\VFSx.apk");
driverOptions.AddAdditionalCapability("appPackage", "com.soonr.apps.go.production");
driverOptions.AddAdditionalCapability("appActivity", "com.soonr.apps.workplace.ui.HomeActivity");
// app-option
driverOptions.AddAdditionalCapability("noReset", true);
driverOptions.AddAdditionalCapability("skipDeviceInitialization", true);
driverOptions.AddAdditionalCapability("kipServerInstallation", true);

_driver = new AndroidDriver<AndroidElement>(serverUri, driverOptions, TimeSpan.FromSeconds(180));
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}

[OneTimeTearDown]
public void TearDown()
{
_driver.Dispose();
_driver.Quit();
}

[Test]
public void TestView1()
{

System.Threading.Thread.Sleep(5000);
Console.WriteLine("has[0000]=" + has("@0000"));
getList();
if(has("@0000")) WE.Click();
//if (has("@scribble.pdfauto.pdf")) WE.Click();
System.Threading.Thread.Sleep(5000);

}
}
}
133 changes: 133 additions & 0 deletions Solution/AppiumTest/Remote2x.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System;

namespace AppiumTest
{
public class TestRemote2x
{
/* Target Appium 2.x version without Workplace app setup.
* URI: http://uk.35cloud.com:4723/wd/hub
*
*/
private AndroidDriver<AndroidElement> _driver;
private AndroidElement WE;
private static String XID = "//android.widget.";
private static String PID = "com.soonr.apps.go.production:id/";
private static String AID = "android:id/";
public By getBy(String txt)
{
if (txt.StartsWith("//")) return By.XPath(txt);
By by = By.XPath(XID + txt); // default xpath
if (txt.StartsWith("#")) return By.Id(PID + txt.Substring(1));
if (txt.StartsWith("$")) return By.Id(AID + txt.Substring(1));
if (txt.StartsWith("@")) return By.XPath(XID + "TextView[@text='" + txt.Substring(1) + "']"); //@Projects
if (txt.StartsWith("&"))
{
if (txt.Equals("&+")) return By.Id(PID + "fab_button");
if (txt.Equals("&UP")) return By.XPath(XID + "ImageButton[@content-desc='Navigate up']");
if (txt.Equals("&Allow")) return By.Id("com.android.permissioncontroller:id/permission_allow_button");
if (txt.Equals("&Strike")) return By.XPath(XID + "ImageButton[@content-desc='Strike Out']");
}

return by;
}
public Boolean wait4(String txt, int n)
{
try
{
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(n));
wait.Until(ExpectedConditions.ElementIsVisible(getBy(txt)));
return true;
}
catch (Exception t)
{
Console.WriteLine("can not find element " + txt);
return false;
}
}
public Boolean has(String txt)
{
try
{
WE = _driver.FindElement(getBy(txt));
return WE != null;
}
catch (Exception t)
{
return false;
}
}
public String getList()
{
//return text under id:list
System.Threading.Thread.Sleep(2000);
if (!has("$list")) return "";
String list = "";
if (has("#emptyText")) return WE.GetAttribute("text");
if (wait4("#label",10))
{
foreach (AndroidElement w in _driver.FindElements(getBy("#label")))
{
String name = w.GetAttribute("text");
if (list.Length==0) list = name;
else list = list + ":" + name;
}
}
Console.WriteLine(list);
return list;
}
[OneTimeSetUp]
public void SetUp()
{
//using Appium on remote machine
var serverUri = new Uri("http://localhost:4723");
var driverOptions = new AppiumOptions();
driverOptions.AddAdditionalCapability("platformName", "Android");//newCommandTimeout
driverOptions.AddAdditionalCapability("appium:automationName", "UIAutomator2");
driverOptions.AddAdditionalCapability("appium:newCommandTimeout", 600);
driverOptions.AddAdditionalCapability("appium:udid", "emulator-5554");
driverOptions.AddAdditionalCapability("appium:deviceName", "PXL");
driverOptions.AddAdditionalCapability("appium:platformVersion", "10");
driverOptions.AddAdditionalCapability("appium:app", "C:\\Auto\\a\\VFSx.apk");
driverOptions.AddAdditionalCapability("appium:appPackage", "com.soonr.apps.go.production");
driverOptions.AddAdditionalCapability("appium:appActivity", "com.soonr.apps.workplace.ui.HomeActivity");
// app-option
driverOptions.AddAdditionalCapability("appium:noReset", true);
driverOptions.AddAdditionalCapability("appium:skipDeviceInitialization", true);
driverOptions.AddAdditionalCapability("appium:kipServerInstallation", true);

_driver = new AndroidDriver<AndroidElement>(serverUri, driverOptions, TimeSpan.FromSeconds(180));
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
}

[OneTimeTearDown]
public void TearDown()
{
_driver.Dispose();
_driver.Quit();
}

[Test]
public void TestView2()
{
//_driver.StartActivity("com.soonr.apps.go.production", "com.soonr.apps.workplace.ui.HomeActivity");
System.Threading.Thread.Sleep(2000);
Console.WriteLine("has[newproject]=" + has("@newproject"));
if(has("@0000")) WE.Click();
if (has("@scribble.pdfauto.pdf")) WE.Click();
System.Threading.Thread.Sleep(2000);
if(has("&UP")) WE.Click();
System.Threading.Thread.Sleep(1000);
if (has("&UP")) WE.Click();
getList();
Console.WriteLine("done");

}
}
}
37 changes: 37 additions & 0 deletions Solution/AppiumTest/SeleniumChrome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;

namespace SeleniumTest
{
[TestFixture]
public class BasicTests
{
private IWebDriver driver;

[SetUp]
public void SetUp()
{
// Initialize ChromeDriver
driver = new ChromeDriver(@"C:\AutoTest\res");

Check failure on line 17 in Solution/AppiumTest/SeleniumChrome.cs

View workflow job for this annotation

GitHub Actions / Test Report

SeleniumTest.BasicTests ► TestGoogleHomePageTitle

Failed test found in: Solution/AppiumTest/TestResults/test-results.trx Error: OpenQA.Selenium.DriverServiceNotFoundException : The file C:\AutoTest\res/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Raw output
OpenQA.Selenium.DriverServiceNotFoundException : The file C:\AutoTest\res/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
   at OpenQA.Selenium.DriverService..ctor(String servicePath, Int32 port, String driverServiceExecutableName, Uri driverServiceDownloadUrl)
   at OpenQA.Selenium.Chrome.ChromeDriverService..ctor(String executablePath, String executableFileName, Int32 port)
   at OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService(String driverPath, String driverExecutableFileName)
   at OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService(String driverPath)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory)
   at SeleniumTest.BasicTests.SetUp() in /home/runner/work/unit-test/unit-test/Solution/AppiumTest/SeleniumChrome.cs:line 17
--TearDown
   at SeleniumTest.BasicTests.TearDown() in /home/runner/work/unit-test/unit-test/Solution/AppiumTest/SeleniumChrome.cs:line 34
}

[Test]
public void TestGoogleHomePageTitle()
{
// Navigate to Google
driver.Navigate().GoToUrl("https://www.google.com");

// Assert the title
Assert.AreEqual("Google", driver.Title, "The title of the page is not as expected.");
}

[TearDown]
public void TearDown()
{
// Close the browser
driver.Quit();
}
}
}
48 changes: 48 additions & 0 deletions Solution/AppiumTest/Test2xBattery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Remote;
using System;

namespace AppiumTest
{
public class Test2xBattery
{
private AndroidDriver<AndroidElement> _driver;

[OneTimeSetUp]
public void SetUp()
{
//var serverUri = new Uri(Environment.GetEnvironmentVariable("APPIUM_HOST") ?? "http://127.0.0.1:4723/");
var serverUri = new Uri("http://127.0.0.1:4723/");
var driverOptions = new AppiumOptions();
driverOptions.AddAdditionalCapability("platformName", "Android");
driverOptions.AddAdditionalCapability("appium:automationName", "UIAutomator2");
driverOptions.AddAdditionalCapability("appium:udid", "emulator-5554");
driverOptions.AddAdditionalCapability("appium:platformVersion", "11");
driverOptions.AddAdditionalCapability("appium:appPackage", "com.android.settings");
driverOptions.AddAdditionalCapability("appium:appActivity", ".Settings");
// NoReset assumes the app com.google.android is preinstalled on the emulator
driverOptions.AddAdditionalCapability("appium:noReset", true);

_driver = new AndroidDriver<AndroidElement>(serverUri, driverOptions, TimeSpan.FromSeconds(180));
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}

[OneTimeTearDown]
public void TearDown()
{
_driver.Dispose();
_driver.Quit();
}

[Test]
public void TestBattery()
{
_driver.StartActivity("com.android.settings", ".Settings");
_driver.FindElement(By.XPath("//*[@text='Battery']")).Click();
}
}
}
Loading

0 comments on commit 4542757

Please sign in to comment.