【发布时间】: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