Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

WebDriver Select Test Environment C# Code

When writing console application with Selenium WebDriver, sometime, users need to select a test environment, so that Selenium tests can navigate to the selected environment and do the test. In this code snippet below, users have the option to select Production, Staging, or QA environment within the timeout specified in the code. If the user does not press any key within the timeout period, the method will return the QA environment as default test environment.

 

 

Code Snippet
  1.  
  2. public static int SelectTestEnvironment(int timeOutInSeconds)
  3. {
  4. int _environmentNumber;
  5. Console.WriteLine("Please enter the environment number you are going to test:");
  6. Console.WriteLine("You have 60 seconds to select the environment");
  7. Console.WriteLine("If you do not respond within 60 seconds, the system navigate to Default QA Test Environment");
  8. Console.WriteLine("1. Production");
  9. Console.WriteLine("2. Staging");
  10. Console.WriteLine("3. QA");
  11. //waiting for to read environment number 1 to 3
  12. DateTime currentTime = DateTime.Now;
  13. DateTime elapseTime = DateTime.Now;
  14. bool timeUp = false;
  15. String enteredEnvironmentKey;
  16.  
  17. Console.WriteLine("Please enter environment number ....");
  18. while (!Console.KeyAvailable)
  19. {
  20.     elapseTime = DateTime.Now;
  21.     var timeDiff = elapseTime.Subtract(currentTime).TotalSeconds;
  22.     if (timeDiff > timeOutInSeconds)
  23.     {
  24.         Console.WriteLine("Time out. System is navigating to QA Environment ");
  25.         timeUp = true;
  26.         break;
  27.     }
  28. }
  29.  
  30. enteredEnvironmentKey = timeUp ? ConfigurationManager.AppSettings["DefaultTestEnvironmentNumber"]: Console.ReadLine().ToString();
  31. int value;
  32. if (int.TryParse(enteredEnvironmentKey, out value))
  33. {
  34.     _environmentNumber = value;
  35.     Console.WriteLine("You entered environment number: " + _environmentNumber);
  36. }
  37. else
  38. {
  39.     Console.WriteLine("Please enter correct Test Environment number in your next attempt! ");
  40.     Console.WriteLine("Close this console and restart it, Thanks!");
  41.     Console.ReadKey();
  42. }
  43. return _environmentNumber;
  44. }