当我在本地运行硒测试时,我声明一个驱动程序实例并执行每个测试,而无需每次都初始化一个新的驱动程序类(请参见下面的基本示例)。但是,当我执行测试时,Visual Studio指示两个测试都通过了,但SauceLabs仅显示了运行测试的结果。如果我在SauceLabs中观看实时供稿,则确实会看到两个测试都在同一个驱动程序实例中运行,这就是我想要的,但是我想将每个测试表示为在webdriver实例之外通过/失败。
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SauceDemo
{
[TestClass]
public class iosTests
{
private static IWebDriver driver;
public TestContext TestContext { get; set; }
[ClassInitialize]
public static void Setup(TestContext t)
{
#pragma warning disable 618
var caps = new DesiredCapabilities();
caps.SetCapability("deviceName", "iPhone 8 Simulator");
caps.SetCapability("deviceOrientation", "portrait");
caps.SetCapability("platformVersion", "12.0");
caps.SetCapability("platformName", "iOS");
caps.SetCapability("browserName", "Safari");
caps.SetCapability("username", SauceUsername);
caps.SetCapability("accessKey", SauceAccessKey);
caps.SetCapability("name", t.TestName);
driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
caps, TimeSpan.FromSeconds(600));
}
[TestMethod]
public void TestMethod1()
{
driver.Navigate().GoToUrl("http://www.google.com");
}
[TestMethod]
public void TestMethod2()
{
driver.Navigate().GoToUrl("http://www.amazon.com");
}
[TestCleanup]
public void TestCleanup()
{
var passed = TestContext.CurrentTestOutcome == UnitTestOutcome.Passed;
((IJavaScriptExecutor)driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
}
[ClassCleanup]
public static void ClassCleanup()
{
driver?.Quit();
}
}
}