ERROR: The requested URL could not be retrieved Appium - c#

I am trying to creat an Appium framework to test a real android device plugged into my computer.
TestBrowser.cs
[TestClass]
public class UnitTest1
{
//create instance for appium driver
AppiumDriver<AndroidElement> _driver;
[TestMethod]
public void TestBrowser()
{
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("deviceName", "Xiaomi MI 5");
cap.SetCapability("platformVersion", "8.0.0");
cap.SetCapability("platformName", "Android");
cap.SetCapability("udid", "6d8d73n2");
cap.SetCapability("fullReset", "True");
cap.SetCapability(MobileCapabilityType.App, "Browser");
_driver = new AndroidDriver<AndroidElement>(new Uri("http://0.0.0.0:4723/wd/hub"), cap);
_driver.Navigate().GoToUrl("http://www.google.co.uk");
_driver.FindElementByName("q").SendKeys("Appium");
_driver.FindElementByName("q").SendKeys(Keys.Enter);
}
}
When I run my test I am getting the following error:
ERROR: The requested URL could not be retrieved
It seems to error out at
_driver = new AndroidDriver<AndroidElement>(new Uri("http://0.0.0.0:4732/wd/hub"), cap);
This is new to me and I'm just trying to set up a basic test to build upon.

Stupid answer, but maybe You miss-typed your port, default is 4723 not 4732.
_driver = new AndroidDriver<AndroidElement>(new Uri("http://0.0.0.0:4723/wd/hub"),
I know it is same, but try also:
http://127.0.0.1:4723/wd/hub
Hope this helps,

Related

Cant Launch a window with Selenium Chrome Driver c#

Environment:
Vs2019, C#
NuGet Package: Selenium v3.141.0 by Selenium Committee.
Chrome Driver from Selenium website - v92.0
IWebDriver Driver = new ChromeDriver("FolderPath"); //time out error here.
Driver.url = "www.google.com"
with no other code, I can't get pass declaring Chrome Driver. I get a time out error with local host.
I tried:
setting a different port.
adding "no-sandbox" to arguments.
I would try utilizing the ChromeDriver NuGet package instead of pointing to a local file location
https://www.nuget.org/packages/Selenium.WebDriver.ChromeDriver/
Here is a simple example for you to reference
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace ExampleDemo
{
[TestFixture]
public class Chrome_test
{
private IWebDriver driver;
[Test(Description="Go To Google")]
public void GoToGoogle() {
homeURL = https://www.google.com/;
driver.Navigate().GoToUrl(homeURL);
}
[TearDown]
public void TearDownTest()
{
driver.Close();
}
[SetUp]
public void SetupTest()
{
driver = new ChromeDriver();
}
}
}
I need to add a chrome option:
ChromeOptions options = new ChromeOptions()
options.add("--remote-debugging-port=9222 ") // change port if necessary

Run Selenium Grid C# Project in Visual Studio

I'm implemented Nunit selenium C# testing in visual studio (Console Application n Class Library). My project in visual studio is console application. I started the selenium grid using
java -Dwebdriver.gecko.driver="..\jar\geckodriver.exe" -Dwebdriver.chrome.driver="..\jar\chromedriver.exe" -Dwebdriver.ie.driver="..\jar\IEDriverServer.exe" -jar ..\jar\selenium-server-standalone-3.14.0.jar -role hub -port 4444
Code:
using Automation_Framework.Manager;
using NUnit.Framework;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;
namespace Automation_Framework.TestManager
{
[TestFixture]
class ChromeTestManager
{
private WebDriverManager webDriverManager;
private IWebDriver driver;
public ChromeTestManager()
{
webDriverManager = new WebDriverManager();
}
[SetUp]
public void setup()
{
webDriverManager.createDriver("chrome");
driver = webDriverManager.getDriver();
}
[Test]
public void test()
{
driver.Url = "http://www.google.com.my";
driver.Navigate();
}
[TearDown]
public void shutdown()
{
driver.Close();
}
}
}
I had tried execute using Test Explorer but it does not open any browser. I"m following this tutorial.
Questions:
How to run the project with browser open and see all actions?
How to run using Nunit-console-runner.
Please help me. Thanks.
I assume that:
1. You have tried your code locally and your test is opening the browser when you run it on your machine without the grid.
2. Your nodes are set up and registered with the hub.
You need to:
1. Use RemoteWebDriver:
var uri = 'uri_to_your_grid_hub';
var capabilities = new ChromeOptions().ToCapabilities();
var commandTimeout = TimeSpan.FromMinutes(5);
var driver = new RemoteWebDriver(new Uri(uri),capabilities,commandTimeout)
Add the attribute to a class: [Parallelizable(ParallelScope.Self)] in order to run your tests in parallel with other test classes.
In order to verify whether the hub is running, open the browser and navigate to http://localhost:4444 on the hub machine.
Sources:
How can I run NUnit(Selenium Grid) tests in parallel?
Selenium Grid in C#
Useful C# WebDriver examples
Selenium Grid set up
I haven't used grid in .Net but here my answer:
your command is just register a hub, which needs to keep running (open a browser and test it is working)
you need to register your nodes under that hub (different ports) (open a browser and test it is working)
in your code, you should use "RemoteWebDriver" to connect to the hub.
something along these lines (it is in java but I hope it gives you a starting point)
public class Gmail
{
public WebDriver driver=null;
#Parameters("browser") //testng.xml
#Test()
public void GmailTest(String browser)
{
System.out.println("Gmail " + browser);
// RemoteWebdriver
DesiredCapabilities cap = null;
if(browser.equals("firefox")){
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
}else if (browser.equals("iexplore")){
cap = DesiredCapabilities.internetExplorer();
cap.setBrowserName("iexplore");
cap.setPlatform(Platform.WINDOWS);
}
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
driver.get("http://gmail.com");
driver.findElement(By.id("Email")).sendKeys("abcd");
driver.quit();
}
I hope this helps.good luck

C# Appium AppiumOptions error The capabilities platform name are standard capabilities and should not have the appium prefix

I am writing a first test in Appium using AndroidDriver with C#. When I write this line
driver = new AndroidDriver<AndroidElement>(url, options);
I get the error:
OpenQA.Selenium.WebDriverException: 'The capabilities ["PlatformName"] are standard capabilities and
should not have the "appium" prefix'
My code snippet is:
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using OpenQA.Selenium.Appium;
namespace AppiumCTest1
{
[TestClass]
public class TC1
{
AndroidDriver<AndroidElement> driver;
DesiredCapabilities cap;
[TestMethod]
public void TestBrowser()
{
AndroidDriver<AndroidElement> driver;
AppiumOptions options = new AppiumOptions();
options.PlatformName = "Android";
options.AddAdditionalCapability("deviceName", "Pixel API 29");
options.AddAdditionalCapability("PlatformName", "Android");
options.AddAdditionalCapability("Udid", "169.254.138.177:5555");
options.AddAdditionalCapability("fullRest", "True");
options.AddAdditionalCapability("appPackage", "MyPackage");
options.AddAdditionalCapability("appActivity", "MyActivity");
Uri url = new Uri("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver<AndroidElement>(url, options);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.Navigate().GoToUrl("http://www.bing.com");
}
}
}
Some help appreciated to solve this issue. Thanks
Currently making some experiments with Appium, for me this code working to driver initiliazing:
private AndroidDriver<AppiumWebElement> _driver;
private AppiumLocalService _appiumLocalService;
[SetUp]
public void Setup()
{
DriverOptions caps = new AppiumOptions();
caps.AddAdditionalCapability(MobileCapabilityType.DeviceName, "R32CC02AGJK");
caps.AddAdditionalCapability("appPackage", "com.android.calculator2");
caps.AddAdditionalCapability("appActivity", "com.android.calculator2.Calculator");
caps.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
caps.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, "5.1.1");
caps.AddAdditionalCapability(MobileCapabilityType.AutomationName, "UiAutomator2");
_driver = new AndroidDriver<AppiumWebElement>(
new Uri("http://127.0.0.1:4723/wd/hub"), caps);
And, this line in your code:
options.AddAdditionalCapability("fullRest", "True");
, is this correct? Maybe fullReset?
Here you also can find list with all capabilities.

Desired capabilities depreciated, how to get it appium working with Android driver?

I'm currently trying to start a automation project with Appium/Selenium and noticed that DesiredCapabilities are depreciated. Does anyone have a working example of a workaround or new implementation of this?
using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Remote;
namespace LeaflyAutomation
{
public class TC1
{
public AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL(sc_url), caps);
public void InitDriver()
{
//No longer valid, need new driver setup
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("PlatformName", "Android");
caps.SetCapability("appPackage"), "myapp file path");
caps.SetCapability("appActivity", "com.demo.demofolder.activities.MainActivity");
}
}
}
you are importing the wrong library for DesiredCapabilities.
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class TC1
{
public static AppiumDriver<MobileElement> driver;
public void InitDriver()
{
DesiredCapabilities caps=new DesiredCapabilities();
caps.setCapability("appPackage", "your app package name");
caps.setCapability("appActivity", "your app activity name");
caps.setCapability("deviceName", "Android phone");
caps.setCapability("platform","Android");
driver = new AndroidDriver<MobileElement>(new URL(
"http://127.0.0.1:4723/wd/hub"), caps);
}
}
For more info visit Step by Step Appium Tutorial for Beginners
DesiredCapabilities are depreciated in latest version , we can use AppiumOptions to pass device detail instead of DesiredCapabilities.
AppiumOptions capabilities = new AppiumOptions();
capabilities.AddAdditionalCapability("platformName", "Android");
capabilities.AddAdditionalCapability("app", "APK_Path");
capabilities.AddAdditionalCapability("udid", "Device_ID");
capabilities.AddAdditionalCapability("deviceName", "Android");
capabilities.AddAdditionalCapability("noReset", false);
driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);

Appium C# AppiumDriver<AppiumWebElement>.DeviceTime does not work

How to get current time on real device (android) using selenium C#
I have tried AppiumDriver.DeviceTime
I got error:
Message: OpenQA.Selenium.WebDriverException : Unexpected error. That URL did not map to a valid JSONWP resource
I'm using Visual Studio Community 2017
//C# Appium getting time
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
namespace YourNameSpace
{
[TestClass]
public class UnitTest1
{
//Creating instance for Appium driver
AppiumDriver<AndroidElement> _driver;
[TestMethod]
public void MainScreen()
{
//set the capabilities
(https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md)
DesiredCapabilities cap = new DesiredCapabilities();
//remember to put here all your DesiredCapabilities
_driver = new AndroidDriver<AndroidElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap);
String deviceDateX = _driver.DeviceTime; //Geting device date and time.
Console.WriteLine(deviceDateX); //Writing the date and time in console
_driver.Quit();
}
}
}
I Hope this helps you. :)

Categories

Resources