How to write test case using Appium in C#? - c#

I want to use Appium to make automated testing for my Android app. I downloaded the Appium for Windows and could run my app on device from within appium.exe. Now I want to write test cases in C# which make use of selendroid integrated in Appium. I googled a lot but couldn't find any example demonstrating the same. I found one project on github but it's giving a lot of compiler errors. Could anybody guide me about how to write test cases using Appium in C#? If you have any resources, please provide me the same. Thanks.

You can go with installing Visual Studio 2012 or visual express for web. Use NUnit framework to write the test cases and execute them as a class library project in that.

1) Create a class library project and create a new class e.g "Class1". Add the packages as provided in the links above. Try the below code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using OpenQA.Selenium.Interactions;
using System.Threading;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Appium.Interfaces;
using System.Drawing;
namespace ClassLibrary2
{
[TestFixture]
public class Class1
{
public AppiumDriver driver;
public DesiredCapabilities capabilities;
public Class1()
{
Console.WriteLine("Connecting to Appium server");
capabilities = new DesiredCapabilities();
capabilities.SetCapability(CapabilityType.BrowserName, "Android");
capabilities.SetCapability(CapabilityType.Platform, "Windows");
capabilities.SetCapability(CapabilityType.Version ,"4.1.2");
capabilities.SetCapability("Device", "Android");
//Application path and configurations
driver = new AppiumDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
}
[Test]
public void login()
{
driver.FindElement(By.Name("Country")).Click();
//Your further code as per the application.
}
Then Build a solution for your project and run it in NUnit.

Related

Why does Visual Studio not complain when I write Console.WriteLine?

I was recently watching tutorials on C# where they mentioned that specifying either using System and then using Console.WriteLine or writing System.Console.WriteLine if you don't specify using <namespace> was necessary for proper namespace resolution to happen when writing programs otherwise the compilation would fail.
But when I'm using Visual Studio 2022, Console.WriteLine seems to work without specifying any namespace at all.
Is this some optimisation that Visual Studio 2022 does? Why is this working?
On top of this, if I use System.Console.WriteLine, VS 2022 also greys out System and says "name can be simplified".
Try this
using System;
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
If you create c# project of asp.netframrwork. It will automatically generate the "using System" namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
}
}
}
If your console project is .NET 6 and above. Please refer to Exploring Ideas to Build Code While Learning Using Top-Level Statements.
Hope it helps you.

Why doesn’t using a missing package cause Visual Studio to complain about my Azure Function?

I’m new to C#. In my project, I have a file which contains this code:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace MyNamespace {
public static class MyClass {
// ...
static MyClass () {
// ...
}
[FunctionName("...")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
ILogger log) {
log.LogInformation("...");
// ...
}
}
}
For all those using statements, I have a corresponding package listed under Solution explorer → Dependencies → Packages, except for Microsoft.Extensions.Logging. Now, Visual Studio doesn’t complain about this fact and the build process works just fine. Running the application, however, this error appears when it reaches the log.LogInformation("...") statement:
So, I install it from NuGet:
but doing that, I obtain a different error from the console of the Azure Function, that is:
What should I do?
Have you installed dependent packages like Microsoft.Extensions.Logging.Abstractions. Its strange by default dependent packages are installed as part of creating new Azure function. I would suggest try once again using Visual studio template of Azure function.

Sikuli error in c# with Sikuli integrator package

When I run this code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Sikuli4Net.sikuli_REST;
using Sikuli4Net.sikuli_UTIL;
using SikuliModule;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Sikuli
{
class Program
{
static void Main(string[] args)
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://www.google.co.nz/");
Thread.Sleep(2000);
String image = #"C:\Users\safa\Desktop\gmail.jpg";
SikuliAction.Click(image);
}
}
}
I got this exception:
System.ComponentModel.Win32Exception:
'The system cannot find the file specified'
I changed file location but again I got same error.
This the image that I want to click on:
Run the visual studio as administrator using below steps
Click on start(windows) button
Type Visual Studio
Mouse Right Click on Visual Studio
Click on Run as Administrator option
I think there is no mistakes in code. So try this once.
Increase the wait time to 5000ms
For this program you don't need Sikuli4Net, remove usings related to that

Where are the `HttpContext.IsWebSocketRequest` property and the `HttpContext.AcceptWebSocketRequest` method?

I'm trying to get a simple "Hello World" type WebSocket server running on my Mac using Visual studio for Mac OS X.
Where are the HttpContext.IsWebSocketRequest property and the
HttpContext.AcceptWebSocketRequest method? The documentation appears to say they are in HttpContext inside the System.Web dll but I've referenced that (as well as System.Net) and Visual studio can't find them.
Is there something I'm missing or have forgotten?
This is the code I've that's giving me problems.
using System;
using System.Web;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Collections.Generic;
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
context.AcceptWebSocketRequest(HandleWebSocket);
else
context.Response.StatusCode = 400;
}
The sample I'm following is: https://github.com/paulbatum/WebSocket-Samples/blob/master/AspNetWebSocketEcho/EchoHandler.ashx.cs
As far as I remember is not supported yet: http://go-mono.com/status/status.aspx?reference=4.5&profile=4.5&assembly=System
You can use one of the many third-party components available for Mono. I develop and maintain one of them https://github.com/vtortola/WebSocketListener

Unit test timeouts

I suspect this problem might be due to a bad configuration. The simplest unit test hangs also. Basically, whatever I put in my unit test does nothing, the test is launched and keeps loading until it reaches timeout. My initial test used a TransactionScope, DataContext and custom objects. I then simplified my unit test to the following, and it still hangs:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ManifestService;
using System.Transactions;
namespace UnitTesting
{
[TestClass]
public class PackagerUnitTest
{
[TestMethod]
public void Packager_CreatePackageType()
{
string expected = "test-package";
Assert.AreEqual("test-package", expected);
}
}
}
So, it was a configuration issue, at the visual studio level. Basically, you need Visual Studio 2010 SP1 if you want to run unit tests while having Visual Studio 2012 installed at the same time.
Thanks for the answer from this blog:
http://dorkasaurusrex.blogspot.ca/2012/11/visual-studio-2010-unit-test-hangs.html
SP1 download link:
http://www.microsoft.com/en-us/download/details.aspx?id=23691

Categories