(appium dotnet) A exception with a null response was thrown sending an HTTP request to the remote WebDriver server


#1

Hi everyone, I’m trying to test Android app, but I currently have a problem I cannot resolve.
This is the error I’m getting:

OpenQA.Selenium.WebDriverException: ‘A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://127.0.0.1:4723/wd/hub/session/590325fa-f414-47fe-bb52-be04e585cb97/elements. The status of the exception was SendFailure, and the message was: The underlying connection was closed: An unexpected error occurred on a send.’

Appium version: 1.8
Appium.WebDriver version: v3.0.0.2
Selenium.WebDriver version: 3.11
Selenium.Support version: 3.11

Here’s the code:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Service;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Service.Options;
using System.IO;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Appium;
using System.Collections.ObjectModel;
using OpenQA.Selenium;
using System.Linq;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        private static AndroidDriver<AndroidElement> _driver;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            var args = new OptionCollector().AddArguments(GeneralOptionList.PreLaunch());;
            var desiredCaps = new DesiredCapabilities();
            desiredCaps.SetCapability(MobileCapabilityType.DeviceName, "MyDevice");
            desiredCaps.SetCapability(MobileCapabilityType.PlatformName, "Android");
            desiredCaps.SetCapability(MobileCapabilityType.PlatformVersion, "8.0");
            desiredCaps.SetCapability(MobileCapabilityType.App, @"C:\Users\User\Documents\Visual Studio 2017\Projects\UnitTestProject1\UnitTestProject1\bin\Debug\Resources\WikipediaSample.apk");
            _driver = new AndroidDriver<AndroidElement>(new Uri("http://127.0.0.1:4723/wd/hub"), desiredCaps);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            if (_driver != null)
            {
                _driver.ResetApp();
            }
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            _driver.CloseApp();
        }

        [TestMethod]
        public void Test1()
        {
            AndroidElement searchElement = (AndroidElement)new WebDriverWait(_driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia"))
            );
            searchElement.Click();
            AndroidElement insertTextElement = (AndroidElement)new WebDriverWait(_driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.Id("org.wikipedia.alpha:id/search_src_text"))
            );
            insertTextElement.SendKeys("Serbia");
            System.Threading.Thread.Sleep(5000);

            ReadOnlyCollection<AndroidElement> results = _driver.FindElements(By.ClassName("android.widget.TextView"));
            Assert.IsTrue(results.ElementAt(1).Text.Equals("Serbia"));
        }

        [TestMethod]
        public void Test2()
        {
            AndroidElement searchElement = (AndroidElement)new WebDriverWait(_driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia"))
            );
            searchElement.Click();
            AndroidElement insertTextElement = (AndroidElement)new WebDriverWait(_driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.Id("org.wikipedia.alpha:id/search_src_text"))
            );
            insertTextElement.SendKeys("USA");
            System.Threading.Thread.Sleep(5000);

            ReadOnlyCollection<AndroidElement> results = _driver.FindElements(By.ClassName("android.widget.TextView"));
            Assert.IsTrue(results.ElementAt(1).Text.Equals("United States"));
        }
    }
}

Thanks in advance for any help!