I am automating my github profile and Following are my test cases:
- Load Browser (this is defined in testInitialize()
- Load Url
- Perform Login Below is the code snippet:
namespace GitAutomationTest { using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium.IE; using OpenQA.Selenium.Remote; using System; [TestClass] public class GitTest { private string baseURL = "https://github.com/login"; private RemoteWebDriver driver; public TestContext TestContext { get; set; }
[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));
}
}
}
OUTPUT
Everytime I run all tests:
- Test is initialized : the internet explorer is loaded
- The base url is loaded
- Then the driver quits with TestCleanUP()
Next time the driver runs testperformLogin() - The test cannot find the username and password elements to perform login, because the base url is not loaded this time.
How can we manage the TestInitialize() class such that: - browser is up with baseurl until all the tests are completed. How can we manage TestCleanup() such that: - browser closes only after all the test are completed.