c# - 从 Selenium.Webdriver 2.44 更新到 2.46 导致 NullReferenceException

标签 c# android selenium nullreferenceexception appium

我们公司刚刚开始使用 Appium,用它来自动测试网站和网络应用程序。

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

我之前在一个简单的网站上使用过 Selenium 和 Nunit 在桌面上进行测试,在我的测试中使用 [Test]、[TestCase] 和 [TestCaseSource] 属性。

按照文章中的建议回答: How to integrate Appium with C#?

(更快的文章链接在这里: http://blogs.technet.com/b/antino/archive/2014/09/22/how-to-set-up-a-basic-working-appium-test-environment.aspx )

一名同事设置了一个解决方案,该解决方案将对 StackOverflow 进行简单导航,单击链接并断言:

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);
    }    

非常感谢 Andrea Tino 提供了这个起点。

现在它运行良好,设置它并向我展示它的我的同事去度假了,让我完成了添加现有测试并到处调整位的任务。

我在需要安装 Webdriver.Support 包的测试类中添加,这取决于 Webdriver >= 2.46.0

现在,当我运行我的代码时,我在这一行得到一个空引用异常: driver = new AndroidDriver(testServerAddress, capabilities, INIT_TIMEOUT_SEC);

这是我遇到的错误:

AppiumTests.AndroidAppiumTestSuite.CheckTestEnvironment: SetUp:System.NullReferenceException:对象引用未设置为对象的实例。 TearDown:System.NullReferenceException:未将对象引用设置为对象的实例。

所以我的想法是 2.46.0 中的某些东西意味着我需要提供另一种功能,但我已经为此苦苦思索了两天,现在没有任何进展。

我有 appium 服务器通信的屏幕截图,但我还无法链接图像 XD 所以将其粘贴到这里:

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

所以从这里我可以看到它试图在我已经开始一个新 session 时开始一个新 session ,但我无法找出原因!

最佳答案

下面是解决 Appium 特有的一半问题的方法——我没有针对您的 NullReferenceException 的答案。


当您尝试启动 Appium WebDriver 的端口已被使用时,会导致该 Appium 错误。

手动修复

  1. 您可以执行$telnet ip port 来找出您需要杀死的进程ID

    • 您可以通过检查 new Uri(TestServers.WindowsServer); 返回的值来找到地址/端口。
  2. 退出所有模拟器

  3. 一旦您获得了 PID,您就可以执行 $kill -9 pid 并像往常一样启动您的 Appium 服务器。


适当的解决方案

如果您经常遇到这个问题,可能是因为您的脚本在没有退出 Appium WebDriver 的情况下结束。

通常,您将 WebDriver 的拆卸代码 (driver.quit()) 放在测试的 @After 部分,或者放在基础 TestCase 中 类。

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.

关于c# - 从 Selenium.Webdriver 2.44 更新到 2.46 导致 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873438/

相关文章:

c# - 有没有办法在同一行中转换对象并访问其属性?

c# - ServiceStack.Razor 从 razor View 输出绝对 URL

c# - 使用对象以外的东西锁定线程

java - 无法使用 Selenium 3.0.0-beta3 通过 Geckodriver 启动 Firefox 41

c# - 如何使用 Selenium ExecuteScript 函数获取 HashTable

c# - 是否可以使用具有一系列值的哈希表?

android - 如何使用 Hilt 在存储库中注入(inject)应用程序上下文?

android - 通过布局更改 xml 可绘制文件中形状的颜色

android - 从 xml 文件中扩充 View 是什么意思?

java - 使用 "webdriver.firefox.marionette"时由 : java. lang.NullPointerException 引起的 UnreachableBrowserException