【问题标题】:Updating from Selenium.Webdriver 2.44 to 2.46 causes NullReferenceException从 Selenium.Webdriver 2.44 更新到 2.46 会导致 NullReferenceException
【发布时间】:2015-09-01 14:49:02
【问题描述】:

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

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

我之前在一个简单的网站上使用过 Selenium 和 Nunit 在 Desktop 上进行测试,在我的测试中使用 [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 : Object reference not set to an instance of an object. TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

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

我有一个appium服务器通信的截图,但是我还不能链接图片XD所以这里是粘贴的:

信息:[调试] 设备启动!准备好命令

info: [debug] 将命令超时设置为默认值 60 秒

信息:[调试] Appium 会话以 sessionId 4575272bba7d11c85414d48cf53ac8e3 开始

信息:

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

信息:代理 [GET /wd/hub/session/4575272bba7d11c85414d48cf53ac8e3] 到 [GET http://127.0.0.1:9515/wd/hub/session/4575272bba7d11c85414d48cf53ac8e3] 正文:{}

信息:得到状态 200 的响应:{"sessionId":"4575272bba7d11c85414d48cf53ac8e3","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false," browserName":"chrome","chrome":{},"cssSelect...

信息:

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"}}

错误:无法启动 Appium 会话,错误是:错误:请求新会话但正在进行中

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

【问题讨论】:

  • 从头开始,使用我链接的文章中的示例,我设法通过更改:public IWebDriver driver; 而不是 appium 驱动程序和:driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180)); 使其工作。抱怨的似乎是 AndoridDriver,但我确信我已经提供了所有必需的功能。我无法通过原始项目解决此问题,因此我将其添加为评论,因为它更像是一种解决方法。

标签: c# android selenium nullreferenceexception appium


【解决方案1】:

因此,这里是解决 Appium 特有的那一半问题的方法——我没有你的NullReferenceException 的答案。


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

手动修复此问题

  1. 您可以通过$telnet ip port 找出您需要杀死的Process ID

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

  3. 一旦你有了PID,你就可以做$kill -9 pid,然后像往常一样启动你的Appium服务器。


适当的解决方案

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

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

我看到您那里有一个拆解部分 - 所以也许您在之前的会话中进入了这种糟糕的状态?无论哪种方式,手动修复都应该让您恢复正常工作。

【讨论】:

  • 谢谢你的回答,它给了我一些想法。当我调试并进入:driver = new AndroidDriver(testServerAddress, capabilities, INIT_TIMEOUT_SEC); 我看到 chrome 在手机上启动,appium 发送上面的命令,而且更准确,现在我可以发布图像,我会在我回来工作时添加它们。如果对NullReferenceException 有更多了解。对我来说真正奇怪的是上面的代码在我拥有的一个副本中工作,我所做的唯一更改是导入 Webdriver.Support,将 Webdriver 更新到 2.46 并添加 using 语句:S
  • 你应该使用 Appium c# webdriver -- 而不是 seleniums。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 2015-04-27
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多