WebDriver Exploration Part-1(Setting the stage for WebDriver)
In this lecture, we will set up and start exploring the Webdriver API
Let's commence
Setting the stage
I have created a new Unit test project called 'WebDriverCourse', which we will be used for WebDriver exploration lectures
Right-click on the project -->Manage Nuget packages
Click on Browse and search for Selenium.WebDriver and install same.
Now you can see the project has reference to WebDriver DLLs under the reference section of the project.
Below is the default unitTest.cs file which is autogenerated during project creation
In our first test, we will be launching google site on the Internet Explorer browser.
We need to download IEDriverServer.exe from https://www.seleniumhq.org/download/
Unzip the folder and copy and paste the IEDriverServer.exe to WebDriverCourse project.
Right-click the exe and open properties, set 'Copy to Output Directory' value to 'Copy if newer'
This will copy the exe in our debug folder and our code can use same.
Below is our modified test
Here we started with creating an object of InternetExplorerOptions, using which we can manage Internet explorer settings
To run our script on IE, we need to set protected mode settings for all zone to same. We can manually do this or we can ignore this while running the script.
Next, we created the IWebDriver object using InternetExplorerDriver(For IE) and passed the options object to same.
And finally, we opened the google web page.
Run the test and you can IE browser opens and google page is loaded.
Now we will add another test to do the following
Here driver.Close(), will close the current browser instance.
If there are multiple browser instances and you want to close all then use a driver.Quit() method
There are many more IWebdriver commands which you can explore, we have set our stage and can move forward
Let's commence
Setting the stage
I have created a new Unit test project called 'WebDriverCourse', which we will be used for WebDriver exploration lectures
Right-click on the project -->Manage Nuget packages
Click on Browse and search for Selenium.WebDriver and install same.
Now you can see the project has reference to WebDriver DLLs under the reference section of the project.
Below is the default unitTest.cs file which is autogenerated during project creation
namespace WebDriverCourse { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { } } }
In our first test, we will be launching google site on the Internet Explorer browser.
We need to download IEDriverServer.exe from https://www.seleniumhq.org/download/
Unzip the folder and copy and paste the IEDriverServer.exe to WebDriverCourse project.
Right-click the exe and open properties, set 'Copy to Output Directory' value to 'Copy if newer'
This will copy the exe in our debug folder and our code can use same.
Below is our modified test
[TestClass] public class WebdriverCourse { [TestMethod] public void Launch_BrowserScript() { InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; IWebDriver driver = new InternetExplorerDriver(options); driver.Url = "http://www.google.com"; } }
Here we started with creating an object of InternetExplorerOptions, using which we can manage Internet explorer settings
To run our script on IE, we need to set protected mode settings for all zone to same. We can manually do this or we can ignore this while running the script.
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
Next, we created the IWebDriver object using InternetExplorerDriver(For IE) and passed the options object to same.
And finally, we opened the google web page.
Run the test and you can IE browser opens and google page is loaded.
Now we will add another test to do the following
- open the browser
- launch google home page
- wait to 10 seconds
- minimize the browser window
- wait to 10 seconds
- maximize the browser window
- wait to 10 seconds
- close the browser window
[TestMethod] public void Verify_Maximize_Minimize_Close_Functionality() { InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; //open the browser //launch google home page //wait to 10 seconds IWebDriver driver = new InternetExplorerDriver(options); driver.Url = "http://www.google.com"; System.Threading.Thread.Sleep(10000); //minimize the window //wait to 10 seconds driver.Manage().Window.Minimize(); System.Threading.Thread.Sleep(10000); //maximize the browser window //wait for 10 seconds driver.Manage().Window.Maximize(); System.Threading.Thread.Sleep(10000); //Close the browser driver.Close(); }
Here driver.Close(), will close the current browser instance.
If there are multiple browser instances and you want to close all then use a driver.Quit() method
There are many more IWebdriver commands which you can explore, we have set our stage and can move forward
Comments
Post a Comment