I donβt know what WTH happened, but this morning it works fine. Last night, the same code did not work. In any case, in case this helps someone, here is the COMPLETE working C # code for chromedriver 2.5, .NET 4.5 and Selenium 2.45 binary files for .NET:
static void Main(string[] args) { ChromeOptions options = new ChromeOptions(); options.AddArgument("test-type"); // Initialize the Chrome Driver using(var driver = new ChromeDriver(options)) { driver.Navigate().GoToUrl("http://localhost/test.aspx"); // Get User Name field, Password field and Login Button var userNameField = driver.FindElementById("txtUsername"); var userPasswordField = driver.FindElementById("txtPassword"); var loginButton = driver.FindElementById("btnLogin"); // Type user name and password userNameField.SendKeys("MyUSN"); userPasswordField.SendKeys("MyPWD"); // and click the login button loginButton.Click(); // Take a screenshot and save it into screen.png driver.GetScreenshot().SaveAsFile(@"F:\temp\screen.png", ImageFormat.Png); Console.ReadLine(); } }
Note. To reuse the same Chrome browser window for all of your tests, enter a static class variable:
private static ChromeDriver driver;
(In fact, you may need to make all your class level variables static.)
And then do something like the following so that your ChromeDriver can be reused in all of your tests:
[ClassInitialize] // this only executes ONCE per test-run (not once per test!) public static void OneTimeSetup(TestContext ctxt) { ChromeOptions options = new ChromeOptions(); options.AddArgument("test-type"); options.AddArgument("start-maximized"); options.LeaveBrowserRunning = true; driver = new ChromeDriver(@"C:\MyStuff", options); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(4)); }
Herrimancoder
source share