我想在我的程序中 运行 多个 [测试方法]。并希望在每次 [测试方法] 后关闭浏览器会话
I want to run multiple [Test Method] in my program. And want to close the browser session after every [Test Method]
记住:我的测试通过后没有发现任何问题。我在测试失败时看到了问题。
问题:我 运行 [Test Method] -1
并为 Test-1
提供了错误的 Xpath 来处理失败情况。
由于 XPath 错误,我的驱动程序一直在浏览器上等待,但最终并没有关闭浏览器会话。驱动程序在测试资源管理器上抛出失败状态。
无论测试是否因任何原因失败,我都希望驱动程序关闭会话。
TestClass.cs
我运行很多[Test Method]
[Test Method]
public void Test1()
{
try{
Assert.IsTrue(ProgramPage.checkTest1());
}
catch(Exception e)
{
throw ;
}
}
[Test Method]
public void Test2()
{
try{
Assert.IsTrue(ProgramPage.checkTest2());
}
catch(Exception e)
{
throw ;
}
}
ProgramPage.cs
我定义页面逻辑和页面对象
// can we put try and catch block on Assert ??
// it's simple display check example
public static class {
public static IWebElement webElement = null ;
public static void checkTest1()
{
try {
webElement = driver.FindElement(xpath--1);
if(webElement.Displayed)
{
Console.Write("element is visible ");
}
else
{
Console.Write("element is not visible");
}
}
catch(Exception e)
{
throw ;
}
}
public static void checkTest2()
{
try {
webElement = driver.FindElement(xpath--2);
// SAY THIS XPATH is Wrong
if(webElement.Displayed)
{
Console.Write("element is visible ");
}
else
{
Console.Write("element is not visible");
}
}
catch(Exception e)
{
throw ;
}
}
// when test fail on wrong Xpath , driver does not close the Browser session
Driver.cs
我初始化驱动程序并从这里关闭。
public static class{
// I am not having private driver for each class , is that an issue ?
[Test Initialize]
public void Init()
{
//initialize the driver
//launch and login the browser
}
[Test Cleanup]
public void Close()
{
Driver.Close(); // IT has to be executed no matter
// what the test result is
}
}
使 class 成为非静态的,并编写不带空格的 TestInitialize 和 TestCleanup。
示例:
public class BasicTestClass
{
private IWebDriver driver;
[TestInitialize]
public void Init()
{
driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.google.nl");
}
[TestCleanup]
public void Close()
{
Driver.Close();
}
// TestMethods go here
}
此外,将测试 class 放入名为 Driver.cs
的文件中似乎很奇怪
记住:我的测试通过后没有发现任何问题。我在测试失败时看到了问题。
问题:我 运行 [Test Method] -1
并为 Test-1
提供了错误的 Xpath 来处理失败情况。
由于 XPath 错误,我的驱动程序一直在浏览器上等待,但最终并没有关闭浏览器会话。驱动程序在测试资源管理器上抛出失败状态。
无论测试是否因任何原因失败,我都希望驱动程序关闭会话。
TestClass.cs
我运行很多[Test Method]
[Test Method]
public void Test1()
{
try{
Assert.IsTrue(ProgramPage.checkTest1());
}
catch(Exception e)
{
throw ;
}
}
[Test Method]
public void Test2()
{
try{
Assert.IsTrue(ProgramPage.checkTest2());
}
catch(Exception e)
{
throw ;
}
}
ProgramPage.cs
我定义页面逻辑和页面对象
// can we put try and catch block on Assert ??
// it's simple display check example
public static class {
public static IWebElement webElement = null ;
public static void checkTest1()
{
try {
webElement = driver.FindElement(xpath--1);
if(webElement.Displayed)
{
Console.Write("element is visible ");
}
else
{
Console.Write("element is not visible");
}
}
catch(Exception e)
{
throw ;
}
}
public static void checkTest2()
{
try {
webElement = driver.FindElement(xpath--2);
// SAY THIS XPATH is Wrong
if(webElement.Displayed)
{
Console.Write("element is visible ");
}
else
{
Console.Write("element is not visible");
}
}
catch(Exception e)
{
throw ;
}
}
// when test fail on wrong Xpath , driver does not close the Browser session
Driver.cs
我初始化驱动程序并从这里关闭。
public static class{
// I am not having private driver for each class , is that an issue ?
[Test Initialize]
public void Init()
{
//initialize the driver
//launch and login the browser
}
[Test Cleanup]
public void Close()
{
Driver.Close(); // IT has to be executed no matter
// what the test result is
}
}
使 class 成为非静态的,并编写不带空格的 TestInitialize 和 TestCleanup。
示例:
public class BasicTestClass
{
private IWebDriver driver;
[TestInitialize]
public void Init()
{
driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.google.nl");
}
[TestCleanup]
public void Close()
{
Driver.Close();
}
// TestMethods go here
}
此外,将测试 class 放入名为 Driver.cs
的文件中似乎很奇怪