Updating from Selenium.Webdriver 2.44 to 2.46 causes NullReferenceException

955 views Asked by At

We have just started working with Appium in my company, using it to automate testing on websites and webapps.

Testing Framework = Nunit 2.6.4
Language used     = C#
Mobile Device     = Samsung Galaxy S4
Android version   = 5.0.1

I've used Selenium and Nunit to test on Desktop before on a simple website, using [Test], [TestCase] and [TestCaseSource] attributes in my tests.

Following the advice in the article of the answer to: How to integrate Appium with C#?

(Quicker article link here: http://blogs.technet.com/b/antino/archive/2014/09/22/how-to-set-up-a-basic-working-appium-test-environment.aspx)

An associate set up a solution that would do a simple navigation to StackOverflow, click a link and assert:

namespace AppiumTests
{
using System;
using NUnit.Framework;
using AppiumTests.Helpers;
using AppiumTest.Framework;
using OpenQA.Selenium; /* Appium is based on Selenium, we need to include it */
using OpenQA.Selenium.Appium; /* This is Appium */
using OpenQA.Selenium.Appium.Interfaces; /* Not needed for commands shown here. It might be needed in single tests for automation */
using OpenQA.Selenium.Appium.MultiTouch; /* Not needed for commands shown here. It might be needed in single tests for automation */
using OpenQA.Selenium.Interactions; /* Not needed for commands shown here. It might be needed in single tests for automation */
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Android;

[TestFixture]
public class AndroidAppiumTestSuite
{
    private AppiumDriver driver;

    private static Uri testServerAddress = new Uri(TestServers.WindowsServer);
    private static TimeSpan INIT_TIMEOUT_SEC = TimeSpan.FromSeconds(180); /* Change this to a more reasonable value */
    private static TimeSpan IMPLICIT_TIMEOUT_SEC = TimeSpan.FromSeconds(10); /* Change this to a more reasonable value */

    [SetUp]
    public void BeforeAll()
    {
        DesiredCapabilities capabilities = new DesiredCapabilities();



        TestCapabilities testCapabilities = new TestCapabilities();

        //testCapabilities.App = "";
        testCapabilities.AutoWebView = true;
        testCapabilities.AutomationName = "<just a name>";
        testCapabilities.BrowserName = "Chrome"; // Leave empty otherwise you test on browsers
        testCapabilities.DeviceName = "Needed if testing on IOS on a specific device. This will be the UDID";
        testCapabilities.FwkVersion = "1.0"; // Not really needed
        testCapabilities.Platform = TestCapabilities.DevicePlatform.Android; // Or IOS
        testCapabilities.PlatformVersion = "5.0.1"; // Not really needed


        testCapabilities.AssignAppiumCapabilities(ref capabilities);
        driver = new AndroidDriver(testServerAddress, capabilities, INIT_TIMEOUT_SEC);
        driver.Manage().Timeouts().ImplicitlyWait(IMPLICIT_TIMEOUT_SEC);
    } 

    [TearDown]
    public void AfterAll()
    {
        TestContext.CurrentContext.Result.ToString();
        driver.Quit(); // Always quit, if you don't, next test session will fail
    }

    /// <summary>
    /// Just a simple test to heck out Appium environment.
    /// </summary>
    [Test]
    public void CheckTestEnvironment()
    {
        driver.Navigate().GoToUrl("http://stackoverflow.com");
        driver.FindElementByCssSelector("body > div.topbar > div.network-items > div.login-links-container > a").Click();
        Assert.AreEqual("Log in using any of the following services", (driver.FindElementByCssSelector("h2.title")).Text);
    }    

Many thanks to Andrea Tino for this starting point.

Now this worked fine, and my colleague who set this up and showed it to me has gone on holiday leaving me the task of adding in our existing tests and tweaking bits here and there.

I added in my testclass which requires the Webdriver.Support pacakge to be installed, which depends on Webdriver >= 2.46.0

Now, when I run my code, I get a null reference exception on this line: driver = new AndroidDriver(testServerAddress, capabilities, INIT_TIMEOUT_SEC);

This is the error I'm getting:

AppiumTests.AndroidAppiumTestSuite.CheckTestEnvironment: SetUp : System.NullReferenceException : Object reference not set to an instance of an object. TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

So my thought was that something in 2.46.0 meant I need to supply another capability, but I've been banging my head against this for two days now with no progress.

I have a screenshot of the appium server communication, but I'm not able to link images yet XD so here is it pasted in:

info: [debug] Device launched! Ready for commands

info: [debug] Setting command timeout to the default of 60 secs

info: [debug] Appium session started with sessionId 4575272bba7d11c85414d48cf53ac8e3

info: <-- POST /wd/hub/session 303 10828.204 ms - 70

info: --> GET /wd/hub/session/4575272bba7d11c85414d48cf53ac8e3 {}

info: Proxying [GET /wd/hub/session/4575272bba7d11c85414d48cf53ac8e3] to [GET http://127.0.0.1:9515/wd/hub/session/4575272bba7d11c85414d48cf53ac8e3] with body: {}

info: Got response with status 200: {"sessionId":"4575272bba7d11c85414d48cf53ac8e3","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{},"cssSelect...

info: <-- GET /wd/hub/session/4575272bba7d11c85414d48cf53ac8e3 200 6.770 ms - 506

info: --> POST /wd/hub/session {"desiredCapabilities":{"javascriptEnabled":true,"device":"Android","platformName":"Android","deviceName":"d5cb5478","browserName":"Chrome","platformVersion":"5.0.1","browserVersion":"43.0.2357.93"}}

error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

So from here I can see that its trying to start a new session when I've already started one, but I can't figure out the cause!

1

There are 1 answers

2
Jess On

So here's how to get around the Appium-specific half of the problem -- I don't have an answer for your NullReferenceException.


That Appium error is caused when the port you're trying to start Appium WebDriver on is already in use.

To manually fix this

  1. You can do $telnet ip port to figure out what Process ID you need to kill.

    • You can find the address/port by checking the value for what new Uri(TestServers.WindowsServer); returns.
  2. Exit any emulators

  3. Once you have the PID then you can do $kill -9 pid and start your Appium server like normal.


A proper solution

If you are having this problem regularly, it may be because your script is ending without quitting the Appium WebDriver.

Usually you put the teardown code for the WebDriver (driver.quit()) in the @After section of your tests, or in your base TestCase class.

I see that you have a teardown section there -- so maybe you got into this bad state during a previous session? Either way, the manual fix should get you back to working order.