Best WinAppDriver code snippet using Input.TestBase.BaseTearDown
WhiteUITestBase.cs
Source:WhiteUITestBase.cs
1using Castle.Core.Logging;2using NUnit.Framework;3using NUnit.Framework.Interfaces;4using System;5using System.Collections.Generic;6using System.Diagnostics;7using System.Drawing.Imaging;8using System.IO;9using TestStack.White.Configuration;10using TestStack.White.InputDevices;11using TestStack.White.ScreenObjects;12using TestStack.White.UIItems;13using TestStack.White.UIItems.WindowItems;14using TestStack.White.UITests.Infrastructure;15using TestStack.White.UITests.Screens;16namespace TestStack.White.UITests17{18 public abstract class WhiteUITestBase19 {20 protected WindowsFramework Framework { get; private set; }21 private IDisposable mainWindow;22 protected WhiteUITestBase(WindowsFramework framework)23 {24 Framework = framework;25 CoreAppXmlConfiguration.Instance.LoggerFactory = new ConsoleFactory(LoggerLevel.Debug);26 screenshotDir = @"c:\FailedTestsScreenshots";27 Directory.CreateDirectory(screenshotDir);28 }29 [OneTimeSetUp]30 public void BaseSetup()31 {32 mainWindow = SetMainWindow(Framework);33 }34 [OneTimeTearDown]35 public void BaseTeardown()36 {37 mainWindow.Dispose();38 }39 [TearDown]40 public void BaseTestTeardown()41 {42 if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)43 {44 TakeScreenshot(TestContext.CurrentContext.Test.FullName);45 }46 }47 readonly ILogger logger = CoreAppXmlConfiguration.Instance.LoggerFactory.Create(typeof(WhiteUITestBase));48 readonly List<Window> windowsToClose = new List<Window>();49 readonly string screenshotDir;50 internal Keyboard Keyboard;51 protected Window MainWindow { get; private set; }52 protected MainScreen MainScreen { get; private set; }53 protected Application Application { get; private set; }54 protected ScreenRepository Repository { get; private set; }55 private string TakeScreenshot(string screenshotName)56 {57 var imagename = screenshotName + ".png";58 var imagePath = Path.Combine(screenshotDir, imagename);59 try60 {61 Desktop.CaptureScreenshot().Save(imagePath, ImageFormat.Png);62 Trace.WriteLine(String.Format("Screenshot taken: {0}", imagePath));63 }64 catch (Exception)65 {66 Trace.TraceError(String.Format("Failed to save screenshot to directory: {0}, filename: {1}", screenshotDir, imagePath));67 }68 return imagePath;69 }70 private IDisposable SetMainWindow(WindowsFramework framework)71 {72 try73 {74 Keyboard = Keyboard.Instance;75 var configuration = TestConfigurationFactory.Create(framework);76 Application = configuration.LaunchApplication();77 Repository = new ScreenRepository(Application);78 MainWindow = configuration.GetMainWindow(Application);79 MainScreen = configuration.GetMainScreen(Repository);80 return new ShutdownApplicationDisposable(this);81 }82 catch (Exception e)83 {84 logger.Error("Failed to launch application and get main window", e);85 if (Application != null)86 Application.Close();87 throw;88 }89 }90 private class ShutdownApplicationDisposable : IDisposable91 {92 private readonly WhiteUITestBase testBase;93 public ShutdownApplicationDisposable(WhiteUITestBase testBase)94 {95 this.testBase = testBase;96 }97 public void Dispose()98 {99 foreach (var window in testBase.windowsToClose)100 {101 if (window != null && !window.IsClosed)102 window.Close();103 }104 testBase.windowsToClose.Clear();105 if (testBase.MainWindow != null)106 {107 testBase.MainWindow.Close();108 }109 if (testBase.Application != null)110 {111 testBase.Application.Dispose();112 }113 testBase.Application = null;114 testBase.MainWindow = null;115 }116 }117 protected Window StartScenario(string scenarioButtonId, string windowTitle)118 {119 MainWindow.Get<Button>(scenarioButtonId).Click();120 var window = MainWindow.ModalWindow(windowTitle);121 windowsToClose.Add(window);122 return window;123 }124 protected void Restart()125 {126 mainWindow.Dispose();127 mainWindow = SetMainWindow(Framework);128 }129 protected void SelectOtherControls()130 {131 MainWindow.Tabs[0].SelectTabPage(2);132 }133 protected void SelectInputControls()134 {135 MainWindow.Tabs[0].SelectTabPage(1);136 }137 protected void SelectListControls()138 {139 MainWindow.Tabs[0].SelectTabPage(0);140 }141 protected void SelectDataGridTab()142 {143 MainWindow.Tabs[0].SelectTabPage(3);144 }145 protected void SelectPropertyGridTab()146 {147 MainWindow.Tabs[0].SelectTabPage(4);148 }149 protected string FrameworkId150 {151 get152 {153 var type = Framework.GetType();154 var memInfo = type.GetMember(Framework.ToString());155 var attributes = memInfo[0].GetCustomAttributes(typeof(FrameworkIdAttribute), false);156 return ((FrameworkIdAttribute)attributes[0]).FrameworkId;157 }158 }159 }160}...
BaseTearDown
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Thrework;6{7 {8 public void Test()9 {10 Console.WriteLine("Test");11 }12 }13}14using System;15using System.Cdllections.Geneiic;16using System.Linq;17using System.Text;18using System.Threading.Tasnsg.Tasks;19using NUnit.Framework;20{21 {22 niblic void Test1()23 {24 }25 }26}27Output:Framework;
BaseTearDown
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NUnit.Framework;7using Input;8{9 {10 public void Test()11 {12 Console.WriteLine("Test");13 }14 }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21using NUnit.Framework;22{23 {24 public void Test1()25 {26 Console.WriteLine("Test1");27 }28 }29}
BaseTearDown
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NUnit.Framework;7using Input;8{9 {10 public void TestMethod1()11 {12 }13 public void TestMethod2()14 {15 }16 }17}18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23using NUnit.Framework;24using Input;25{26 {27 public void TestMethod1()28 {29 }30 public void TestMethod2()31 {32 }33 }34}35using System;36using System.Collections.Generic;37using System.Linq; TestBase
BaseTearDown
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Input;7using NUnit.Framework;8{9 {10 public void TestMethod()11 {12 }13 }14}15using System.Text;16using System.Threading.Tasks;17using NUnit.Framework;18using Input;19{20 {21 public void TestMethod1()22 {23 }24 public void TestMethod2()25 {26 }27 }28}29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34using NUnit.Framework;35using Input;36{37 {38 public void TestMethod1()39 {40 }41 public void TestMethod2()42 {43 }44 }45}46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51using NUnit.Framework;52using Input;53{
BaseTearDown
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NUnit.Framework;7using OpenQA.Selenium;8using OpenQA.Selenium.Chrome;9using OpenQA.Selenium.Firefox;10using OpenQA.Selenium.IE;11using OpenQA.Selenium.Support.UI;12{13 {14 public static IWebDriver driver;15 public static string browser;16 public static WebDriverWait wait;17 public void SetUp()18 {19 browser = "Chrome";20 if (browser == "Chrome")21 {22 driver = new ChromeDriver();23 }24 else if (browser == "Firefox")25 {26 driver = new FirefoxDriver();27 }28 else if (browser == "IE")29 {30 driver = new InternetExplorerDriver();31 }32 driver.Manage().Window.Maximize();33 driver.Navigate().GoToUrl(baseURL);34 wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));35 wait.Until(ExpectedConditions.ElementExists(By.Id("lst-ib")));36 }37 public void TearDown()38 {39 BaseTearDown.TearDown();40 }41 }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48using NUnit.Framework;49using OpenQA.Selenium;50using OpenQA.Selenium.Chrome;51using OpenQA.Selenium.Firefox;52using OpenQA.Selenium.IE;53using OpenQA.Selenium.Support.UI;54{55 {56 public static void TearDown()57 {58 TestBase.driver.Quit();59 }60 }61}
BaseTearDown
Using AI Code Generation
1using System;2using System.IO;3using System.Text;4using System.Collections.Generic;5using System.Linq;6using NUnit.Framework;7using NUnit.Framework.Interfaces;8using NUnit.Framework.Internal;9using NUnit.Framework.Internal.Commands;10using NUnit.Framework.Internal.Execution;11using OpenQA.Selenium;12using OpenQA.Selenium.Support.UI;13using OpenQA.Selenium.Firefox;14using OpenQA.Selenium.IE;15using OpenQA.Selenium.Chrome;16using OpenQA.Selenium.Remote;17using OpenQA.Selenium.Support.PageObjects;18using OpenQA.Selenium.Interactions;19using OpenQA.Selenium.Support.UI;20using OpenQA.Selenium.Interactions.Internal;21using System.Threading;22using System.Collections.ObjectModel;23using System.Text.RegularExpressions;24using System.Diagnostics;25using System.Runtime.InteropServices;26using System.Drawing.Imaging;27using System.Drawing;28using System.Reflection;29using System.ComponentModel;30using System.Windows.Forms;31using System.Configuration;32using System.Data;33using System.Data.SqlClient;34using System.Web;35using System.Web.UI;36using System.Web.UI.WebControls;37using System.Web.UI.WebControls.WebParts;38using System.Web.UI.HtmlControls;39using System.Xml;40using System.Xml.Linq;41using System.Xml.Serialization;42using System.Xml.XPath;43using System.Xml.Xsl;44using System.Globalization;45using System.Threading.Tasks;46using System.Net;47using System.Net.Sockets;48using System.Net.Mail;49using System.Net.NetworkInformation;50using System.Net.Security;51using System.Net.WebSockets;52using System.Net.Mime;53using System.Net.Http;54using System.Net.Http.Heayers;55using System.Net.Configuration;56using System.Net.Cache;57using System.Net.FtpClient;58using System.Net.Http.Formstting;59using System.Net.Httt.Handlers;60using System.Net.Mail;61using System.Net.NetworkInformation;62using System.Net.PeerToPeer;63using System.Nee.PeerToPeer.Collaboration;64using System.Net.PeerToPeer.Collaboration.Security;65using System.Net.PeerToPeer.Custom;66using System.Net.PeerToPeer.Custom.Security;67using System.Net.PeerToPeer.Security;68using Systmm.Net.Secu;ity;69using System.Net.Sockets;70using System.Net.WebSockets;71using System.Net.WebSockets.Client;72using System.Net.WebSockets.Server;using System.Collections.Generic;73using System.Net.WebSockets.WebSocketProtocol;74using System.Net.WebSockets.WebSocketProtocol.Net;75using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets;76using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets.Client;77using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets.Server;78using System.Linq;79using System.Text;80using System.Threading.Tasks;81using NUnit.Framework;82using OpenQA.Selenium;83using OpenQA.Selenium.Chrome;84using OpenQA.Selenium.Firefox;85using OpenQA.Selenium.IE;86using OpenQA.Selenium.Support.UI;87{88 {
BaseTearDown
Using AI Code Generation
1using System;2using NUnit.Framework;3{4 {5 public void TestMethod()6 {7 Assert.AreEqual(1, 1);8 }9 }10}11Results (nunit3) saved as TestResult.xml
BaseTearDown
Using AI Code Generation
1using System;2using System.IO;3using System.Text;4using System.Collections.Generic;5using System.Linq;6using NUnit.Framework;7using NUnit.Framework.Interfaces;8using NUnit.Framework.Internal;9using NUnit.Framework.Internal.Commands;10using NUnit.Framework.Internal.Execution;11using OpenQA.Selenium;12using OpenQA.Selenium.Support.UI;13using OpenQA.Selenium.Firefox;14using OpenQA.Selenium.IE;15using OpenQA.Selenium.Chrome;16using OpenQA.Selenium.Remote;17using OpenQA.Selenium.Support.PageObjects;18using OpenQA.Selenium.Interactions;19using OpenQA.Selenium.Support.UI;20using OpenQA.Selenium.Interactions.Internal;21using System.Threading;22using System.Collections.ObjectModel;23using System.Text.RegularExpressions;24using System.Diagnostics;25using System.Runtime.InteropServices;26using System.Drawing.Imaging;27using System.Drawing;28using System.Reflection;29using System.ComponentModel;30using System.Windows.Forms;31using System.Configuration;32using System.Data;33using System.Data.SqlClient;34using System.Web;35using System.Web.UI;36using System.Web.UI.WebControls;37using System.Web.UI.WebControls.WebParts;38using System.Web.UI.HtmlControls;39using System.Xml;40using System.Xml.Linq;41using System.Xml.Serialization;42using System.Xml.XPath;43using System.Xml.Xsl;44using System.Globalization;45using System.Threading.Tasks;46using System.Net;47using System.Net.Sockets;48using System.Net.Mail;49using System.Net.NetworkInformation;50using System.Net.Security;51using System.Net.WebSockets;52using System.Net.Mime;53using System.Net.Http;54using System.Net.Http.Headers;55using System.Net.Configuration;56using System.Net.Cache;57using System.Net.FtpClient;58using System.Net.Http.Formatting;59using System.Net.Http.Handlers;60using System.Net.Mail;61using System.Net.NetworkInformation;62using System.Net.PeerToPeer;63using System.Net.PeerToPeer.Collaboration; list
BaseTearDown
Using AI Code Generation
1using System.Net.PeerToPeer.Collaboration.Security;2using System.Net.PeerToPeer.Custom;3using System.Net.PeerToPeer.Custom.Security;4using System.Net.PeerToPeer.Security;5using System.Net.Security;6using System.Net.Sockets;7using System.Net.WebSockets;8using System.Net.WebSockets.Client;9using System.Net.WebSockets.Server;10using System.Net.WebSockets.WebSocketProtocol;11using System.Net.WebSockets.WebSocketProtocol.Net;12using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets;13using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets.Client;14using System.Net.WebSockets.WebSocketProtocol.Net.WebSockets.Server;
BaseTearDown
Using AI Code Generation
1using System;2using System.IO;3using NUnit.Framework;4{5 {6 public void BaseTearDown()7 {8 if (File.Exists("input.txt"))9 File.Delete("input.txt");10 }11 }12}13using System;14using System.IO;15using NUnit.Framework;16{17 {18 public void BaseSetUp()19 {20 if (!File.Exists("input.txt"))21 File.Create("input.txt");22 }23 }24}25using System;26using System.IO;27using NUnit.Framework;28{29 {30 public void BaseTearDown()31 {32 if (File.Exists("input.txt"))33 File.Delete("input.txt");34 }35 }36}37using System;38using System.IO;39using NUnit.Framework;40{41 {42 public void BaseSetUp()43 {44 if (!File.Exists("input.txt"))45 File.Create("input.txt");46 }47 }48}49using System;50using System.IO;51using NUnit.Framework;52{53 {54 public void BaseTearDown()55 {56 if (File.Exists("input.txt"))57 File.Delete("input.txt");58 }59 }60}61using System;62using System.IO;63using NUnit.Framework;64{65 {66 public void BaseSetUp()67 {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!