【发布时间】:2017-02-11 00:41:38
【问题描述】:
我正在自动化我的 github 配置文件,以下是我的测试用例:
- 加载浏览器(在 testInitialize() 中定义
- 加载网址
- 执行登录 下面是sn-p的代码:
命名空间 GitAutomationTest { 使用 Microsoft.VisualStudio.TestTools.UnitTesting; 使用 OpenQA.Selenium.IE; 使用 OpenQA.Selenium.Remote; 使用系统; [测试类] 公共类 GitTest { 私有字符串 baseURL = "https://github.com/login"; 私有 RemoteWebDriver 驱动程序; 公共 TestContext TestContext { 获取;放; }
[TestMethod]
public void LoadURL() {
driver.Navigate().GoToUrl(baseURL);
Console.Write("Loaded URL is :" + baseURL);
}
[TestMethod]
public void PerformLogin() {
driver.FindElementById("login_field").SendKeys("USERNAME");
driver.FindElementById("password").SendKeys("PASSWORD");
Console.Write("password entered \n ");
driver.FindElementByClassName("btn-primary").Click();
driver.GetScreenshot().SaveAsFile(@"screenshot.jpg", format: System.Drawing.Imaging.ImageFormat.Jpeg);
Console.Write("Screenshot Saved: screenshiot.jpg");
}
[TestCleanup()]
public void MyTestCleanup()
{
driver.Quit();
}
[TestInitialize()]
public void MyTestInitialize()
{
driver = new InternetExplorerDriver();
driver.Manage().Window.Maximize();
Console.Write("Maximises The window\n");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
}
}
}
输出
每次我运行所有测试时:
- 测试已初始化:Internet Explorer 已加载
- 基础网址已加载
- 然后驱动程序退出 TestCleanUP()
驱动程序下次运行 testperformLogin() - 测试找不到执行登录的用户名和密码元素,因为这次没有加载base url。
我们如何管理 TestInitialize() 类: - 浏览器使用 baseurl 直到所有测试完成。 我们如何管理 TestCleanup() 以便: - 浏览器仅在所有测试完成后关闭。
【问题讨论】:
-
为什么不试试 Nunit 测试框架呢?请看一看。 nunit.org/index.php?p=docHome&r=2.6.4
标签: c# selenium testing selenium-webdriver integration-testing