tag:blogger.com,1999:blog-75908768222057973442024-02-02T01:11:19.234-08:00Selenium Tutorials | Selenium WebDriver Tutorials | For BeginnersSelenium Tutorials for Beginners, WebDriver, Interview Questions, C#, Locators, Commands, Mobile, Applications, IDE.Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comBlogger93125tag:blogger.com,1999:blog-7590876822205797344.post-21448684998940114742017-06-09T05:01:00.001-07:002017-07-18T01:55:24.944-07:00WebDriver - Find XPath<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Find XPath</h3><br />
In this post we will first see how to install Firebug and later a simple example of how to use Firebug in order to <b>find XPath</b>.<br /><br />
Download and install Firebug. Its a Firefox Add-in.<br /><br />
Launch Firefox. Go to Tools (menu) -> Web Developer -> Get More Tools.
<img alt="WebDriver Find XPath" alt="WebDriver Find XPath" "border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0U3X6g7HheUEI0DtNtjFfyDKUGNMAj1iOoiTTAN-mNdXMAgmIJPLH4zgPsoPrkQWupIDI5Jn0AI4KVOVJtI9pV68LhrNOYtlQA03id_Ng190oKT7zVhyphenhyphenhNYoboIzY2DdAEZ62UoN-ZQk/s1600/finding-xpath-0a.JPG" /><br />
<br />
Web Developer's Toolbox page opens and install the Firebug add-on.
<img alt="WebDriver Find XPath" alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip180O8mE4aaVAUOO4r7K8cxDbxxd74-knFL1ng_Bblo9KbVdK622vZZzZkUfIrfX-uCMt9Xf8Qu2G7bYF7AHW4EwViNz1KANoi7iTLSoej-V87j3JJuiCErdGF02k_6F7ziFl5Q4zn2U/s1600/finding-xpath-0b.JPG" /><br /><br />
Now as the Firebug is installed, lets see a simple example of its usage to <b>find the XPath</b>.<br /><br />
Lets suppose we have this table. You can also see below the HTML code of the table.<br />
<img alt="WebDriver Find XPath" alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgetfNS1aO7wBit6WZSGInSx6Gz9nGwTkeEWZilkV6iFCHn6tXnpKQQVSGzJs3qpUs8d_Na3uOGxm9aIt4_Uaqwv7UouBWHr6TbYHxqx3g4Lbet3el3LOM-M1WrQm_rgi6yb4PdcThGots/s1600/finding-xpath-1.JPG" /><br />
<img alt="WebDriver Find XPath" alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhegT3fM4plwYJEwiThYL93_vGFMSH-zZiAe1-ZFGmZWXRkIAblY5tQdzdEk6AFpuNDPPDFWZ7y1lVh3en0cFQYbBRp4M6KkA7gj7pktkPQzuRDWue-kSLHVKFrpOy6nD6kdNQmyVtKEZQ/s1600/finding-xpath-0.JPG" /><br /><br />
Right-click on the element, for example if we need to find element 3 in the table, right-click on it and select Inspect Element with Firebug.
<img alt="WebDriver Find XPath" alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghOLzO87v56pUc1ePmeMuciTmKD4Lyb9YUxH3jyb0sCg_c3oq7qmrydKB7EIfLhm4hkSn4YQ2Z5Ulsk3Ritq27DfmtS1erf82zcaPKLsquQyWWyl7IXi694wizmoWLeyL81qg_IvCPbc0/s1600/finding-xpath-1a.JPG" /><br /><br />
It shows the element as HTML code as can be seen below. Right-click on it and select Copy XPath.<br />
<img alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOLXdCogZwhWBDdElmPJD0ZeCKr96Ww-UKILdfuwjG9kuhAzeVSQERtYSe9wXdwGtFgP6qOi-FmJGoEISvhQNCkjRW7bI3yB7RuTk9eo569UXqVSqO_ATH_b6m86MQLXaCMs3uwsJfL7o/s1600/finding-xpath-1b.JPG" /><br />
The XPath which is copied above is:<br />
<img alt="WebDriver Find XPath" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-in981xTBhJwLLrTqV2kqd-gIqKU6uZqtvjaJXB8XvuqjqyWuJMK1UuRXq3oKMZyqkyzw0tTCsueQnrek4TX03CPjitY5vp_vbLdBGuN_gyWowV2RrznyYyQhAkkFdISzC1ePA8vitQ/s1600/finding-xpath-1c2.JPG" /><br /><br />
The same XPath is being used in this code to find an element and printing its text on the console.<br />
<img alt="WebDriver Find XPath" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjD5AiZYdBs5c_Yzk7S2aGGPSRiMK9-PtXo-UVTgRk7Ua-P4p796DIUK4xeeueEFSgNr7jK4RlHMoZUGQsKphyphenhyphenlh6qP9ityQCsyXWrvO0z1TJcWyXJv8qm7gq-zP9IZZEYAoSSqwOc8Ro/s1600/finding-xpath-2.JPG" /><br />
<img alt="WebDriver Find XPath" border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrV8nGnyTscWQFlKWcg0WnLEmpUPkPWYsHHApEsiqSOMHFN2NlnlT7Qu9cKxumkU5B5EGswkB3yXokfzfeaMXH1FH81UKJhpmLp7EWQAny96GkJAOlwHkaH3wz8yfL8hOn3p6ymd2tlLU/s1600/finding-xpath-3.JPG" />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-19616086422655547692017-05-24T06:02:00.000-07:002017-05-24T06:02:31.069-07:00Selenium WebDriver Architecture<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Selenium WebDriver Architecture</h3><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1rqZBw9jQ3rdwOZKji0fSTx3o5el5up9ntv8786bcgz0yyOx6kzuui_tUy1XnBzG5tMrQRXVGxhuzktV10cr2YlWHiWs43Zs2O3ejx5cK3CtjA0pIfVWRMnDC99MQ-cpc5_7GhTZlr34/s1600/Selenium+WebDriver+Architecture-1.png" /><br />
Suppose you are writing your tests in C# and using a common Selenium WebDriver API.
That C# binding is going to send commands across the common WebDriver API. A driver (which ever you are using) is going to be listening at the other end. The driver basically interprets those commands and executes those on the actual browser. Results are returned to the code using the WebDriver API.
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQnvwSwmu-p4QnNgpfK2FSP8JPz5AkAoTcWvb02gZ2Rb-7iqBpClhTcKDCSuah6ok93A9QBm2BXfwP5K2AfkF9rF_pmBjlARrQef8VDt8SefBXytHKyG3rhSReVDSq83Ir12vXjsxHqkg/s1600/Selenium+WebDriver+Architecture.png" />
<br />
You write your test in lets say C# against that Selenium WebDriver API.
That binding code is going to issue commands across WebDriver wire protocol (which is basically REST Based Web Service) that is able to interpret those commands. We also send HTTP requests to driver server.
This driver server is an executable. Each one of the drivers has this driver server that basically listens on the port on your local machine. When the commands come in, the driver server interprets those commands and automates the browser and returns the results back.<br /><br />
You write WebDriver command like:<br />
driver.findElement(By.Name("q"))<br /><br />
This is converted into SPI (Stateless Programming Interface):<br />
findElement(using="name", value="q")<br /><br />
Then we call JSON Wire Protocol.<br /><br />
Selenium server uses JSON Wire Protocol commands to break down JSON object and proceed. This part of the code is dependent on which browser it is running on.<br /><br />
Also see: Learning Selenium Testing Tools - Third Edition by Raghavendra Prasad MG
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-41552168611820168762017-05-07T00:42:00.000-07:002017-05-07T00:42:26.646-07:00WebDriver - Reading XML Files<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Reading XML Files</h3><br />
There can be many ways of <b>reading an XML file using WebDriver & C#</b> like <a href="https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx">XmlDocument</a>, <a href="https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx">XmlReader</a>, <a href="https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx">XDocument</a> etc. In this post we will look into very simple examples of reading XML files using XDocument or XElement. <br /><br />
Below is the XML file ("a.XML") which we will be using in first 3 examples.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9ylhkhy8EyI8kuIFkTbjPnEHzxw1_CxKHbmuwYK8oWeZJei-HrNrRBO7sgUTPAZfzXvFWt-Qr0VTNr9xj5MvwjWhXJogErsTcuYKD6782IV-gorq1XlojMRD22MYEz37veFibLrsU1yI/s1600/Selenium_WebDriver_Reading_XML_Data_0_a.JPG" /><br /><br />
<b>Example 1 -</b> <b>Reading an XML File using WebDriver & C#</b><br /><br />
Firstly we are creating a new XDocument from file "a.XML". Load method uses an underlying XmlReader to read the XML into an XML tree. Elements() method returns a collection of the child elements. Then using foreach loop we are printing the XML document.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKiTgMVraHjD_NftANyOpD7z6qUo1NTPkY35p8ujq8wdpnwqKaxI76CGvlZXFT_wyDYgwYt4w2UiWik8rvHZF1GsFLpk4J5_ao5E0mYg0vFyLeTTJUtbi3GhLVeejO5CUW_BXRsONKg9s/s1600/Selenium_WebDriver_Reading_XML_Data_1.JPG" />
<br /><br />
List of references with this project. Make sure to use <i>System.Xml</i> & <i>System.Xml.Linq</i>.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMol6hnCAcyfD_2mGb7YbtzrYIXgeIMe3vM3aueq_jrGs8kUatMy-OdYHDH2LZWg1SuNjsd7S0Z7TmLzF0HtpS-ag5q8WYoskTvxxWIv6vy7EE9QEoVKxYZq8UeHSENozqBBZDzlDUh7A/s1600/Selenium_WebDriver_Reading_XML_Data_2.JPG" />
<br /><br />
Results of running the above example<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjd1ftFo3QcypsxpuEK8faR8AhEYXMEAeQzvkt25j41RVRi1bjXpiJfLacsKfD1DFNb6coj3hOXp2LTq2uKmczj_ugDVlX2UjI6JWwT2F4RKrLXWgrCgAOfw7ous2fZlFWptZy68Hcm-DU/s1600/Selenium_WebDriver_Reading_XML_Data_3.JPG" />
<br /><br />
<b>Example 2 -</b> <b>Reading an XML File using WebDriver & C#</b><br /><br />
This example is similar to the first one except that we are using XElement instead of XDocument.<br /><br />
XDocument in a way represents a whole XML document. It is normally composed of a number of elements whereas XElement represents an XML element (with attributes, children etc). It is <i>part</i> of a larger document. [<a href="http://stackoverflow.com/questions/3879868/whats-the-difference-between-xelement-and-xdocument">Via</a>]<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8insLD0oU6ekSD2tfeDit6JzUTx9TwdWCD_KOawhv4HQFK9x3SkswdTCtpF3lKYjS_-IyeQ4fip_4Xu_0keDtlGE-vgxWkgqcN4dP2-85o4Jy06Hsm86P9DMCkBw_dw0CUGDgGpvSzGY/s1600/Selenium_WebDriver_Reading_XML_Data_4.JPG" />
<br /><br />
List of references with this project<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkoXd8Uu9htXg4rj1i_Wupu42DSjuIbVkTpUHNKfM7Z6Ke9ql0M9nSAhqbu-zY8epiNfeO8ukhP0uE-UDs6bW3S3Ax_06QcldTHTGmFR_Rx775jUfCnT3k7r4ylFV4tSpk1WbuFDjKHoQ/s1600/Selenium_WebDriver_Reading_XML_Data_5.JPG" />
<br /><br />
Results of running the above code<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtiav_RMyExZuE4AyWKUDASce_ngaeWEa8i6dDi08GX92YhZ0HeeefPpPHKPGWz0FIvFWC0ikuqJQfjj_oxoaHD3drV5tSXxT6TFiIC_Ws6Iupc9IZZ0tJT0OWgZLlLOBH6gJwUDrqTmI/s1600/Selenium_WebDriver_Reading_XML_Data_6.JPG" />
<br /><br />
<b>Example 3 -</b> <b>Reading an XML File using WebDriver & C#</b><br /><br />
In this example we are retrieving the entry names (Entry 01, Entry 02) and printing them on the console. Moreover, if we find "Entry 01", we are searching this in the google.com also. This is just one of those very simple examples where we retrieve the value from the XML file and use them in some application. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5dH_7aodI73kOd7C023yYs6rvRiUk6LnugeQLyBOfqL3a_LjrdStMZmbVJtf6frfb6s6BdsG7IG8JKi7OWrUkQHb7tS6MjZgOdaY044aUj3_6xxfOvxGMKFqErtbZbg9dd_UjAprze8s/s1600/Selenium_WebDriver_Reading_XML_Data_7.JPG" />
<br /><br />
Apart from navigating to google.com and searching for "Entry 01", it also shows the below result:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiriw9zQVWs1jOWvjJKWcEFZicsnbKhmeTwb4SwnvriJ8VfZ9SBqNP1zljWiVeIecH5Mt_97I66fvzm9LCWxgoA84RoVq1iqZdwFYL0XroDN1JclThodndI1szZ1sOIBg50sPQIymmnU40/s1600/Selenium_WebDriver_Reading_XML_Data_8.JPG" />
<br /><br />
Below is the XML file ("b.XML") which we will be using in this fourth example.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGjTGylTn-k7YBWI2WBxO_bLafKQXF5n03-kmQ_SYi2SSX6y0HPW6mjTVsRX-c0_8vdZOFnhln5IlYW6D6nLqEmALAl028d10318BUst1qTfmhXekADwxft2SFPCxEV8tB7Bw5ywha3ps/s1600/Selenium_WebDriver_Reading_XML_Data_0_b.JPG" /><br /><br />
<b>Example 4 -</b> <b>Reading an XML File using WebDriver & C#</b><br /><br />
In this example we are retrieving usernames and passwords. These usernames and passwords are further being used to login in the gmail. This is also very simple example of how you can retrieve the required values from XML and use them in some application.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbrFW89EvvRCKtyODMWvZcR3C3nxx2hJC_wLzgwq5L7AxNhYk3mrllHIHKTIUrIxw1r6BbXY96eZWOFc6rx9ONjVag1i851meP9hlzy4_UgS1R1K_X0GhyWj9JY-zBYjj1L6Oi2Da1hS0/s1600/Selenium_WebDriver_Reading_XML_Data_9.JPG" />
<br /><br />
Apart from navigating to gmail.com and trying to login, it also shows the below result:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirHEHM51jaOTP-ehmdS0kKeA4tFKaH9wvZ0xx_ONJMs5pt7RnTKPHkR8EUflmWJEdVmFgpXXi3H-LUdi60aLlgv_EQxCdME3-sy6VcgyldqAJEDVQ8yV9XVyx1wqeFxH-5gbRyxQwzP0E/s1600/Selenium_WebDriver_Reading_XML_Data_10.JPG" />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-44804361500400737162017-04-11T21:39:00.002-07:002017-04-11T21:42:57.318-07:00Selenium WebDriver Framework Using PageFactory<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Selenium WebDriver Framework Using PageFactory</h3><br />
Previously we saw how to create a simple <a href="http://selenium-tutorial.blogspot.com/2017/03/selenium-webdriver-framework.html">Selenium WebDriver Framework</a>. Here we will look into Selenium WebDriver Framework using <b>PageFactory</b>.<br /><br />
<b>PageFactory</b> can be called an enhanced model of Page Object Model. In <b>PageFactory</b> we use <i>@FindBy</i> annotation to locate the elements. @FindBy annotation is actually a way to pass a By object into a driver.findElement() call to create a WebElement. This call to driver.findElement() is completely transparent to you and will be performed in the background whenever you use the WebElement that has an annotation applied to it.
Then we have the supported actions (or methods) which use the @FindBy annotations. These methods use the @FindBy instances which makes them unaffected by changes to the FindBy instances.<br /><br />
<b>Steps to create a Selenium WebDriver Framework using PageFactory</b><br /><br />
1. Start by creating a new unit test project (File (menu) -> New -> Project). Enter Solution name and Location. I am using Microsoft Visual Studio Professional 2012.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkd-jZ72d2LOSAdoHTBd8vvVBfUG-R5Iguj2lngU_OWRSG7o0pMQCfHVR4ndOncU_0i4q_kDzniLo6rclpZ7yRaTQ04s-szxv4BXzeTG_OusS-e5wDPHlCZr9PVAjebFN6RmXuR-vkDHU/s1600/selenium_webdriver_page_factory_1.JPG" />
<br />
2. In Solution Explorer, change the name of unit test from UnitTest1 to loginTest.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic__N3RdjpTVu8BTiImxzZUL4tDH0LiMGQ1Nb2VhPZsRHMM2nEo-3L2VTay1M7jMoQKxfpkJKa85Q5jEuGUB5jAcg9qLjzQkIiIl2_pSunNF8nOa6VQyvIslt-b_4-PSuyaSpEgZCWHKI/s1600/selenium_webdriver_page_factory_2.JPG" />
<br />
3. Write the below code in loginTest.cs file.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyGN2dm1IiY7JoIpLGI9AiRhoC-iegVhnHZCbWLaU3jcPyL5cJETBh_dRlzEZrbZRZzo1MbQo_FydajJAGRoYPrNMs7sq2V0nkZzK2lv1IxKk-2ZEW4vGtXCGRrbinRXtnCioLyUxP_l0/s1600/selenium_webdriver_page_factory_3.JPG" />
<br />
4. Right-click on Solution "MyTests" and add a new project of type Class Library. Name it UILibrary.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTTFKH_6UdHBgvomZ136CBaiIMfrq8Stf-yCof8Zx8BrScnqlU0YV-NaURromRnXgHqEffvZx7gc2t5iY-VnogH-dVmx4GZNNZAus53c36T_mG3hpEK0bUFpQQNmPRbioMvnK-on12nHQ/s1600/selenium_webdriver_page_factory_4.JPG" />
<br />
5. Change the name of Class1.cs under UILibrary to loginPage.cs.<br /><br />
6. Type the code (as seen in the below image) in loginPage Class in loginPage.cs file.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRwJzIiC4tC-O7yJpDRpN_zzrhsaNNOVc9Np_5TjTevRfWzys2O7qhptK5G0Do0-Zg8jwkIpQ9_2fMtuPsMyNS-8A3uuoBkDolzvrZ3LU9oOiz6swoajjGuQ2kfstj-N8Shv2Q6IYy-BE/s1600/selenium_webdriver_page_factory_5.JPG" />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQwV9qwbNiXEiqxAc7Rr4fH31Bnfum6Fr5ihFpWvjEDaaEOnbYgYWJB3_4pEMDULENoQSflOXBQNVggeBQTMcZ5jp1PWllVXyeZWK_S965Zv_kK-Z4YymXLj4dcd0d8Z0oU9qJYldImnU/s1600/selenium_webdriver_page_factory_5b.JPG" />
<br />
For PageFactory you need to use <i>OpenQA.Selenium.Support.PageObjects</i> namespace.<br /><br />
We have a constructor for LoginPage class. In the constructor we have <i>PageFactory.InitElements</i> Method. This method initializes the elements in the Page Object.
It takes two paramenters i.e. <br />
<i>driver</i> - The driver used to find elements on the page and <br />
<i>page</i> - The Page Object to be populated with elements. <br /><br />
PageFactory.InitElements method throws an exception if a field or property decorated with the FindsByAttribute is not of type IWebElement or IList{IWebElement}.<br /><br />
<i>FindsBy</i> attribute marks program elements with methods by which to find a corresponding element on the page. Used in conjunction with the PageFactory, it allows you to quickly create Page Objects.<br /><br />
7. In UILibrary, add references for Selenium WebDriver and Selenium WebDriver Support.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmh-aR-NALRY5voHqoZOF5nky4S-Giy2K2vAQibxJ4h0zuPh3EW2m4DzCLtY_WuWf3-z9s7VsXkbP4LKT-kW_ZYtzJpIuCeSVPPfgoaSXujCc_ApCW7TGZCHphkgKw8SJqu9ij8XAU0lY/s1600/selenium_webdriver_page_factory_6.JPG" />
<br />
8. Add a new CS file with name Driver.cs under UILibrary.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9gi3QBu3tEWySLq5PqFWgrFpMqYAMeCmI0fT74T82YDyYo3CbVVEjjneMHphPfFkjN_KQebvrRfZbkucb3Tpv7vL2Bk8aClCD-Bc8rgG4hPCl_gyIWVyG4LMo08iPbcdN-M0sqw3LIn8/s1600/selenium_webdriver_page_factory_7.JPG" />
<br />
9. Add below code in the Driver.cs file.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE3sG1Ia63JCDK92Ciui7OlJqVGbMj-liiOo55lr5DBtS0pudMWJDGdrKpqmxV3QwyAuftXWaaFV_vIb9CDuUMjJ2ioymoehOH-jw9Y6WEo8xb_MErjdzLgy1h0GBvlI_p47ZKD6T3s5o/s1600/selenium_webdriver_page_factory_7a.JPG" /><br />
10. Add reference to UILibrary for loginTests. In loginTest.cs, add namespace "using UILibrary".<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlJq-C_-ZG8D-8P4INZ4yl49gbab5KlMnu4RKtLKFfe7BKc_Kn3lD-_2ny6LLfoLz85LxPoY1FOalXp8IxjIFxIuWz-Vao8RTGlvhGNo9qTp-v35PgLwz0iS3c-_yJFvGwdnjrF1sZ13s/s1600/selenium_webdriver_page_factory_8.JPG" />
<br />
10b. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVr5rqnYk_ue2y26Z7M4y1JmxN-qnqgkA75AoeLcDhltTscAeJ39eVx_1pIxozFdLNz_Gwc1KHsPH0_y1ubvACwEzLnkSm9eaWZV0r3KE3GlCUvk8zSpF5VCpu6JnXEC1DrKuW_wnHDp8/s1600/selenium_webdriver_page_factory_9.JPG" />
<br /><br />
Now run the <i>EnterCredentialsForLogin()</i> method and see the framework working.
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-46515431746745615952017-03-19T03:07:00.000-07:002017-03-19T03:07:42.141-07:00Step by Step Guide to Simple Selenium WebDriver Framework in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Step by Step Guide to Simple Selenium WebDriver Framework in C#</h3><br />
There are two parts to this post. First, we will login to Gmail without using any framework. Later we will create simple Selenium WebDriver framework with C# for the same. Obviously you will not be able to login as we are using dummy username and password in this post. I am using Microsoft Visual Studio Professional 2015.<br /><br />
<b>Without WebDriver Framework</b><br /><br />
Below is the code which navigates to Gmail. Then it clicks on the SIGN IN link, enters email address as username, clicks Next, enters password and finally clicks the Sign in button.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwp8QNZtHeCwfQWIV7o5qLbHe8WPBZAKTkKzObPzsNsCh2bUuv5ahfSlvYdDNhuJ4eckyQD8GlakGgPNzDxhC0SJ7ca-54Qs2euy8yAauU8u0b6kzktm84Ig3XYcZ6MZJnRRHOEjIyVXI/s1600/without_webdriver_framework_1.JPG" /><br />
You can take a look at the SIGN IN link, field to enter email, Next button, field to enter password and Sign in button in the next 3 images (labeled as a, b & c).<br />
a.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-jLdxIQC5RjwCbQ4PxLRpggTI45ElMhzIKJdNAOwO999HjkREFfcVDWImXfFI2efO6fj7sGpgvDF7EjhQykhHkJy6z_1-Dijs0MgsaFDCCgtuhxeYZblWNl0zM36E30J6YUi0lYuFzSA/s1600/without_webdriver_framework_2.JPG" /><br />
b. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCCHQi3rPvEvWlML0QQiYz1X1xd4Tkq41wVh1Tv296u28ojRZciop4dC1p56DSOfznhRfsukMwFBXpcEiSI9FKvePWepZDTad9NBkFw554GEd08GYzzfDOciACUHqSnxKcidfSNX4yz80/s1600/without_webdriver_framework_3.JPG" /><br />
c. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjz5zGrkkykHvfelYRk-QtvSkVSbqY40Vrg_Ort7BGwHtxMLQjtb1XLvJz5eioSe1E70SA3YxW1eww0XvdK2UlwJpK1QfpvH6w0h5S8iSwHSVDEARIQ0iuDcRRlckDWe3rsWN-7gft85Ek/s1600/without_webdriver_framework_4.JPG" /><br /><br />
On the last step, after entering the password, when we are clicking on Sign in button, it gives an error saying something like "wrong password...", but we are good with it as we are not trying to login successfully. We are just entering all the required information and clicking on the Sign in button.<br /><br />
<b>Using Simple WebDriver Framework</b><br /><br />
1.
Start by creating a new unit test project (File (menu) -> New -> Project). Enter Solution name and Solution location.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-UerewLxgAiYVMjCianSVVeMT9mOq3R_hwvFIbrMIZCucsybNkJO_XBq0Ua29gbI_zkCQ3gbynkk43HTzNtZGXHYBArRNQYOqGN-kEOimrBzhQVEbj8HeXeiE-Pq2sY5Hhyv3kUq-ms8/s1600/Selenium_wedDriver_Framework_C%2523-1.JPG" /><br /><br />
2.
In Solution Explorer, change the name of unit test from UnitTest1 to loginTest.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizASPHSbCEgy08IgEhUQvSvpyTDs2-ouIbDwXn_xcHJq9uH3pfrJKGWkmyEJOAAlHZS8C7BVJg6a4L5OrdXMU0D2NzMqfRMVMYDqJRyqJp6blkpwIvyPaJGmC0_Xwx3qG_kuhyeJ3pqN4/s1600/Selenium_wedDriver_Framework_C%2523-2.JPG" /><br /><br />
3.
In loginTest.cs file, change the name of method <i>TestMethod1()</i> to <i>EnterCredentialsForLogin()</i>.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWsLEiozPIMwHPF-dtBXmyyZDLtycqs6ceNltUQ33ENAUX0jXOrU2rQQsBRzxEAgfyY__dl4WDWdZuSprDpObHXQf1QWlnhwKhLtFg2UmqXKydbbiXohswjSFJ9l7hziEGt89OsIAum24/s1600/Selenium_wedDriver_Framework_C%2523-3.JPG" /><br /><br />
4.
Write below snippet of code in <i>EnterCredentialsForLogin()</i> method. This is how we will create our UI tests. You will notice that how easy it is to read and understand the below code like with loginPage.GoTo() we are navigating to the desired URL, then with loginPage.ClickSignInLink(), we are clicking on the Sign In link and so on. The code will show the red squiggly lines under the loginPage. Let it be as it is for the time being. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlA1EZAKjmC3bnOiK-Xy5cyR9VmCpTQxAooO_lYKz4vODdppdo0pQ4ahnQOPPR9RvYpXcqYA9ff6-KasYwYb7LcW7Yo2TLEGsHhME2ryRDkZrYjts1Q9BgGr70fQWVOkUtnHf6vlF5O3c/s1600/Selenium_wedDriver_Framework_C%2523-4.JPG" /><br /><br />
5.
Right-click on Solution "MyTests" and add a new project of type Class Library. Name it UILibrary.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEWHaFMeZfrWAYrvLuT0Th_5yHU7R6rq4DPcTyKrg6xVv7r2QYjwv57Ri9zZMzUktgRbDZGnhkNU5bfPm4rW4V8RJts9eFCBRzBI5OluV4hr3VM-uHInzuBwWT1C-gzosmsDZsqtHckFc/s1600/Selenium_wedDriver_Framework_C%2523-5.JPG" /><br /><br />
6.
Change the name of Class1.cs under UILibrary to loginPage.cs<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2KHPStb93MQRLVBrQCgcyDJN_PeDgWstSEZ43zJpgUL6XZkIoMIupNeSOpnooI430O1_SPfzc68wt-N4rSjAqx2FqYjlK4nurUQXIwFGb2XmRQx02Uj_EyUrdpPoA9MKNduTCaa9BnSk/s1600/Selenium_wedDriver_Framework_C%2523-6.JPG" /><br /><br />
7.
Type the code (as seen in the below image) in loginPage Class in loginPage.cs file.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPfJOiVOWfmiPJjcQDCKM1VOgLd-4Sg2RgvYkuRwjQTgp2gdS8wyOEY46tokJqorQkMha8btD5cDgMqmAHEFvpaEOKQmq9Dwhj214PgNpe9qy9pWXnh4IxcGNDezJtzuSy4bzsEdibFVM/s1600/Selenium_wedDriver_Framework_C%2523-7.JPG" />
<br />
Remember our <i>EnterCredentialsForLogin()</i> method in loginTest class. We are calling different methods for example GoTo(), ClickSignInLink() like loginPage.GoTo(), loginPage.ClickSingInLink() and so on in it. We have written these methods in loginPage class as can be seen above.<br /><br />
<i>GoTo()</i> navigates to provided URL. <br />
In <i>ClickSignInLink()</i> method we are clicking on the SignIn link after finding it using CSS selector.<br />
In <i>EnterUserName()</i> method we are waiting until the element is visible and then entering email address in it after finding it using Id.<br />
In <i>ClickNext()</i> method we are clicking the Next button.<br />
In <i>EnterPassword()</i> method, similar to EnterUserName() method, we are first waiting until the element is visible and then entering the password in it after finding it.<br />
With ClickSignInButton() method we are just clicking the SignIn button.<br /><br />
8.
In UILibrary add references for Selenium WebDriver and Selenium WebDriver Support.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhgLqaQvhSHzhpPDu9gP46lyd9fpbnAKGkDxB51MrJvIM22AkR2vng1w8X-1oXJF5_LVDUTBgkx2w2vJeEhz0wbRgKXnwr2Gc7b4AKUqUGh5aJpGIgqSFj8YVt6wOWMqngt26HUwdpE3k/s1600/Selenium_wedDriver_Framework_C%2523-8.JPG" /><br /><br />
9.
Also add namespace "using OpenQA.Selenium.Support.UI;" & "using OpenQA.Selenium;" in loginPage.cs.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYZ049Ufoe6p-eg6EAeIk2gXWEgIXGGeUzDenFVVnOLaHrIzYO2eijLSIu2B96Xj92dQumEPAbhyH69e-zHG8mvOUeH2thkuxr97L82k14AIvtsZmkDiSl0Ddo-PDdne984lwuPkf0hN4/s1600/Selenium_wedDriver_Framework_C%2523-9.JPG" /><br /><br />
10.
Add a new CS file with name Driver.cs under UILibrary.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb4hAP-zK4B7G7UQWKdSW9DEwdZQ9GbNGsUIMhaOIVJsk9rccnaIVvrUCClWkM5mRko2QmYSt0qKHIceW6337Hub2X4-uaevI6eQrK9u7d2klHFsKogLaWreXNPExBtYu4sFPafyafzhg/s1600/Selenium_wedDriver_Framework_C%2523-10.JPG" /><br /><br />
11.
After adding the Driver.cs file, Solution Explorer looks like below.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYV1z24gYDlv0-pw8RXM94V-J2D0Q7FPru6u11yhP4RQOfCqo5SWpPUvcH_pu036mP_9le8UdHEv7wFcYi9rqDbrji54tWChyphenhyphenlSX01levM6zMrjBC1_pO89OlqFtLk-U002nChx4gOdNo/s1600/Selenium_wedDriver_Framework_C%2523-11.JPG" /><br /><br />
12.
Add below code in Driver.cs file. Add two namespaces "using OpenQA.Selenium;" & "using OpenQA.Selenium.Firefox;". Also be sure to make class "Driver" public.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkSFDvurzihJ1qiXq6NCyLoaIU0CH_JpCOMaBXAD_ugrB16h13jk2o3ntbe1UdstDryI_U-_ZcyR1drN9hdS8NxUImw12lGJmytcrXtbYK1bn-12CVk7X6ECaGhpKb6Roj48HvLVYTfYs/s1600/Selenium_wedDriver_Framework_C%2523-12.JPG" /><br /><br />
We have kept the class <i>Driver</i> as separate and are initializing the Firefox driver in it. This is being used by our methods written in loginPage class in loginPage.cs file. Suppose in future we need to expand our framework and add one more page like inboxPage and write some methods in it also. Then that new inboxPage can still use the <i>Driver</i> class in the Driver.cs file. So this makes sense to keep our Driver class separate and let others use it.
<br /><br />
13a.
Add reference to UILibrary for loginTests. In loginTest.cs, add namespace "using UILibrary".<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhguxPoMzPpcSYIXqhh9yompuPC8oRhPmDCvy7AmNy1POrMow1Bw7-_xUub04NNb0nnF3nWySonSMhRcPBIscJQLztsReDBMJVPLPC36vpDj8cV-pEaHTvQvXyCVFnkho36vV_Yw0L2L0E/s1600/Selenium_wedDriver_Framework_C%2523-13.JPG" /><br /><br />
13b.
<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimkBysiqeBROCxQribZZ9RhQR1NJcURrLQxYhCgLVMVF3UemveSwLifzGK2xNhqYMQ93yiBu0FhcCx_jXnQpcIzWklm0vM6kG6_4PO_PgWnkJ8j8xVWiOV4FOUuQGnrc8rIvYvU5tqGdU/s1600/Selenium_wedDriver_Framework_C%2523-14.JPG" /><br /><br />
15.
In loginTest.cs also add testinitialize method so the final loginTest looks like as shown below.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZoHynepYiUXDlrWPd5Rfydj9cVSCLyPyYNfftc6OYYdaWjFfXl0J_qC0v0glQJ3lJarT4pWyYIOiJcVFVzrcVyubsWQYUt6rzT4Ehzqpuyqafy328z5fmEYBBgQDoK6Panr7Gftr-8Ds/s1600/Selenium_wedDriver_Framework_C%2523-15.JPG" /><br /><br />
Now run the <i>EnterCredentialsForLogin()</i> method and see the framework working.<br /><br />
When the framework is run the control first goes to <i>init()</i> in loginTests.cs. New Firefox driver is initialized here. <i>[TestInitialize]</i> attribute contains code which we need to run before execution of each test.
Then <i>EnterCredentailsForLogin()</i> method is run. In the first line of this method <i>GoTo()</i> method of loginPage class is run which navigates to the desired URL. Then <i>ClickSignInLink()</i> method of the loginPage class is run which clicks on the Sign In link and so on.<br /><br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-22376705285969673382017-03-06T03:19:00.002-08:002017-03-19T03:09:53.043-07:00Selenium WebDriver - Find Broken Links Using C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Selenium WebDriver - Find Broken Links Using C#</h3><br />
In order to find the broken links using Selenium WebDriver we are using below example HTML page.<br /><br />
Code for our example HTML page:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhjzc68x6T6lTuWkY5oCzuTCqh-y5t6dXdrRS3atN2Qu0x3G-8meYtdiUJ3z09Ko1Dm8y1rzjVWqoDDHXioj4cdnh2UFqgk0g7YL8w_HGZEHtMTkl4LgD5XbuUVT1HwwphUpJC05uqCDI/s1600/WebDriver_find_broken_links_3.JPG" />
<br /><br />
HTML page opened in a browser:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYDi_NVqJS_5TNr1X6_gQdQbP8-njudwMwZ0SprDIGyhl2svmGDvnDWYhZjo3tDvq6P4Cpl0Yz8xi88rWP_mUhzYRE8uYhmmV6mevLXiDzLRDxJX2ifMzEy50AArrNPluNBk1V1c2G85s/s1600/WebDriver_find_broken_links_4.JPG" />
<br /><br />
Code used to find the broken links:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpd9SLC5XCCjgOCjHtQeOMUI4UoZ3GunJ5jZRrKuEyQeV5KuvfB4RaDMDbffF4-oduh5lrxWYeInlOB6n8VpzCOYQuPX6c62BYdNnsfPxb8UpekoMRk-N_E7sQ266XgmWyX-iXXTXWY30/s1600/WebDriver_find_broken_links_1.JPG" />
<br /><br />
Using Firefox we are navigating to the page on which we need to verify all links. We get all links on a page using "a" tag. Next we are iterating through the list of links and printing the Error code for each on the Console.
<br /><br />
<i>HttpWebRequest</i> Class provides an HTTP-specific implementation of the WebRequest Class. In the above code we are initializing a new instance of the HttpWebRequest Class. We are using HttpWebRequest Class to make a request to a Uniform Resource Identifier (URI). Similarly <i>HttpWebResponse</i> Class provides an HTTP-specific implementation of the WebResponse Class. This line returns an HttpWebResponse from an HttpWebRequest.<br /><br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgti-8YPzbFz2SfhteACoHiUeYsg8duI8IBGunzBEixnxr3KNOByavqE2uXZxoo2nGPvxgtRvKO-8BBPsC5oa20f09BWaAvheeNi3Hn60qPP2fZoBlNTiYJ1nsAkrUEMxSEZqZzJXFHwAc/s1600/WebDriver_find_broken_links_2.JPG" />
<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-64515739806468605152017-02-28T07:10:00.000-08:002017-02-14T20:13:48.038-08:00FindElement Chain - To Find Child Elements <div dir="ltr" style="text-align: left;" trbidi="on">
<h3>FindElement Chain - To Find Child Elements</h3><br />
You can chain findElement in order to find child elements. Below is a simple example of chaining findElement.<br /><br />
We have 3 radio buttons as can be seen in the image below and their code (using Inspect Element in Firefox). When the below web page is opened, the first radio button is checked by default.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidj7gnhNKoIjO-hk776aAUZ9SvKzAYVUB5aWaPiStyx3nUXwpGLUPrNLBoWqwcnHKdjO9DZu8KZyd21y0UdKs8M_ehp7e3rYA419B44QldlwO7qAzH5SDu0SUUl5OIuPom96xKYS2fMvI/s1600/Chain_FindElement_1.JPG" /><br /><br />
There can be many ways to find elements or child elements. One of the ways is that we can write the code by way of chaining findElement in order to check the second radio button. First findElement is using the <i>Id</i> to find an element and second findElement is using <i>Name</i>.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqhz3mASTO2A0DiR3X0SdJN1JTjIhCXe2jbU_HK9Sg9g3lqCyg7-mXyLu0WTjf5mLiCSlivBB-6pnU-RiEt4w-0pG4quX22F1ytKtVCSABh4NNp7ApHM45dhMvrG_r9pVLTFyJfauK5gg/s1600/Chain_FindElement_2.JPG" /><br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-2323820383052568062017-02-14T20:13:00.000-08:002017-05-08T04:12:59.518-07:00Sitemap - Selenium Tutorials<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Sitemap - Selenium Tutorials</h3><br />
1.
Selenium Tutorial Links [<a href="http://selenium-tutorial.blogspot.com/2012/03/selenium-tutorial.html">Link</a>]
<br />
2.
Selenium - Uploading a File [<a href="http://selenium-tutorial.blogspot.com/2012/04/uploading-file.html">Link</a>]
<br />
3.
Selenium WebDriver Installation [<a href="http://selenium-tutorial.blogspot.com/2012/04/selenium-webdriver-installation.html">Link</a>]<br />
4.
Selenium Element Locator Examples [<a href="http://selenium-tutorial.blogspot.com/2012/05/selenium-element-locator-examples.html">Link</a>]<br />
5.
Taking Screenshots with Selenium WebDriver [<a href="http://selenium-tutorial.blogspot.com/2012/05/screenshot-with-selenium-webdriver.html">Link</a>]<br />
6.
Selenium WebDriver Page Objects [<a href="http://selenium-tutorial.blogspot.com/2012/06/webdriver-page-objects-pattern.html">Link</a>]<br />
7.
Selenium WebDriver & Excel [<a href="http://selenium-tutorial.blogspot.com/2012/06/selenium-webdriver-excel.html">Link</a>]<br />
8.
Selenium WebDriver Drag & Drop [<a href="http://selenium-tutorial.blogspot.com/2012/07/selenium-webdriver-drag-drop.html">Link</a>]<br />
9.
Selenium WebDriver & Internet Explorer [<a href="http://selenium-tutorial.blogspot.com/2012/07/selenium-webdriver-internet-explorer.html">Link</a>]<br />
10.
Parameterization / Data-Driven Testing Using Selenium WebDriver [<a href="http://selenium-tutorial.blogspot.com/2012/08/parameterization-data-driven-testing.html">Link</a>]<br />
11.
WebDriver Popup Alert [<a href="http://selenium-tutorial.blogspot.com/2012/08/webdriver-popup-alert.html">Link</a>]
<br />
12.
Selenium-2 / WebDriver / Python [<a href="http://selenium-tutorial.blogspot.com/2012/09/selenium-2-webdriver-python.html">Link</a>]<br />
13.
Select / Check Check-box WebDriver [<a href="http://selenium-tutorial.blogspot.com/2012/10/select-check-checkbox-webdriver.html">Link</a>]<br />
14.
Selenium WebDriver Exception Handling [<a href="http://selenium-tutorial.blogspot.com/2012/11/selenium-webdriver-exception-handling.html">Link</a>]<br />
15.
WebDriver - Download a File [<a href="http://selenium-tutorial.blogspot.com/2012/11/webdriver-download-file.html">Link</a>]<br />
16.
Selenium WebDriver Click Link / Button [<a href="http://selenium-tutorial.blogspot.com/2012/08/webdriver-click-link-button.html">Link</a>]<br />
17.
WebDriver - Open new tab [<a href="http://selenium-tutorial.blogspot.com/2012/12/webdriver-open-new-tab.html">Link</a>]
<br />
18.
WebDriverWait, ImplicitlyWait & ExplictlyWait [<a href="http://selenium-tutorial.blogspot.com/2012/12/webdriverwait-implicitlywait.html">Link</a>]<br />
19.
Verify Broken Links / 404 Not-found - Webdriver [<a href="http://selenium-tutorial.blogspot.com/2013/01/verify-broken-links-404-not-found.html">Link</a>]<br />
20.
How to send a mail - Selenium Webdriver [<a href="http://selenium-tutorial.blogspot.com/2013/02/how-to-send-mail-webdriver.html">Link</a>]<br />
21.
Scrolling a Page - Webdriver [<a href="http://selenium-tutorial.blogspot.com/2013/02/scroll-page-webdriver.html">Link</a>]<br />
22.
Selenium Assertions [<a href="http://selenium-tutorial.blogspot.com/2013/03/selenium-webdriver-assertions.html">Link</a>]
<br />
23.
Selenium Webdriver - How to Know if Checkbox is Checked / Set or Not [<a href="http://selenium-tutorial.blogspot.com/2013/04/webdriver-check-if-checkbox-is-set.html">Link</a>]
<br />
24.
Selenium WebDriver - Database Connection [<a href="http://selenium-tutorial.blogspot.com/2013/05/webdriver-database-connection.html">Link</a>]<br />
25.
Selenium Webdriver - Access Data from Web Tables or HTML Tables [<a href="http://selenium-tutorial.blogspot.com/2013/05/access-data-webtables-webdriver.html">Link</a>]<br />
26.
Selenium WebDriver - Verify Text Present [<a href="http://selenium-tutorial.blogspot.com/2013/07/webdriver-verify-text-present.html">Link</a>]<br />
27.
Highlight an Element - WebDriver [<a href="http://selenium-tutorial.blogspot.com/2013/08/highlight-element-webdriver.html">Link</a>]<br />
28.
NoSuchElementException - Webdriver [<a href="http://selenium-tutorial.blogspot.com/2013/09/nosuchelementexception-webdriver.html">Link</a>]<br />
29.
Simulating Keyboard Keys - WebDriver [<a href="http://selenium-tutorial.blogspot.com/2013/09/simulating-keyboard-keys-webdriver.html">Link</a>]<br />
30.
iframes in Selenium Webdriver [<a href="http://selenium-tutorial.blogspot.com/2013/10/iframes-in-selenium-webdriver.html">Link</a>]<br />
31.
Closing the browser window at the end of Selenium test [<a href="http://selenium-tutorial.blogspot.com/2013/11/webdriver-close-browser-window.html">Link</a>]
<br />
32.
WebDriver with Visual Studio [<a href="http://selenium-tutorial.blogspot.com/2014/01/webdriver-with-visual-studio.html">Link</a>]<br />
33.
WebDriver - Select Option from Right Click Menu [<a href="http://selenium-tutorial.blogspot.com/2014/02/select-option-right-click-menu.html">Link</a>]<br />
34.
Read / Get Text from Textbox [<a href="http://selenium-tutorial.blogspot.com/2014/03/read-text-from-textbox.html">Link</a>]<br />
35.
WebDriver - Wait for page load [<a href="http://selenium-tutorial.blogspot.com/2014/03/webdriver-wait-for-page-load.html">Link</a>]<br />
36.
Select a Value from Drop-Down [<a href="http://selenium-tutorial.blogspot.com/2014/04/select-value-from-dropdown.html">Link</a>]<br />
37.
Selenium WebDriver - Cheat Sheets [<a href="http://selenium-tutorial.blogspot.com/2014/04/selenium-webdriver-cheat-sheets.html">Link</a>]<br />
38.
Control Execution Speed - WebDriver [<a href="http://selenium-tutorial.blogspot.com/2014/05/setspeed-execution-speed-webdriver.html">Link</a>]
<br />
39.
Difference between findElement & findElements [<a href="http://selenium-tutorial.blogspot.com/2014/05/difference-findelement-findelements.html">Link</a>]<br />
40.
Stale Element Reference Exception - Webdriver [<a href="http://selenium-tutorial.blogspot.com/2014/06/staleelementreferenceexception-webdriver.html">Link</a>]<br />
41.
Install Selenium WebDriver with Eclipse [<a href="http://selenium-tutorial.blogspot.com/2014/07/install-selenium-webdriver-eclipse.html">Link</a>]<br />
42.
Selenium WebDriver - Iterate a list of WebElements [<a href="http://selenium-tutorial.blogspot.com/2014/08/iterate-list-of-webelements.html">Link</a>]<br />
43.
What is Selenide [<a href="http://selenium-tutorial.blogspot.com/2014/09/what-is-selenide.html">Link</a>]
<br />
44.
Selenium WebDriver Vs JMeter [<a href="http://selenium-tutorial.blogspot.com/2014/09/selenium-webdriver-vs-jmeter.html">Link</a>]
<br />
45.
WebDriver - Get Elements by Text / XPath [<a href="http://selenium-tutorial.blogspot.com/2014/10/webdriver-get-element-by-text-xpath.html">Link</a>]<br />
46.
CSS Selectors for WebDriver [<a href="http://selenium-tutorial.blogspot.com/2014/11/css-selectors-for-webdriver.html">Link</a>]<br />
47.
Webdriver isDisplayed() [<a href="http://selenium-tutorial.blogspot.com/2014/12/webdriver-isdisplayed.html">Link</a>]
<br />
48.
WebDriver - pageLoadTimeout() [<a href="http://selenium-tutorial.blogspot.com/2015/01/pageloadtimeout-webdriver.html">Link</a>]<br />
49.
Difference between Selenium WebDriver & Selenium RC [<a href="http://selenium-tutorial.blogspot.com/2015/01/difference-webdriver-selenium-rc.html">Link</a>]
<br />
50.
WebDriver - Clear text from textbox [<a href="http://selenium-tutorial.blogspot.com/2015/02/webdriver-clear-textbox.html">Link</a>]<br />
51.
Selenium WebDriver - Set Focus [<a href="http://selenium-tutorial.blogspot.com/2015/02/selenium-webdriver-set-focus.html">Link</a>]<br />
52.
WebDriver - Switch to a New Window [<a href="http://selenium-tutorial.blogspot.com/2015/04/webdriver-switch-to-new-window.html">Link</a>]<br />
53.
How to use locators in Selenium WebDriver [<a href="http://selenium-tutorial.blogspot.com/2015/05/using-locators-selenium-webdriver.html">Link</a>]<br />
54.
WebDriver - Close a Browser Tab [<a href="http://selenium-tutorial.blogspot.com/2014/12/webdriver-close-browser-tab.html">Link</a>]<br />
55.
WebDriver.Close() Vs WebDriver.Quit() [<a href="http://selenium-tutorial.blogspot.com/2014/11/webdriver-close-webdriver-quit.html">Link</a>]<br />
56.
Selenium WebDriver Interview Questions [<a href="http://selenium-tutorial.blogspot.com/2015/07/selenium-webdriver-interview-questions.html">Link</a>]<br />
57.
Selenium WebDriver - Set Date in Datepicker [<a href="http://selenium-tutorial.blogspot.com/2015/07/set-date-in-datepicker-webdriver.html">Link</a>]<br />
58.
Selenium WebDriver & JUnit [<a href="http://selenium-tutorial.blogspot.com/2015/04/selenium-webdriver-junit.html">Link</a>]<br />
59.
WebDriver - Launching a Browser [<a href="http://selenium-tutorial.blogspot.com/2015/07/webdriver-launching-browser.html">Link</a>]<br />
60.
What are the different types of Drivers available in WebDriver? [<a href="http://selenium-tutorial.blogspot.com/2015/08/drivers-available-in-webdriver.html">Link</a>]
<br />
61.
WebDriver Browser Navigation Commands [<a href="http://selenium-tutorial.blogspot.com/2015/09/webdriver-browser-navigation-commands.html">Link</a>]<br />
62.
Verify if an image is displayed on a webpage [<a href="http://selenium-tutorial.blogspot.com/2015/10/verify-if-image-is-displayed-on-webpage.html">Link</a>]<br />
63.
Selenium WebDriver with Visual Studio [<a href="http://selenium-tutorial.blogspot.com/2015/10/selenium-webdriver-visual-studio.html">Link</a>]<br />
64.
WebDriverWait & ExpectedCondition [<a href="http://selenium-tutorial.blogspot.com/2015/11/webdriverwait-expectedcondition.html">Link</a>]<br />
65.
Page Object pattern [<a href="http://selenium-tutorial.blogspot.com/2015/12/page-object-pattern.html">Link</a>]
<br />
66.
WebDriver - Full Page Screenshots [<a href="http://selenium-tutorial.blogspot.com/2016/01/webdriver-full-page-screenshots.html">Link</a>]<br />
67.
Difference Between Debug & Trace [<a href="http://selenium-tutorial.blogspot.com/2016/01/difference-between-debug-trace.html">Link</a>]<br />
68.
WebDriver - FindElement Examples [<a href="http://selenium-tutorial.blogspot.com/2016/02/webdriver-find-element-by.html">Link</a>]<br />
69.
WebDriver - Select Item From Drop-down [<a href="http://selenium-tutorial.blogspot.com/2016/02/webdriver-select-item-from-dropdown.html">Link</a>]<br />
70.
WebDriver MouseHover Actions [<a href="http://selenium-tutorial.blogspot.com/2016/04/webdriver-mousehover-actions.html">Link</a>]<br />
71.
WebDriver Page Object Model [<a href="http://selenium-tutorial.blogspot.com/2016/05/webdriver-page-object-model.html">Link</a>]<br />
72.
WebDriver - Double-click an Element [<a href="http://selenium-tutorial.blogspot.com/2016/05/webdriver-double-click-element.html">Link</a>]<br />
73.
Drag & Drop - Selenium WebDriver [<a href="http://selenium-tutorial.blogspot.com/2016/06/drag-drop-selenium-webdriver.html">Link</a>]<br />
74.
WebDriver - Find All Links [<a href="http://selenium-tutorial.blogspot.com/2016/06/find-all-links-webdriver.html">Link</a>]<br />
75.
WebDriver DefaultWait in C# [<a href="http://selenium-tutorial.blogspot.com/2016/07/webdriver-defaultwait.html">Link</a>]
<br />
76.
WebDriver - Select Multiple Items in a List [<a href="http://selenium-tutorial.blogspot.com/2016/08/webdriver-select-multiple-items-in-list.html">Link</a>]<br />
77.
Assert.AreEqual - with Examples [<a href="http://selenium-tutorial.blogspot.com/2016/08/assert-areequal-selenium-webdriver.html">Link</a>]<br />
78.
Selenium WebDriver Table Examples [<a href="http://selenium-tutorial.blogspot.com/2016/09/selenium-webdriver-table-examples.html">Link</a>]<br />
79.
WebDriver - CSS Selector Examples [<a href="http://selenium-tutorial.blogspot.com/2016/09/webdriver-css-selector-examples.html">Link</a>]<br />
80.
WebDriver - Read from Excel [<a href="http://selenium-tutorial.blogspot.com/2016/09/webdriver-read-from-excel.html">Link</a>]<br />
81.
Assert.IsTrue Examples [<a href="http://selenium-tutorial.blogspot.com/2016/10/assertistrue-examples.html">Link</a>]
<br />
82.
WebDriver - log4net C# Examples [<a href="http://selenium-tutorial.blogspot.com/2016/11/log4net-csharp-webdriver-examples.html">Link</a>]<br />
83.
WebDriver - Using Firefox, IE & Chrome Browsers [<a href="http://selenium-tutorial.blogspot.com/2016/12/webdriver-using-firefox-ie-and-chrome.html">Link</a>]<br />
84.
WebDriver - geckodriver.exe File Does Not Exist [<a href="http://selenium-tutorial.blogspot.com/2016/12/webdriver-geckodriver-exe-file-does-not.html">Link</a>]<br />
85.
Webdriver - Upload file using C# [<a href="http://selenium-tutorial.blogspot.com/2017/01/webdriver-upload-file-csharp.html">Link</a>]<br />
86.
FindElement Chain - To Find Child Elements [<a href="http://selenium-tutorial.blogspot.com/2017/01/findelement-chain-to-find-child-elements.html">Link</a>]<br />
87.
Selenium WebDriver - Find Broken Links Using C# [<a href="http://selenium-tutorial.blogspot.com/2017/03/webdriver-find-broken-links-c-sharp.html">Link</a>]<br />
88.
Selenium WebDriver - Framework Using C# [<a href="http://selenium-tutorial.blogspot.com/2017/03/selenium-webdriver-framework.html">Link</a>]<br />
89.
Step by Step Guide to Simple Selenium WebDriver Framework in C# [<a href="http://selenium-tutorial.blogspot.com/2017/03/selenium-webdriver-framework.html">Link</a>]<br />
90.
Selenium WebDriver Framework using PageFactory [<a href="http://selenium-tutorial.blogspot.com/2017/04/selenium-webdriver-framework-pagefactory.html">Link</a>]<br />
91.
Selenium WebDriver - Reading XML Files [<a href="http://selenium-tutorial.blogspot.in/2017/05/webdriver-reading-xml-files.html">Link</a>]<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-4364324254451832962017-01-04T03:15:00.000-08:002017-03-19T03:11:05.005-07:00Webdriver - Upload file using C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Webdriver - Upload file using C#</h3><br />
We will look at two ways to upload a file in Selenium WebDriver - one is uploading a file using AutoIt and second is uploading a file using SendKeys.<br /><br />
<b>Example 1 - </b><b>Uploading a File using AutoIt</b><br /><br />
<a href="https://www.autoitscript.com/site/autoit/downloads/">Download AutoIt</a> and Install it.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDkA13qlrF5Wc0xMItjqPtVljOIjYpKlLGJQftP5dXNw47RrZGtKh2nd-LVsSoBGp9eV5Xp0tcCeHTy-o_C6Ov4LQOXRoGk-ir1hh8rftppYTAJGRDWipEoGlOIuluXq12aKXybE82gfQ/s1600/WedDriver_Upload_File_13.JPG" /><br /><br />
Install AutoIt. Below are the screenshots I took during AutoIt installation.<br />
1. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMmMCnebYta2w7OJcOKd8uFXfzicZG-lH1DtdBrI1-65KWnhxsXohg3rSflTl-hh3HSkoPmw_d6y8O4fYOihKetLiD4Td1-wqwSryrvSptjh3n5ZuMBQ7DDvTtxr2nm9cnohlDzNZDFto/s1600/WedDriver_Upload_File_1.JPG" />
<br /><br />
2. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOrCm6n0JbZ05vAeKKfV4zx30BQqq1mFnj8WK3x-7Wm-FNI7auiSvFkOLkvTtOAhhRYezrxlXkrhIanGlB4afbF5kiD7XqPfE_VhQA4LWl-HlxTdwCZXBSNYbVuZdIrA3ohFTlW_tjnuU/s1600/WedDriver_Upload_File_2.JPG" />
<br /><br />
3. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFct-ouqUtw1D8NdcmxlB3eosuIBWm4zuc5WbwtW2XUw-Svv1c6VgMUaQk37hGDSBmsHXh4Rx6zks8BNXg-EfEJZcKxGOiVc-bd93F3rFcFRe-K09a1rEzyUuDXPVckN65Z_M05mgd8bI/s1600/WedDriver_Upload_File_3.JPG" />
<br /><br />
4. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihADhaP61HsSVMifIcJA5ITmACMrf83syfe1zLw-RX2Lq8su1yfN3vhz_ObHFjZ-8BOp-url6bnREQGSdBbiwP09K_fylDbCdP2LHYV6OiEO45DPJi_eLNvbKbDJVwBL0vaIs113OwY7o/s1600/WedDriver_Upload_File_4.JPG" />
<br />
<br />
5. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-Uqa8ttUK2TY1Zw9vvgHHQ2qXEHWWBUofpw1hf-QD7Gv5TkouSiI4WCyebo2bU0WTXrSc9Z_KCKW_hVSIrNoAG7wovlquci9BTG6pukfTD40svTH9md47_Q2WSADD5X3JELMRMsDmJHE/s1600/WedDriver_Upload_File_5.JPG" />
<br /><br />
6. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgepsFH-OvaGKMosLyTiht3ZEDOhtARmFQM7LNG2Sz_49aZy7DsIF8KkySCTO9HPFl2DBKSDFErLdBOwWBSU-7zL5iB9Aj_rQWVEcFa2cDGGD4ARFdMnPSCb1gOFzjsrRauM4L0FKwXNLw/s1600/WedDriver_Upload_File_6.JPG" />
<br /><br />
7. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsqtzrqItE-fg5JIs7VsEwDZj_hUmRsoADrYGeYHhYlutAHtJwJLqCZAPGW_pV2y5j4Srr68elb_IAsGMHXtcZWqTb5xewHoXwM8JtWf5I7LKSdZvaOAnmtWUfEprQTxxgkzaleVoZDzQ/s1600/WedDriver_Upload_File_7.JPG" />
<br /><br />
8. <br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-B963mMMjFvJy50dkIeE_xhGxgmqWOg8JG3QgUdudf2LNb0A6S9dNS5tqsew4btNnb63bqudfk_wzuL59XPJKQR_miPFkCx7CT-s6RxH7O4MoEgD9U1QODqnAYoyb62mIJqdsx0e5IgI/s1600/WedDriver_Upload_File_8.JPG" />
<br />
<br />
I have used the below web page to upload the file. Below I have right-clicked on the Browse button and selected "Inspect Element". You can use any other web page to upload the file and obviously change the dependent lines in the code accordingly.
<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZImCJeQ6rqusDV1Vzvql5nMfQ-oAGxi19BBboxjUMkT8ltg4bDovfLI6yaRikkLR8Gr4lDX2Eu2Y6wZE0610uRRkmFZ6N4DWZoSC0S2bpcQxDHNLXV7bn4VLMvMyFUMRtaPkJMPCwdsY/s1600/WedDriver_Upload_File_12.JPG" /><br /><br />
Create a new test in Visual Studio.<br />
Add References: WebDriver, WebDriver.Support, AutoltX3Lib.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlqHC1eL9dMI0xyXYIaNf_NiC5OjYEKgvPVJ0pihzOrze25VxhACaCXwh2ReAouvSNx5x5XdbumUSUV2jgu4VD18S6czVLTsRTeipPicvKnjlnYO_VfYu1P2UyBxsMBGhxJravos8m1Oc/s1600/WedDriver_Upload_File_9.JPG" /><br /><br />
Write the below code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4IV6pNbDcE9NBllnjfSLayeyRHww9ZafsAeSJ8CoH-PaGp4KKMt85dlXW69-ABH4QZwSwnkMNh9rzcy3dYLitQTtA7it4Mu6QkCJ0-5YT9KabAsFGECVQ1yIicI6h_IJ6I66m5LuvZCw/s1600/WedDriver_Upload_File_11.JPG" /><br /><br />
Using Firefox we are opening our html page where we need to upload the file. Then we are finding the Browse button and clicking on it in order to open File Upload dialog. Create a new interface for AutoItX3 and then use it to access the methods/ properties of AutoItX3. With <i>au3.ControlFocus</i> we are setting the focus on the File name field of the File Upload dialog. With <i>au3.ControlSetText</i> we are giving path of the file to be uploaded i.e. pasting the filename (along with path) in the File name field. At last we are clicking on the Open button.
<br /><br />
You can find more about AutoIt methods like <a href="https://www.autoitscript.com/autoit3/docs/functions/ControlFocus.htm">ControlFocus</a>, <a href="https://www.autoitscript.com/autoit3/docs/functions/ControlSetText.htm">ControlSetText</a> & <a href="https://www.autoitscript.com/autoit3/docs/functions/ControlClick.htm">ControlClick</a>.<br /><br />
For passing parameters to functions like ControlFocus etc. you can use AutoIt Window Info. Its like an Object Spy in QTP/UFT. I have added parameters like "File Upload", "Edit1" etc. using this.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtzznS2q22LgtJzpOg8KneTsk7mSGPTdwezPpCGCGMgGqb_NL_MO8q-93TcKRpbnxUeAciYGVUcIeEQmGzgfcMVzxN5xS4_xSvLrRAZy3AIXUv3zhUee0gJHolKlsJPQNrEYP5XbU2I_E/s1600/WedDriver_Upload_File_15.JPG" /><br /><br />
After running the code, the upload file page will be shown like below:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXjs2s7AGdWpZrbDGjkT-oLlHwXWEEMjiRjBsCeIhmyEE2Z2ZMsIuHcA-y52nwc7N2n8oSBKxkvewYiruM9qo8hlHo2ft1xhN36hW1lDy3W3hpgIhmbmOcphyphenhyphen8e2vT7YSm1CB2wkhBxZ8/s1600/WedDriver_Upload_File_10.JPG" /><br /><br />
<b>Example 2 - </b><b>Uploading a File using SendKeys</b><br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8vqkUQBy6MDfe8rtoFEkyjtjOJYFxfUJs2jfaoVtq48O65MOf8-hhlD3VGS-sXtnT0dQE-pk-erARcLPz1tMCmgnnYhNiHqtOd_3AKU27r69MdMjBVL7XElhyphenhyphenQWivFF3tc1OUVNRSQuE/s1600/WedDriver_Upload_File_14.JPG" /><br /><br />
For SendKeys to work make sure you add "System.Windows.Forms" in references and also add it in the code.<br /><br />
Here, using Firefox we are opening our html page where we need to upload the file. Then we are finding the Browse button and clicking on it in order to open File Upload dialog.
<br /><br />
<i>SendKeys.SendWait</i> method actually sends the given keys to the active application, and then waits for the messages to be processed. Here we are sending the filename (along with path) to be uploaded and then hitting Enter key.
<br /><br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-65972109053831862752016-12-20T01:08:00.004-08:002016-12-20T01:09:52.963-08:00WebDriver - geckodriver.exe File Does Not Exist<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - geckodriver.exe File Does Not Exist</h3><br />
While running tests using WebDriver & C# in Visual Studio with Firefox, if you happen to receive below exception:<br /><br />
<font color="red"><i>OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable.</i></font><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAUjLzWj7y9sdv5_fVr4bKYkcXX4iRNNSBLFQK7u73NR-cLmy59zk0F6vTyxTtYADFHzHcFtsqwt5BcPrSFp5nJseDk-bmzw53dqPt69SrnY3HEdTrewminfMBDYPvWE6KlnmWc8-X2ZE/s1600/geckodriver-2.JPG" />
<br /><br />
then just go to <a href="https://github.com/mozilla/geckodriver/releases">this</a> link and download the geckodriver.exe file.<br />
<br />
One of the solution is to put the file (<i>geckodriver.exe</i>) in the bin\Debug folder of the current project which you are trying to run. You can right-click on the project name, choose 'Open Folder in File Explorer',
<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk8ziPw4EEAZB1BUNpIRJuqwzlfDMiPSuxIbKvBv4T1H3LiYIqk-tdVSTjVgC3AA3iQN6eofFSvbULMYE7DK1-L9Nk39ll_aKh35LpJtg9WrrSvbXwS_Y7GrhwI-yAebWWsUMqeXD1zYA/s1600/geckodriver-1.JPG" />
<br /><br />
go to <i>bin\Debug</i> and paste the geckodriver.exe file in there.<br /><br />
Now the test should run fine.
<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-37410976324972422292016-12-03T07:26:00.000-08:002016-12-03T07:28:32.013-08:00WebDriver - Using Firefox, IE & Chrome Browsers<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Using Firefox, IE & Chrome Browsers</h3><br />
You can see examples of using different browsers. First example shows using Firefox browser, second example uses IE and lastly you can see an example for Chrome browser.<br /><br />
Create a new project.
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-tbeCj0FWi30CvRBFBH52TXL_so6MJmMtQNsC1_k8WiETFbUj5v82puni6vGJxUNawXwmVBsHR0dlTv5xRjT34WKWoJ9GsTf0J8u0AQ04bGQtuwdQR2B7SXshc7G8LdFSwIf1ItGMiRE/s1600/webdriver-using-different-browsers-1.JPG" /><br /><br />
Add reference for Selenium WebDriver.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPGyhAlKfBvDfeBkxgoTNy0yhdyjrVAen6OHQ9VSccUJ7I7rKILklyt1C54dnBOOXXywYNFIVAv_2UvSqoa4Lz4RycB0yUOlz8Wz9FmeIsAbFXvECDLnkcopQjSHHCMQVVs8JrmHIC2XM/s1600/webdriver-using-different-browsers-2.JPG" /><br />
<br />
<b>Example 1 - Launching Firefox Browser</b><br /><br />
Below code uses Firefox browser and opens www.google.com.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrITcmqzS-blZJr8HV-msjCssOtI5svzBhEwn9pKmV8Wxk0v3xkPPzPue4yJPT0kVZ81gLGclnQl35CaTjWcq5CguZsgfRAr0v9g3rPg_R3TKvDYW7I_ADOKET9tNWSjBTfehaoWwoAjk/s1600/WebDriver-using-firefox-driver.JPG" />
<br />
<b>Example 2 - Launching IE Browser</b><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYcDpiNikMY3wGjgzl9jnJ5gzVHzwT8QwdAEOL4euane-vDdUChCUFqYf4gmzQZyYfeIYmvXduuQRQgUsTN7D3loTdCWYtk1uvKfo8tUX1igvvHJ8X3IvtwWL-7nFmckPlbanNlCFahVY/s1600/WebDriver-using-IE-driver.JPG" />
<br /><br />
While running the above code if you do not give the path for IEDriverServer then below exception (DriverServiceNotFoundException) will be shown. So you have to download the <a href="http://selenium-release.storage.googleapis.com/index.html">IEDriverServer</a> and give its path (for example @"D:\IE\") as shown above.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjy2TLwP6p_O32wUXk64Z9ig-vReU8ncMY0Y3a8IIC7DL1zcvQspY1pXWt40c68MwHsQYlivAeEqNgAUZYyEI_BYolMHlSc_gCN8J1gGkR8NnzrAt7HhJ12VxuQCputW2m0SVE1J5zf3rY/s1600/WebDriver-using-IE-driver-2.JPG" />
<br /><br />
<i>For using Firefox we don't need to give this Driver Server path as its built-in for Firefox.</i><br /><br />
<b>Example 3 - Launching Chrome Browser</b><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSaaAzDPfKLJs7S-HQ9RimSEjZv_5imsjFxdPGMDgwtPrdY9T3X8tvXOnVQ2RDXmh6mHFBWmF_B2ftkwDEcYRG3Eg8PVDj-8JyiyEQcI4WJ5-WA1z2h1hzQZCopAyu_i2M9eE5lTq4ins/s1600/WebDriver-using-Chrome-driver.JPG" /><br />
Similar is the case while using the Chrome browser. You have to download the chrome driver and give its path otherwise below exception is thrown.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4fjVxJAnRbPqyg31uJTQeKa9ULr7v4El1wZlhOH2l4cP1Dd1q9J6CRInKQNZ_InWa-SYPVhs-iImDGk6UMJ9B_y_-kQvN78yxt2gwqlmf-GJdqu3FK6f4f4hp1AM1oj9HKqpD9k2pJuU/s1600/WebDriver-using-Chrome-driver-4.JPG" />
<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-91851646862872668942016-11-01T21:13:00.000-07:002016-11-06T22:10:35.377-08:00WebDriver - log4net C# Examples<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - log4net C# Examples</h3><br />
<b>log4net</b> is a logging tool for .Net. log4net is a tool to help coders output log messages to different output targets. We will look at <b><i>how to use log4net with WebDriver using C#</i></b>. If you are new to log4net then you are at the right place. Below are two very simple & detailed examples of log4net in order to give it a start. <br /><br />
<b>Example 1 of log4net</b> - <b>logging output to console</b><br /><br />
In Visual Studio (I am using Visual Studio version 12) open a new project (Unit Test Project).<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjD14DpYNDdAXxsVYRqR46dL2j6Ji5VOUW7upo_-ZGdOYtyOSz_keLuBethk9E2TwnvY_PeVW3_IG5hp9W2GnDa8OecOjXW_T-tjIk64op5rb75KbYlHkgsFFoIXRKKaE_9RAhtpYyDOhk/s1600/log4net_example_1_1.JPG" />
<br /><br />
Install NuGet packages for Selenium WebDriver and Selenium WebDriver Support Classes.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhccNDSJ0qfn8zANOu5iTqFtPad2MEJ0BBXVZW9HZ42628-Wojd4PxgwxxQ6K9Hc5GaH88974W_mIm0ySFa_JQz4Eo2dGq4z-h_c5VbqOwdQogixjWy66H3G2d68Exwob0oNbfL1lAPHaE/s1600/log4net_example_1_2.JPG" />
<br /><br />
Add a reference for log4net.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5PspBa94le3r9wjdyoFtn5Ra8q1qXpl9QcQQpD1i4symhvTdopbuipWOFdvX5u8p0l2UQ61tjlunL-nICQLSqgekDdDPqyCUhWUYR7jJO1grlcTx8ZrLl4IFImOztbWPhm5Ky-PAJumU/s1600/log4net_example_1_3.JPG" />
<br /><br />
After adding all the references, the Solution Explorer looks like as seen below:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0M-76oDrUr6KKmHfdvb6cNjnvxa4ufzFsPyuWvfKWjWAaOCD1ljLYjLxybucJHrf5XvSyVa9Rists20L73dAu_wE6FevWpn-Pmdd_ENDroY3f97KOIhdhnnN_A0ArhBbgDKSYnuWOdXo/s1600/log4net_example_1_4.JPG" />
<br /><br />
We will use below code for our example: <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-XUjHsYkCTsIIpqM2ZHXcalO4vAr3nvtHi55k8fm_MfNpP6z0mz-mIBugpQE-BP1n2Y4xGYDc95rVEr5IIOtlwSb_8CfEV2zhQSfLESxHCSiuI7QJtM1ZVhfeFt7jIT5jMYXXnxhlHDA/s1600/log4net_example_1_5.JPG" />
<br /><br />
Output (console) after running the above code is:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1srYtgEF-oPW2ieZaS4oagdWN2Vvzt0fPbose-6x4DbpFXeQzmABqCSvGjxSLL35Q8pGUmA2d7c2xjReOnAFBJOIth2jn9dnR94l0aPuyyfHnHPoQSrRsGlrXD4CMf2Cu4eb8HccNdwY/s1600/log4net_example_1_6.JPG" />
<br /><br />
Lets go through the code used above and see what exactly we have done.
We are using an ILog interface to log messages. LogManager is being used to obtain logger instances that implement this interface. <br />
<i>LogManager.GetLogger</i> method retrieves or creates a named logger.<br /><br />
Then we are using the BasicConfigurator class's 'Configure' method to initializes the log4net logging system.
BasicConfigurator class allows very simple programmatic configuration of log4net. BasicConfigurator.Configure initializes the log4net logging system using a ConsoleAppender that will write to Console.Out.
<br />
<br />
From the log4net message levels (ERROR, WARN, DEBUG, INFO etc) we are using INFO level, which logs INFO level messages.<br /><br />
Then with the help of Firefox we are opening www.google.com and again printing using logger.info in the last line.<br /><br />
In the output, the log messages are formatted using the PatternLayout layout object with the DetailConversionPattern layout style.
<pre>
Default is %r [%t] %p %c %x - %m%n
</pre>
Where<br />
r = Equivalent to timestamp. The milliseconds between the start of the application and the time the log entry was made.
<br />
t = Used to output the name of the thread that generated the logging event. Uses the thread number if no name is available.<br />
p = Equivalent to level e.g. ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF<br />
c = Equivalent to logger<br />
x = Equivalent to ndc<br />
A Nested Diagnostic Context, or NDC
<br />
m = Equivalent to message
<br />
n = Equivalent to newline
<br /><br />
Another way to write th above pattern is:<br />
%timestamp [%thread] %level %logger %ndc - %message%newline<br />
<br />
If you observe the output of the above code, it shows <i>null</i> before text "Here is a ...". So in order to make sure that it does not show <i>null</i> in the output we can add the following line in the code. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieCUpD-l-Fxcuaqf8mTRpc9jgmnKlSrnRQKucHGS0TA8r92L-ZYu938qCqo9EDNTLGq0LvMbu-u9GE1avRsgdNx9QbF-uRV8Zaj5FTt0Gb9cLhMXwq0qYi8D-Pd_ENoPqubIIXeWY_8yk/s1600/log4net_example_1_7.JPG" />
<br /><br />
Output (console) after running the above code is:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWDgfH0XAq2VV9NAyHKic2qpZHl7cOR-dxmeveygiNFeR1OVDD_5EPqLpOdbollf-uzEXyzgf5ObgpKCvem9XFSUbyU4MgIDmiUrbFUMfFhbPouFYymAVJgHUbqSU21apk0sxysRrbuNI/s1600/log4net_example_1_8.JPG" />
<br /><br />
Here we are using NDC in the above newly added line of code. NDC, in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.
Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play.<br /><br />
<b>Example 2 of log4net</b> - <b>logging "success" message to console as well as log file</b><br /><br />
Here, instead of using BasicConfigurator, we are using XmlConfigurator. XmlConfigurator initializes the log4net environment using an Xml tree. In this example we are opening button.html file and asserting if the button with ID as 2 is enabled. If the button is enabled, we are logging a success message else we are throwing an exception.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7ArshOUiXxRT8GeWO46HZq2JM2BR-5574enpVPHC_oVJXMKX39MOkRW6c95xNCdhEeZZSe0iOumQo4wz5u6irv_RavEuYRlss8LSX5-ivgN77wcsmJWrA9buuqwyDf3xf4u_vXSVLT7Y/s1600/log4net_example_2_image_1.JPG" />
<br /><br />
XML file used with the above code is:<br />
(<i>In Solution Explorer, right-click on the project name, click Add and then choose New Item to add this XML file to the project.</i>)<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwEJXtgpaGOaUvzPqKX8q1mxXSXCyf0F76ET-sTo7wrqNQdosdrp8yAmBfHSYBGkGdrxzmigBMgQDpoSo3SDC4fEJldLiBGbqA5gnR9B7cfrrDE6WBaFOiTyDBVURf6DkQ68hkjLULkTM/s1600/log4net_example_2_image_4a.JPG" />
<br /><br />
Button.html when opened in a browser looks as shown below. I right-clicked on button and selected Inspect Element:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwcUk6ujw5iCOXqble6gq9Jz32eNwi-8qF7bcTfY4iaO3lo3661T7AoZfVg4Y3pbDLr5IlfRXAmjDpSA4nSiDbLE9GzpV3KSrALwYH__4IUHjdZmUxcaWTYgRvsnN2C36lupzdLs0Clsc/s1600/log4net_example_2_image_1a.JPG" /><br /><br />
Output (console) after running the above code is:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKugdfTC1wmgGf_wdvxulJB_RuDjZywx955K1F1fG-4DrmPkiZy-69uMTqgJoKpoQDspWh6BWNlwP8LgsSTV8YKIcX5Ol1Vll6hPvWyKDWPgEfehCocRtlGqQP-suyjaPWt3IRcvNiGBI/s1600/log4net_example_2_image_2.JPG" /><br /><br />
Output (log file) after running the above code is:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio2dGnhH1ag42CrWncSHLd715C-yRGe-_CXNoirEIAYzR3jM3GSDQxGXqeMxQbAXBwF4LD6awEHNUrJrmxiI0N1ULKFATV4GAwKQcahoz2xXyR5kTWyDXbEjjn4UgwxdvW6LBZ0BcAvVQ/s1600/log4net_example_2_image_3.JPG" />
<br /><br />
For accessing the log file (generated on running the code) right click on the project and select 'Open Folder in File Explorer'. Then go to folder 'bin' and then folder 'Debug'. Here your log file will be created.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk62g6lzEkf2-yw0gSEufBBDTcDgyXDWnMX3kojV-EqKhqe5puxB9CbdK0q9_aJsxvQ0Rh6xJ4pvULy-8lOQ3cdJUxNCnuveEGCOtoyyFjVFi9UktUWTNSp1vbWf8kXRpmCU6UvpSHGoo/s1600/log4net_example_2_image_8.JPG" /><br /><br />
Let's talk a little bit about XML file used above:<br /><br />
Firstly we are registering the log4net section handler.
The log4net section of the configuration file needs to have a section handler registered. This is the section handler used. It simply returns the XML element that is the root of the section.<br /><br />
Here you have the ability to selectively enable or disable logging requests based on their logger. Log4net allows logging requests to print to multiple destinations. In log4net's language, an output destination is called an appender. Appenders must implement the log4net.Appenders.IAppender interface.<br /><br />
More than one appender can be attached to a logger.
Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.
The first appender we have used is the RollingFileAppender.
RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property.
When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.
<i>MaxSizeRollBackups</i> gets or sets the maximum number of backup files that are kept before the oldest is erased. If set to zero, then there will be no backup files and the log file will be truncated when it reaches MaxFileSize.
<i>StaticLogFileName</i> gets or sets a value indicating whether to always log to the same file.
true if always should be logged to the same file, otherwise false.<br /><br />
ConsoleAppender Appends logging events to the console.<br /><br />
The root logger resides at the top of the logger hierarchy.
<br /><br />
You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root). The only other thing that the root section houses is the minimum level to log. Since everything inherits from the root, no appenders will log information below that specified here. This is an easy way to quickly control the logging level in your application.
Only one root logger element may only be defined and it must be a child of "log4net" element. The root logger is the root of the logger hierarchy. All loggers ultimately inherit from this logger.
<br /><br />
As you can see that our code ran successfully and logged the success message both on the console and in the log file.<br /><br />
<b>Example 2-A of log4net</b> - <b>logging an exception to console as well as log file</b><br /><br />
Below we have used the same code as above. The only difference is in the Id in the FindElement statement. This ID of 3 is some dummy ID that we have NOT actually used in our button.html file. We have used ID as 3 here so as to throw an exception intentionally. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoewF-x9l5BBIKfUbfneaSffBoJjwXxz0NUAOsm_SBS7V25A-uRHuepNuBw5ZrZyA8qb7wFiWpJpxWPldE08JCcXZDM7wq_b-NMcmIOLRvdGFzQ7sN8tdGknOTEhTmoTSbKEhyphenhyphenPI7IZlk/s1600/log4net_example_2_image_5.JPG" />
<br /><br />
Output (console) after running the above code is:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglOEURNfPzwCpYIg6a46xcn6HeAzw0zkHi8qEKudA4smsI51pKmEdGVfdVrgxi68dvwfZ0MCR9bNRKOyeF5JPcUxxYfEB9lqXAT8mD1DHfKPnEAMYGzoZdqzpBfhMRhQNpqJHr_6jwAQ4/s1600/log4net_example_2_image_6.JPG" />
<br /><br />
Output (log file) after running the above code is:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4yqXEWVRl13sNkWmj6eZtJrSGqlkXA2doFS53LgmFcSw50O4fgcHV-O_lRXIYR9HqblM1DYLtsBO88tIkemC8CRpkFupx0023UYoeTDr89ovVFG-rLHpS9W3HW_fyEdhrWcv0Qyp5-wc/s1600/log4net_example_2_image_7.JPG" />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-78916544892672049352016-10-14T05:36:00.000-07:002016-10-14T05:37:01.061-07:00Assert.IsTrue Examples<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Assert.IsTrue Examples</h3><br />
<b>Assert.IsTrue</b> method verifies that the specified condition is true. The assertion fails if the condition is false. Assert.IsTrue is common assertion used while testing web pages.<br />
Lets take a look at some of the <b>examples of Assert.IsTrue</b>:
<br /><br />
<b>Example 1 of Assert.IsTrue</b> - <b>Assert button enabled</b><br /><br />
Below are two buttons “Click Me 1!” and "Click Me!". I right-clicked on "Click Me 1!" and selected "Inspect Element" as can be seen in the below image:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiKeAUwsw0-Xjf4ScG3ANOy2CamZnnQZ7NCpYb0XrRjucj0DBAMi28KpwXzf99EACPFvBPvsz6YUPFjwbcCiMCBl3Bbp76cHdA3gALOrD9h-ChufKhdGODIRl6zh-lwORaN76cvAjxoGs/s1600/Assert_IS_TRUE_2.JPG" />
<br /><br />
We are asserting if the button "Click Me!" (with id as 2) is enabled.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6o9MEgTJHNRpp_nDks52xlXGRuJnoWrK3YirQEAAhWNZmnsSbT0V2nOeVQw0iKZNvIyqdY0D5z8UQbHdHCIo45LfUcAt31iFmAewoEVVesavslbCvl5L-sSdXgZB8qLvrn2i4fHj11bE/s1600/Assert_IS_TRUE_button_enable.JPG" />
<br /><br />
<b>Example 2 of Assert.IsTrue</b> - <b>Assert page text</b><br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_g-0WACcoCuVSIcp9IMGjBzJZGImgaTmlv2aHNd9oWd7NlPuufUJC4Q5wFxy8crPvGVZpJ-aZH2v9EIPAO5HJVUKL5Yu5oHcVf1g3jfM9o5RlssGZ3R-2ahMrH6cespK27MrGLrl0xu0/s1600/Assert_IS_TRUE_second_example_1.JPG" /><br /><br />
We are asserting if text on the page contains a particular string. Like if the page <i>https://gmail.com</i> contains text "One account. All of Google."<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgocvDMMgOeyN1ypkFQFWlhWzAEHRKKnbwj8h0p8JGEF-WYwkuAabeDKVP_Fogl26N_rHp59t30XXf1oP17fV1sZulTSGD8cCyZ10Aoxh3bSdt5UxQRaOyeL7IJ7gxHuSB_EGsWPtZPpsQ/s1600/Assert_IS_TRUE_page_text.JPG" /><br /><br />
<b>Note:</b> The driver.FindElement(By.TagName("body")).Text
returns the text view of a web page after stripping off the HTML tags.
<br /><br />
<b>Example 3 of Assert.IsTrue</b> - <b>Assert page source</b><br /><br />
We are asserting if page source contains a particular string.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLmvk6IWLSLx9v-aBCbykZyhE3NqgVEz3Mds8Unv0Oga6xj3UyyL0jMVu8mR0MI_88n-Bu0RIia9FmloX8fs0cp2T3wn1albAvv6WLcgoAJu_a76qV6bSvwV4XqkUE1X0oaAF_GXQDidM/s1600/Assert_IS_TRUE_page_source.JPG" />
<br /><br />
<b>The page source is raw HTML returned from the server.</b> If I uncomment the line starting with "String Y .." in the above code and debug the code then below is the value of Y as we see it using Text Visualizer.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9iDVpBNLe2kWSVJ1aAARWwSc2edaTwJmgg9Gr4hps_fh3oLGms99XHI7TpsjNk7T5EC3jtFlVWzr1yZoH20sz2E2RUtWjAFLvKcYhrckfAvh_euZsYK_1Cu5xtGc0D1p4XBnpkjmtUWg/s1600/Assert_IS_TRUE.JPG" />
<br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2EvtWMoK09m9ww2SqOfDJAAeqPTQkiXbb624kdFqEpeCBPLgYI-jCeghrgxoGJNkBpFKMfboXAiOXYwqxhjopCGzbLNL_yCQ99TvSXdJ-IeNNGdMs23Bx9I2otme2V4KjKbEpnde2Mn0/s1600/Assert_IS_TRUE_1.JPG" />
<br /><br />
<b>Example 4 of Assert.IsTrue</b> - <b>Assert checkbox selected</b><br /><br />
Below are the two checkboxes and their HTML code.
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNWiM_39OvCfUCtcc7oeq1hFAwlJAAkTuR-ABsfTFB2VQ3T8P3PkB4vplN-9z0pZ53lVX_XPWaqin4z_WIx_tfV3zo7TWuO4Ry0p6tg5rPMV-DLu-9GshUU8uviF-osaM2WfvPalryUsk/s1600/Assert_IS_TRUE_fourth_example_1.JPG" />
<br /><br />
Here we are asserting if the Checkbox2 is checked.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizri7Kfs05oiUJfbHbKZpXBg61EE3IxdvIpVJ5uYUu8zkhX6TPXUgf3HuTsDbHqvZsp6ddlCsjRWats3Tk_gXMg8VyUsFj1sHJn0gjh8jyWfvQOrlqwS_23w_r9eqBsjO8N49zkPumPMU/s1600/Assert_IS_TRUE_checkbox_selected.JPG" />
<br /><br />
<b>Example 5 of Assert.IsTrue</b> - <b>Assert image displayed</b><br /><br />
Below is the Google image and the code you get by right-clicking on the image and selecting "Inspect Element".<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB1cmXi6Iazhdcemx_FEuB9bIlaDglN10sc-Q1bCuKq5QytVJ8Ys52oGuhvCwnvlH_uXz0eR8cPIPgvzm686sFoLGnNmQPE9Ora38L2tGBNRhVLo0n1yhU3Q5cjpSL5t2wBrOd6NQot4Q/s1600/Assert_IS_TRUE_image_displayed_1.JPG" /><br /><br />
We are asserting if the image is being displayed.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhf47EMPFOnoI_KTQj4yIvKt8BjAJkbo74pUhnmMk3A07KecT9nV9k0hmNPM1BgKa65NGMPilwzpaYei2QVEBFtiAtlMaTcFat4JO8600cmqIi4JPqjtAlqalwZiC4-NRNdHGv3RcF1bqY/s1600/Assert_IS_TRUE_image_displayed.JPG" />
<br /><br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-67091799273783287272016-09-27T21:04:00.000-07:002016-09-27T21:06:03.553-07:00WebDriver - Read from Excel<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Read from Excel</h3></br>
Suppose we have an Excel like below and we want to <b>read the values of this Excel using Selenium WebDriver</b>:</br>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF9nb0IYE4Aqa6Xl8nDS7HjgCtwMenPQkKH7ohoe1rq5hO1lSpScYCuuYa7ojvqrMq88KpvlGrGieq_eF10fxMFhQvH-pveExO4LdDhz-kJ372BW3YCSc7iF1c5bSUYVRGZHVfhcVcdD8/s1600/read-all-the-rows-of-the-excel-sheet-webdriver-3.JPG" /></br></br>
Below is the code we used for reading the values from Excel:</br>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3kNo1fE3qu2F5_NwFLordwuk8gSKmtLwoDE5oZw-myozJpQQr0qOrzyF9daKrLjNJij3FcpsT7X1TZg9FVVqAzaV60PJnlnwEiUYIRZm85lseglX8L7tDx0xg0Ln4WjFHWhnw3HpBmcM/s1600/read-all-the-rows-of-the-excel-sheet-webdriver-1.JPG" />
</br>
ExcelFilePath is the path of Excel which we want to read. <i>Excel_App</i> represents the entire Microsoft Excel application. <i>Excel_WB</i> represents a workbook. <i>Excel_WS</i> represents our "Sheet1" in the workbook.
In <i>Excel_Range</i> we are getting the cells that have contained a value at any time. Next we are getting the rowCount & colCount. Using these rowCount & colCount in the For loop we are printing the values of the cells.
For more on Excel Object Model Overview you can see <a href="https://msdn.microsoft.com/en-us/library/wss56bz7.aspx">here</a>.</br></br>
After running the above code the values will be printed as shown below:</br>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLZHyIlkplQGmIY8ZdtOKrYa1fqo3ejYlyPJafy6K-eB7URqg2j9oV1E_Lq3JtTL9jFALQmO9EN0H1A50zJDC72Uqgkjva0lN4ZSAIRiGF_UJvptJVInInnBZ45-kmd4pEtNeDjW_SxV0/s1600/read-all-the-rows-of-the-excel-sheet-webdriver-4.JPG" /></br></br>
Make sure to add "Microsoft.CSharp" and "Microsoft.Office.Interop.Excel" to references along with other references for the above code to work.</br>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXzpJMPNKRxSB0a3_X8H4cjoObXYq6kxTSmMIb8vSE00nOhWRzXR42DKumlmpqS2OVISDO4JE31GeZmfo0h84KAJW0lcZlo0FXM9xDYt1l5bXIQxHJtG_E6UA3Bs4KDeAuK3XZfunNbrk/s1600/read-all-the-rows-of-the-excel-sheet-webdriver-2.JPG" />
</br>
</br>
</div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-60512569326385203312016-09-13T05:48:00.001-07:002017-02-02T09:32:06.258-08:00WebDriver - CSS Selector Examples<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - CSS Selector Examples</h3><br />
In this post you will see <b>examples of Selenium WebDriver CSS Selectors</b>. Selenium WebDriver’s <i>By</i> class gives us the cssSelector() method in order to find elements using CSS Selector.<br /><br />
You can see the simple html code file (named button.html) which creates two buttons, one disabled and another enabled.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJZ_-RcmvkFJG-yZCKE5OrsXkjlo5YLhf2-0_GNoHbgPvH-cI6rdcKUbT9Tb_Ze2FJ2ghNpWVkGa4o47KAT1tO56kL3qqWvpNk7cJ8p3J3qTu_KA5cS_jH9S4uCHnM3jPCTcv-w94vq-s/s1600/webdriver_css_selector_example_1.JPG" /><br /><br />
When you open the above html file in a browser it looks as below:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqAB8IvbMSiQsM86AujDToT4NZJGwXNNi2DUX_-O__8dsvB7pQ3HDVYvUcLxBmswX9qgsYdywMgeeA8P0wih7dd-Ejw4A9n9UVFNYP4qZUr8tzFoE4TcLcC5l0nJtXcTOdsu7T2uNdJE0/s1600/webdriver_css_selector_example_2.JPG" /><br /><br />
<b>Example 1 - CSS Selector</b> - <b>using absolute path with spaces</b><br /><br />
In this example, we are locating first button using absolute path and space is given between the elements. After locating the button, its text is printed.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX3-jofhpv3EmH4PTblBtwEVBWpBlpj_GuqsyYdb9_sdZXY_SHy2-kL8j3vTpLdqxGOhEAeYawvYVoKXfRpRcfyRFs3k1QcBnuweC3XmNmgv1bfJruBlpzidUNDlhdu3MmOLKwdI-xWDI/s1600/webdriver_css_selector_example_3.JPG" /><br /><br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6Mb4rhZz-GzP7XIvFr7DPT4m_YfrVHSxIv3fDrU0io3chi-KY6-LF8KWYaAZqV91xhTSblzyNOK8LfZjph0JUfz1MU1kDEKNRfo7YSMOOLJzm_HuX2VVhtOOp6C_oyBnlpX8iNQ4qWaA/s1600/webdriver_css_selector_example_3a1.JPG" /><br /><br />
<b>Example 2 - CSS Selector</b> - <b>using absolute path with '>' separator</b><br /><br />
Its the same CSS Selector as used in the above example. The only difference is that we have used '>' separator instead of spaces. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoXvrCB-KQQd08uXnhefmL7D8kB4kE8-FW0NmftHl6B53QvgjvSCuMkDNZ4U_iZ3-IZ5p28ND84mhQdpZdgueoRMVhtiat0lWAhghdDRIcChdN8t_s3I0Xq2FxA6YMKSWLezvUbzsjyW4/s1600/webdriver_css_selector_example_3a.JPG" />
<br /><br />
<b>Example 3 - CSS Selector</b> - <b>using absolute / relative path with spaces & with '>' separator</b><br /><br />
Below is the wordpress page (https://wordpress.com/wp-login.php).<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtCyrQVbllDSZERIlbdmAa_MjepEwY-_Rl-n3GDcfP-XR3rO6rbusa2x2bNn9cuRsgdC55atE576zfxbSmxv6UqWX7rKCerTj4avpz4MxSr499MEQXzJ4_6qjObxFQh4hQGpYCU8hE4uw/s1600/webdriver_css_selector_example_3b.JPG" /><br /><br />
When you righclick on the Username field and choose Inspect Element, following html is shown.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEgTwl6jvSezqAQkzmBQwrDkzW301OgrVejHC9l0l9aDuYEk__hcHwRIjy250PJQ0w3Q_Io0FnIAMO8xBRMG5SjHPyFkT9J0pBCq6yBejm7J72w4ADiCciEwp7YMAtFbTyZ64AK7oW9JM/s1600/webdriver_css_selector_example_3b1.JPG" /><br /><br />
In this example, first check the CSS Selectors of the commented lines. The first commented line uses absolute path with '>' separator. Second commented line uses space as a separator. Moreover in the first FindElement statement,
the CSS Selector "label > input" is finding the element using relative path. All the three FindElement statements will show the same result as shown below.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqWqAAYz6i_zYag42NaQwNw7i_tB6ebSorx6CrB_0QPNOO-73QkFK-zsYI2UjaQ_OToDB7L48qcGlszWvi5oSBBaHvdMTvngB0ua7ecHEOCaAizuNTCAIc6U60G1SXF2Qh-DPQnHH5qMw/s1600/webdriver_css_selector_example_3b2.JPG" />
<br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXtKUdoXgnkBWejOPmswBY7miUEu5zceeAnmxmrkjxYJz1l173fiSwltF-5_suNntNLqlR43MLw0CelKvjRbwyZpzCskRHJGCPsJ3nlyknmqfPjN3an1FE1AO3uwH568xa6JgWXbdFdUw/s1600/webdriver_css_selector_example_4.JPG" />
<br /><br />
<b>Example 4 - CSS Selector</b> - <b>next sibling</b><br /><br />
The '+' option here is finding the next adjacent element. Firstly, we are locating the first button and then using '+' to get to next adjacent sibling.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0ul1vY3NwFufbJ56FMcsevf7ZIW4N4z-F6Z7OdijVfGoFAExHlg443rxpg48xPOiHGOKpYcWYmMF-4Z4NLfb0tZ52pdUTIULQL2yLwX7RtXVPcAydni4hcbQmeWFHI5GMZi-FpiwCKck/s1600/webdriver_css_selector_example_5.JPG" />
<br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSYI8rhrFexzmGRyibNJA4z0ek1-TinjcqEV5LRHK0nhxd2Jva4QrvqkXhIEZnRRf7Hbpx57PUjwrzFAT1KeHs1CVNQfd8WfdzNczjWKBmWBqQWDuOVx-VWwR699Hfq9txpWZChJnqt5c/s1600/webdriver_css_selector_example_6.JPG" />
<br /><br />
<b>Example 5 - CSS Selector</b> - <b>relative path</b><br /><br />
It finds the first <i>button</i> element. We are finding the first button element (see button.html on top of this page) and printing its text.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMcMvwoi172oUFJyImrUawV0I8W5x6OUl5hqdS-VU_h2vchSR8h_L9fkzqsXuMURFNTkyOed_kswdx7oUuZ0DwtXJQ7FAGQIiasAxo24Cj-xpfGcKDdBdSJVCyY73GclGsGAJJiLfqpPA/s1600/webdriver_css_selector_example_7.JPG" /><br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiApQdtt0HiyLY8o7iTmO2Ri5k2xGJ28laZvXp7gTMStCcLi3falkGSd5POV_HJGudAP1cQN3cc8fmrn1vUbVZxlcQvYCespjnRa_jYS3vs6juSd4mZwwIRnyjPuGu2nzmhFTD_QjV6g6M/s1600/webdriver_css_selector_example_8.JPG" />
<br />
<br />
<b>Example 6 - CSS Selector</b> - <b>id selector</b><br /><br />
In this example we are using the html tag (<i>input</i>) followed by hash "#" and then "id" attribute of the class (see the html code of example 3). Finally we are printing the value of the <i>id</i> attribute of the input tag.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2aR1t1JxwYr_kCAadrUXY2HfbjShUNgI__TLxprMcLR2WdgGVR6YavCtXEnRQUDQfNvz9varlt82xVf563pu1OiZs9RN4wZrZ-7K0QNoWuJShb3ygWrgWALjYI9BWGQHEALSlW1ltd5w/s1600/webdriver_css_selector_example_9.JPG" />
<br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3dEH_o-BP08AKVupRcT66fj2mJTvuN2p1jntZJpkAvI-i5k6So4TvgAW95YwJGufV07O8N2X8WIy6mlkkveRlr8r5muorUxtolbSkdwYiEstiaJeDr-WaSsQUtTaOy3P2yztskeh24l8/s1600/webdriver_css_selector_example_10.JPG" /><br /><br />
<b>Example 7 - CSS Selector</b> - <b>attributes selectors</b><br /><br />
Its same html code as we saw in example 3. We are right-clicking on the Username field and choosing Inspect Element.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6HpN3mnDCCUtRyB-D7i1_OlEKnH8xciZPcpMDOuBtSnFxtePE6tf_nSwHNAe8xmi8sD1WtYS6v4oJbADNZw78qcicXvuaY4zZrtUtXbeFUgrr0MjJJ6A0DyoMCQvli-5h5aD0oWhv5Vo/s1600/webdriver_css_selector_example_11.JPG" /><br />
<br />
We are finding all the input tags where class attribute is "input" and printing their name attributes. So it prints the names of the Username and Password fields as <i>log</i> and <i>pwd</i> respectively. Refer example 3 for username and password fields.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlDNxYTlvMmoZcV6qzPxjjEQJEWDMFar3U3N1MeGNcKXDjJ38KlXoyUL2geMOeD2XSSTWxxP4P8P4ORrrerrD0clx3vi3BsmcvT1OF16DzQ-64MWaLJQOJfeg-K3SPDz9wSoUf1cSXjMI/s1600/webdriver_css_selector_example_12.JPG" />
<br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdax6lKrPhQO0RWbfUkoDlUr71PTqA9VYDUuFep32eqCGkQ5nL-7-wNguDbZQQRRQGEVK-bXvHF_726YPMUNkxAL2canX5Mv_AQXnYRGS8zRue3MrLcN8wk7cbxqtbmfRiP4mEj5k2oUM/s1600/webdriver_css_selector_example_13.JPG" /><br />
<br />
<b>Example 8 - CSS Selector</b> - <b>class selectors</b><br /><br />
Put a . and a Class attribute value and it will return all elements with Class as input. Here we are finding only the first element with Class as input and printing the value of its "id" attribute.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp_w5GtGhVXRwiW_mkXk4dcX8nO-0ON84znUQb1a_E1dDznyTbtexbSfg6dRyRyTspiVPGv3ZHqtFDAR6FTy9F6_f8UV_9RbSibbs66q-oF0VRrlxw02rfFE0-KUd-i5pHXILLMDPPHwo/s1600/webdriver_css_selector_example_14.JPG" /><br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj13P_OPju39N2GzlQ4hG4ksPCo1prhY0Ley2KxHx7MNq7TPU4uE6c0tmlTpsS91fKO6h_Fu412_sRbLhIdufY2PCrvPxLKxAx-LwX6XTJ4pavYQaZMoOn0AHkGXBHGM2MQX9qVvmR4YFM/s1600/webdriver_css_selector_example_15.JPG" />
<br /><br />
<b>Example 9 - CSS Selector</b> - <b>partial match on attribute values</b><br /><br />
We are finding all the input tags where id attribute starts with "user" and printing the value of their "name" attribute. Refer Wordpress page of example 3 here.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjkyMwyvrJgA8nwE7u4hhwioh1Id8hBPmDrq_KsjmGl1-ArgFhZKjKijlX57_G9w6yYn3wsa-CkZJaZRnOpfp37R5f3dCJMuRbFbOmC1Dd7NmWFR_rPe-LACKg2NV1uaqPp_N8MKoQ4K0/s1600/webdriver_css_selector_example_16.JPG" />
<br />
Result of running the above code:<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnVxJLJle-HzSCjk80MY5sc-tH8zyyDE0wJ5JEvY_MgM4znbM8jV4hw2tAqSIC1-RF0X21STm9B2dZM6Rp46R-sgpntpb9Y-95ptcxUxavU4qsDtWyoq9wtjF98wgSuRoLrTAAUpXVi6A/s1600/webdriver_css_selector_example_17.JPG" />
<br /><br />
Other examples of CSS Selectors can be: <br /><br />
<b>input[id$='user']</b><br />
It means that finding all input tags with attribute id ending with 'user'.<br />
<br />
<b>input[id*='user']</b><br />
It means finding all input tags with attribute id containing 'user'.<br />
<br />
<b>input[id='user'][name='my']</b><br />
It means finding all input tags with id=user and name=my.<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-15776240387469596442016-09-01T20:43:00.000-07:002016-09-01T20:43:30.371-07:00Selenium WebDriver Table Examples<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Selenium WebDriver Table Examples</h3><br />
We will see examples on how to count rows / columns of a table using Selenium WebDriver, how to read and print all rows of a table using Selenium WebDriver and more.<br /><br />
We will use below table for our <b>WebDriver Table Examples</b>:<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjd32YDiRJTTAKZmFMCZp8bS73pCSfEzLpn_mGqR2v8itZQLVWp5gFJgOuj-fMzWXs1DU_E7jlskPTr4Yunoq77Xr_Muoo4p9nNMnRXtXTfw0gCb-44UGKE_yeTBJY0qMiXaqzRgAaoj8/s1600/table_html.JPG" />
<br /><br />
When you open the above html code in a browser, it looks like as below:<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMYtMz4Aq7ZXv3oVuu2SqJJUxhWQQLlFtA_suD6a0oTfZSTgC3lNwurI_gpY5v6KOYoyje4ynZ98DEq_HssbtBCYdZNPHgbdGuXfbH_O4E88H6QUIg3Vxd6KsFh4RwXGZnoVkHit3tZoQ/s1600/webdriver_table_example_0.JPG" />
<br /><br />
<b>Example 1 -</b> <b>WebDriver Table Examples</b> - <b>Counting rows / columns of a table</b><br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe21FPKwjlQnSK1aQWAHCn6xpxiAgXEHb4Lx7GBc5hdsO87jtkPlXBMn4znXAkeF4_dTZUukeFSB7P25yWHqw5HqenTpKzm-RDFuAboPurLTg6lhSU3e6Cn0hXe-36I08ibY6t5fLbfMs/s1600/webdriver_table_example.JPG" />
<br /><br />
We are first finding a table using <i>Id</i>. After finding a table we get all rows and columns in separate lists. Using the count method we get the count of rows and columns in a table.<br />
On running the above code below result is shown:<br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioBlCSCKnxIwEXiO-vR1U9L7mn9aXmFu4mjXEUI2gAiG1Ztoz4XulP05OWbmKVkCUT6ZvG3NUdiYmICpMNG9AjdRSs4dgwsjdpi0DGan-sDH-5RT5CL3bImdCokIIvCJDY89SKnAK5j4s/s1600/webdriver_table_example_1.JPG" />
<br /><br />
<b>Example 2 -</b> <b>WebDriver Table Examples</b> - <b>Printing all rows of a table</b><br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyMovCDbniCVj5SSx9WjuifzA2eQitxe8eyYJumXdzSLeCuXhBSW9dPHcj81RLfYFIJLLbl_kwUFSNbLMUdOnsUeMQWJ-_qU2lFCBWXwbdXe2gxgnyc2Ka-k8FO4hfg0rH986nfJ0w2E8/s1600/webdriver_table_example_2.JPG" />
<br /><br />
In the foreach loop we are iterating the list and printing each item.<br />
Result of the above code run:<br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh59hjKzfbe84qeipV7rN6-Rs-oa-_yQRp6jZmKebUwXghMI7OpoxNGZMqU9bd0kGh9lZhlWOgy8d933RwkhUuffh-PYI3zJq8G8zf1d9gB6aGhVhzTpxE13RPvf2maiP0mBYVgdvnO4Kw/s1600/webdriver_table_example_3.JPG" />
<br /><br />
<b>Example 3 -</b> <b>WebDriver Table Examples</b> - <b>Printing a particular cell value</b><br /><br />
Here we are printing value in the second row and second column of a table. Remember that header column (FirstName, LastName and Age) is considered as first row here. <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWT0CJ81Jn85PmPG26zC_sbpqFcWuzSiwS1A3-VZ4hWbzxZ0-dk9vPn6t3ZyYyso1qy3-6C6bNxFDPGUm9iJgI4pg0Cz2QoN818NKXpfnOs8FU8n5Bsg1xW58J4rXjcs4kdKle8-vIcNA/s1600/webdriver_table_example_4.JPG" />
<br /><br />
We are using XPath to get a particular value of a table.<br />
Result: It prints out the value "Carry".<br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2jPWTYF0bNXclCMirlzNsZo_0QczUF7Pdlg-MTFMhvfuA3NvpGF_sENbKNyJLKh4Y2kMMOLhRnTiETx9xYG5TJMk1MkLjjbyqyi4-oPm4hQO_XVwtZLlmduImZwbwP65qfun8_JQrIQ8/s1600/webdriver_table_example_5.JPG" />
<br /><br />
<b>Example 4 -</b> <b>WebDriver Table Examples</b> - <b>Printing all values using XPath</b><br />
<pre>
XPath for Jim
/html/body/table/tbody/tr[2]/td[1]
XPath for Carry
/html/body/table/tbody/tr[2]/td[2]
XPath for Melina
/html/body/table/tbody/tr[3]/td[1]
</pre>
I tried getting XPath for few values as can be seen above. You can observe that XPath in each case remains the same and only the indexes in the <i>tr</i> and <i>td</i> are changing with each XPath. As a result we can use FOR loop and print all the values as below.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ2Vs5daA4NPxemUmTTmBENVg4vUqiD2swmnAGJu1Rj9QRXnuqvMRob1LGOEClRBDGkl6gZ3ph4AQDolM4zrLxr0UJgIEHhqufGvRQ8vzTEhYeigsaGv9CqTMnOflqrAf80jhyphenhyphenUURz5Hg/s1600/webdriver_table_example_6.JPG" /><br />
Result of running the above code.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkMC0_rqxtpy7ct9lhlpw3uHShvtj-Z1mtVPvCTw9jdnhBsky8uaJ38ew1UkPVEJNT1HwT7MJWG4oxHeYj4gK1T6VGSAaabcz_wym9T3dSttPEF7qMr0AW0VUe4h2qLKHqGYbI8RjXDDE/s1600/webdriver_table_example_7.JPG" />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-69194965846437578112016-08-21T02:37:00.002-07:002016-08-21T02:38:35.434-07:00Assert.AreEqual - with Examples<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Assert.AreEqual - with Examples</h3><br />
<b>Assert.AreEqual</b> method verifies that specified values are equal. In simple words, Assert.AreEqual checks for the equality of the supplied arguments. Below are few examples in order to get a whiff of Assert.AreEqual.<br /><br />
<b>Example 1 of Assert.AreEqual</b><br /><br />
In this first example we are simply verifying whether the two values (value in variable x and "Google Search") are equal or not.<br /><br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnEUbwetQMd-5725-K0D4ctWrPW2gPTdug3op9Kx5_cf1MvUquh2-_LGyWBRX1Wowmpr13SCpQp4pmUDzOvnaXiDxQXWb9Xk1RwmL63MsyXXiZ8cLHjIiBwYzuNcBv699QLyVDMq7BrR4/s1600/assert_areEqual_1.JPG" />
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6B_5y9ZxWkgAZGMCS-VoB59sjE3NkeXwPdSH60M_REY91WzAL9XvEsbG1WJ7YhzKTtcqb8VVKq9eUw7WIyljk6JRLDcBTQYdUC54ju-RvcAfCtrhpMmb9Fq2qQxFay847jE9_xLMB610/s1600/assert_areEqual_2.JPG" />
<br /><br />
The above test will pass. Lets see what happens if the above test fails. So in order to fail the above test we will make a very small change i.e. we will remove 'G' from 'Google Search'. So our new test looks like:<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_LToPuf3rlNOkv53yFOR5-Onfn-n52yGWtMM3LH6Z67y9kPY8nFWOLQiLt0mB1UXQcr5eeZ2s4Ta575YbiUiS54entdcnlfojxIXgWrchQvtryaJof5jdGTtpfD4dA9ziPtk4LxsyImI/s1600/assert_areEqual_3.JPG" /><br /><br />
Now when you run the above test following error is shown:<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwBTOxVasHPNhNkvCbBKNVMzqW-fTKIjXvTWfT-H5F4YOTGPv8939SMtpg9PedZYkWiNK5llTN5ak377XhISJrsdc5HDGQ1YqE6uIo9kH_-LhmNyMoe0woKUMOtmEisENoV0KK3USGIwc/s1600/assert_areEqual_2_example.JPG" /><br /><br />
<b>Example 2 of Assert.AreEqual</b><br /><br />
This example is similar to above except that a user defined message "Dummy_message" (in our case) is displayed if the assertion fails.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPxUcD0jsJ-SjTd6uZ2tFyENQ7_c0eRgoNHCagIGeqen3AIU_hHAchFSksAwxpu-fF8Qh_eLV7Z4V3aKH91Dvc30zvE8ps-lIH8jbBVk9SlmCm47A-Ya_WeShHx64JWismXDVMiII6P7w/s1600/assert_areEqual_4.JPG" />
<br /><br />
The error message shown after running the above code is:<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6gsAbwH1hIN-OkyVjQ4lzC1u1qNb2Mm_q3PJsR_3ooP0WiVtRuVIKNXdo6CZb2f0gG-hH70MF1xlKQs41xJtfDPXFR5J2D0_Ixujx51sXqLec2Dlm6O6Xyi4IDMfwmYrCjoc-FvyAfNw/s1600/assert_areEqual_4_example.JPG" /><br /><br />
<b>Example 3 of Assert.AreEqual</b><br /><br />
In this example the last parameter is boolean true. A boolean value indicates a case-sensitive or insensitive comparison. True indicates a case-insensitive comparison. <br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjaf4rs2rvNTXurjYpQpK1wS3hSztRJWhk0SqY4NJcaocXdWVwlyl6Ss9xzxoZBsHu6ljA3-6HQHci-LJ4LbBYzziUADsKGb0hGW_dausw3xw7oPg1sYTW0SD31Y4iPklDsUjGlgbdgyE/s1600/assert_areEqual_5.JPG" /><br /><br />The above test will pass.<br /><br />
<b>Example 4 of Assert.AreEqual</b><br /><br />
In this example we are verifying whether the two objects are equal.
The assertion fails if the objects are not equal. In below example we are comparing two same objects. Next example compares two different objects.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsS0W13sSrI5LEOlLCGYeYnP-7tXnx7WNBKafnMJHwNZOPobML_AuzeFd9_wLy3fHZea-3jbnpRBk22hHWaSyZJX6RKGykgpfgeJQADifkT8yHh7TEKzgnEJKmRHnrS0LMXY-jKuMon5A/s1600/assert_areEqual_6.JPG" /><br /><br />The above test will pass. We have not considered obj1 in the Assert statement above.<br /><br />
<b>Example 5 of Assert.AreEqual</b><br /><br />
Its similar to above but here a message is displayed if the assertion fails.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdlrzHLk9-PqiBe_iQGNC5YWN1gPFP_rUk8IWVU5baH-YdUIPIFfD-7OIGnkvIgqR7KrhRxaK0-2eaq0NgGrRyWJLyw1AyK8qPt1s-s3P26Co9aWnX9Z_TxIBxThCFvgUsyVs_Ghg3bKQ/s1600/assert_areEqual_7.JPG" /><br /><br />
Result on running the above test will be:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv6yxoi25M5TZvW4nRSJ2GJ7stz99JJ3k-9EPpZ5X1SnxyMGIRdEPuFI_jKL_Kc6Pj04MEP40-8dHsW_IzWCGb_HoK6T6bo5IwANIpwslBr2TMTmpHa2Y9pEPoBJK0YFOhQOpCShIZcMI/s1600/assert_areEqual_7_example.JPG" />
<br /><br />
<b>Example 6 of Assert.AreEqual</b><br /><br />
Its similar to above except that it applies the specified formatting to the message displayed.
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ49j__V1pKafaXyB-MzR7Ic37hVsd0MjKYTPREsp1PoSZFnubD4ZIZ7kBU5i699fj2HKu6TzrUnorfge_carj10cCBrGLqew9hhyphenhyphenmy0WhxrMHGjXQxjwF0aKc6Y-NZvRzKc5V9is5ZEY/s1600/assert_areEqual_8.JPG" /><br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPd00BfYLtTixui25donALUoEOWEg50-6qo8XBcVstTl_DyoTwTnCbg71L0-1cytSpA3kOWGBayQvXSA-72wOeI1ooAyRc-y6b2A3YAdTjdAOo6ayN8l5_JMUueW9Q9xfDKhAC4VbcmCA/s1600/assert_areEqual_8_example.JPG" />
<br /><br />
For more on Assert.AreEqual please have a look at <a href="https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.areequal.aspx">MSDN</a>.
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-47108665563852471402016-08-06T09:48:00.002-07:002016-08-06T09:49:26.016-07:00WebDriver - Select Multiple Items in a List
<h3>WebDriver - Select Multiple Items in a List</h3><br />
This is the MultiSelect Drop-Down ListBox that we are using for all the examples below:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9xcZSqFroCEb-vDzvyFBXrTP7ZuWvb6ZNKYk2WnJFtAKdtZy2p0XDpvWAgVgBz5QsZIZiVfcray-FZFajBhZe00VLeCujZE4bGIdtCx-ueC10Pp1__tAOv36ZDLgCkYc_pu3DiFjmR2g/s1600/webdriver-multiple-select-1.JPG" /><br /><br />
Save the above code in ".html" file and open it in a browser, it looks like below:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr2guIYll8Z4xgIK8IpvmVr1ev88dxZi0rznyCMzmrjWVTjbkDuAPBYEBDnv9uqioiA6H9OQK2-CXpS63yc3se29S-8tvV4WYxVEil-EVZ0VT9NvDQe51_1oVE8qBXgTcPT-4jC6KAlzs/s1600/Selenium_SelectElement_1.png" />
<br /><br />
Below are few examples of how to <b>select multiple options in the list<i></i></b>. In all our examples below, we are using <b>SelectElement class</b>, which is included in OpenQA.Selenium.Support.UI.<br /><br />
<b>Example 1 of Select Multiple Options - SelectByValue</b><br /><br />
Lets go through the first example line by line. First we are creating a new Firefox driver. Then we are navigating to our file in order to open that multiselect dropdown listbox. Next we are creating an object of SelectElement class and assigning the multiselect list to it by name. At last we are selecting the value in the list by using SelectByValue method of SelectElement class.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiowpLPHJYV8q1uBO9kVO8nLyx_FW8fq1cxQT19Hxvix91egeg5FobjUbYr1k4ZqoNiLq100x7hgZULmwSR4rYIo6xeabfX-4wU9dy100L8cvrlnMslfxlu1KtfhRv9q3UEcXypu0b6UkQ/s1600/webdriver-multiple-select-12.JPG" />
<br /><br />
Below is the result of example 1. "Australia" is selected here because the value "Third" is for "Australia" (as can be seen in the multiselect list at the top).<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWe44dyZqQgh_64lcyUmaJ_hAfmbr8vrMxpe511KKJ5hdQCVkwalFw3Xya82zH7A-7mYYDtRRWC-4f1wB5thzGEluPef_oggd7wmZpXsKuwdjfrtK6b98TYL-4hrdD85pVxNimhICamWg/s1600/webdriver-multiple-select-3.JPG" />
<br /><br />
<b>Example 2 of Select Multiple Options - SelectByText</b><br /><br />
This is similar to example 1 except that we are using <b>SelectByText</b> here and as a result Denmark is selected below in the result.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_I0kgBIYG3CoJnvSLwCtK3EXsKRjLBNGEMfwWRSgZAxI-atxuJ_G7sdv_JzC3RrIANl1vOsilsB5qPan1q0neNV2QGXz53w4bCRKRS0M0d77am33fZCK3XkSoPLu_Y72J7D9REzENb_Q/s1600/webdriver-multiple-select-4.JPG" />
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTNgnQVbQjmuMBzMnK5C3_5EZs4VEoh_d-CBOtB_KJnvjS3eRqWFH_UP4pOL2SNNXDll9Jn51nYxqglvVAIXOQ73YO1L4ErZ8S0deBnvuRV7X3u9wrS2vmiQa7cfHjJjHsz5dYfCpyMss/s1600/webdriver-multiple-select-5.JPG" />
<br /><br />
<b>Example 3 of Select Multiple Options - SelectByIndex</b><br /><br />
In this example we are using <b>SelectByIndex</b>. Index starts from 0. We want to select "India", hence we have given index as 1. 0 is for "Please Select".<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC23i9T6mGmwD1m8AoSwwdZxkYyATXA07oZ7CzO0TTlku7sOfHwzPJb5jF24fJxuf8Fz48iplNypCuGjzpgsyjk95A1PbwF2ZatYWRet404GAVbWLD6nS6jJzh24zMnoh-J_RjGA__62w/s1600/webdriver-multiple-select-6.JPG" />
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBg-4eZbWm7Q7ejINx4ApywiQ41GbkQZzb7PydvbtfHXcj_DRj4-tQi0N5do1oyfPQCtaq3UYhsIP1xboYvO6tdkZ6ttzWrF0s_jpyHDqjz4PPtCth-89r7d2-ceG-dUiVD5ljC9u7gn8/s1600/webdriver-multiple-select-7.JPG" />
<br /><br />
<b>Example 4 of Select Multiple Options - AllSelectedOptions</b><br /><br />
In this example first we have selected two options with <b>SelectByIndex</b> and then we are using <i>AllSelectedOptions</i> property to get count of all the selected options. In our case the result will be 2.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivMdYv3Ns-p_WiEqBCzIX49zkQuhHaXuI3Rni7JxgExpBVT6_mxoqkICIdoJnrLYM9aUQClvX-7ZCt5xiC8TMptXPDnKgE97SSEvGVxZ91DauWsyeuXo1c13cr-VeHP3PVwQPl4-g7elA/s1600/webdriver-multiple-select-8.JPG" />
<br /><br />
<b>Example 5 of Select Multiple Options - SelectedOption</b><br /><br />
We are selecting the third item in the list using SelectByIndex(2). Third, because index starts from 0. Then we are using GetAttribute method of SelectedOption property to get the <i>Value</i> of the selected item, America, in our case. So result will be "Second".<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeWYLmlPwP3KfNFuiCybpV0cZbkfC5JgrAe1UdJQB0dSUCgxiCvtZaMWdYWjq_OhuIqpIncBO3DXnUQATN1Erf92GqignKbbaO2IIT83_TyOIzBeDaxTWwLY1wupk91UCrdXlup3jk9Zw/s1600/webdriver-multiple-select-9.JPG" />
<br /><br />
<b>Example 6 of Select Multiple Options - SelectedOption</b><br /><br />
This example prints out the "Text" of the selected option. America in our case.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3ve56JACmcKVM4yMpRgHzFA_OHP2kwTA-htVqiVvS0-Y2OddriBEQnpdzl3ajxx1tMvLTbt3NhFw9zL9kIF4MtAqiwa_z2AlZclnh6XAaKfl2U7uxEj1gMvERU3X_tNUxEq81EIU__og/s1600/webdriver-multiple-select-10.JPG" />
<br /><br />
<b>Example 7 of Select Multiple Options - AllSelectedOptions</b><br /><br />
Firstly we are selecting two options (America and Finland in our case) using SelectByIndex. We get all selected options in elementCount list. Then we iterate and print the text of all the selected options using elementCount[i].Text.<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjooDu4Vq3nUI58ewCQWJ07-PQGlPAYV8eb-hRsP0VMuMjPSkID9G3IgyibCK5rDkujbRx5-ZMcC1xtp8yo2aQ7nAhVKWFPPmuLPAqBdc2ZFV8M0LVKpbtEJPrl2wf34jcsg0HuDrFZTgM/s1600/webdriver-multiple-select-11.JPG" />
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-20322756096149151942016-07-29T10:32:00.001-07:002016-07-29T10:33:57.131-07:00WebDriver DefaultWait in C#
<h3>WebDriver DefaultWait in C#</h3><br />
<b>DefaultWait</b> (FluentWait in Java) utilizes DefaultWait class of the C# Webdriver bindings. WebDriverWait class inherits from the DefaultWait class in C#.<br /><br />
Here, in this post, we will look at a simple example of DefaultWait.<br /><br />
In this example we are using two files "button.html" and "styles.css". When we double click on "button.html" to open it, it opens in the browser and after 10 seconds a button named "proceed" is displayed on it.<br /><br />
Our button.html file: <br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSPGfXq2JXKBAVrAuDrjxVh80f5Hr78Y0kSlsaLGpngcsY220tGsxkhaoBDQo97CDI2CJWYB7l9G9ONsHZQw-1uAEEZHW6MWs8YauIbCeP_CfX4WMitGpOIgM3mkD5NqcP0AiSpEKmJDE/s1600/button_html.JPG" />
<br />
<br />
Our styles.css file:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5bhV3Awdx1BDCvAgTZTvQkzXhXC50_OfiW4o2ALosHls9TFBStFD36DuIxsBJip_VHnTRavkqpd9rm7uiBZ35iE__ZwyvxNF0pCyaj_O_mpmlo4rhaisIXnlULWNCahTCAakF4Sqt4e0/s1600/styles_CSS.JPG" />
<br /><br />
Put both the files in one folder. When you open the button.html file in a browser (I used Firefox), below screen is shown.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCk6SJs7sOInGs4zI7LFkGcf3BXI3x4fP5tut_cBmTYEdrA5oHaBZf1a3aP7w7VtlgCFZcyq3mRg14ewaT26FrrSl5iAI1lzW2oTBw30gf8iwU2xnAeGbuBmhNcs-Xc8J43ru9E2sE9tI/s1600/result_1.JPG" />
<br /><br />
Then after 10 seconds "proceed" button will be visible. This is because we have set the timer to 10000 milliseconds or 10 seconds in our button.html file.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOnT8P4NFn7wzuinI64e1l2z2WJ99nKHBvBR92eYMlTD1VG2jvXTboC6BVi28004gBwcCjIsZFaKdgfGYk0KJvob3mtH1yKK3B2e7oOUVxWaTteAuoMd8A7AAfKXL8WwDj-WotqKwArU4/s1600/result_2.JPG" />
<br /><br />
Lets run a small example using above files. In this example we keep waiting until an element is displayed.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1CGd0aSoHTSZ4K7ZKyyhzeT6XpQqKQjejS-mZ682fbjMKRJoMGinqXlXcV5ysRFc02OX3BQ8TcHSYYmj1FdWafNSs0UmlSjmGdzs0ij_roM75D0pxpNgmrjg6cHSvPCUOgq8c5My9br4/s1600/DefaultWait_6.JPG" /><br /><br />
Lets go through the above code:<br />
Create a new instance of Firefox driver. Navigate to the desired URL. Find the element. We are finding it using Id. Create wait using <b>DefaultWait</b>. Here <i>element</i> is the element on which wait is to be applied. We are using two properties of DefaultWait here, namely <i>timeout</i> and <i>polling interval</i>. Timeout is how long to wait for the evaluated condition to be true. The default timeout is 500 milliseconds. PollingInterval is how often the condition should be evaluated. The default timeout is 500 milliseconds.<br />
Next there is a function "waiting" which takes input parameter as IWebElement and outputs a boolean value. As soon as an element is displayed on screen, "true" is returned and the function ends. We are calling the function on line “wait.Until(waiting)”. The function ends when true is returned i.e. the function keeps running as long as false is being returned.
<br /><br />
Output of the above test can be seen below. Test was run on Visual Studio.<br /><br />
As you can see that “proceed” button is displayed after 10,000 milliseconds and polling is done after every 2500 milliseconds, hence, in our output, text “still waiting” is shown 4 times (10000/4).<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8ao_ukT0O9FUf-Y-eMDbHxDlx0J38Q57gpXY1lnwxZOfEuUkcI3ME3tl0kLUCkz9bggylz75nPkLnEzLPomTCVFAKRVKLx7_EzKNW0PBkJAZGCZ4QVpNv5b7w8pbSdI4l-nITJhe2bB4/s1600/DefaultWait_4.JPG" />
<br />
<br />
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-59936715058765529562016-06-25T09:38:00.000-07:002017-02-02T09:31:05.388-08:00WebDriver - Find All Links <div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Find All Links</h3> <br />
An example of finding all links on a webpage using WebDriver. Suppose we have following ".html" file with 3 links:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPPDlBBbdaAr2cgcoTwx9iOsNlXHKC_XO7gpfvSrSkWyTsMvYLzicPrLkKtVLFIRLPwfk1xoXhlaMxsgKKZLEbV5ND7mk4OhBw4GLLOSTNwobh5JuDCZIafthrdMHwdNauM4FsyO3wq8A/s1600/find_all_links_webdriver_0.JPG" />
</br>
Open the file in a browser and it look likes this:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUqgv-Bd9Wbct2MQhjafiLymNmDz099OKwjw1snEUOiPTdQKdAOZeGo1Y2KTEmPuxQN_m0QtIUl3SdDjgP7nTVx85SV-V0K-23IdagNweKkNYoGi9F_Xb3MauVDcYasnFW8FRgpv93I8E/s1600/find_all_links_webdriver_1.JPG" />
</br>
Below is the code which finds all the links on a webpage. We are navigating to a link using Firefox. Next we get all "A" tags using FindElements and iterating through those using ForEach loop.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8THoHRRs8lngYVmkn58Ca5y7IV33nHgmCuSF3uM5_CEGBQMxfd5jtIAh6bYrzeQ1TpHZrrV2LKAdDWb4bOmAbzYfip2vNbZRFwg7n79epqk0N4aQUv0Jknpdod44a2pllSp82OydcabA/s1600/find_all_links_webdriver_2.JPG" />
</br>
After the code run, output looks like as shown below:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtnHk9iGpiuXru_Gq2Hns-Y9xWtOR1j2crs8JW_LRFu3fVewULxnIvbGPSfTnYs7F0TGfRBVSZ00bwGWvyPU8l3bfNyoz-E8IICyiamItI9QtQXDuZEA8ukPuWPHExLxf_V-guMs6mM3k/s1600/find_all_links_webdriver_3.JPG" />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-11397658915092452792016-06-12T09:11:00.000-07:002016-06-12T09:12:24.899-07:00Drag & Drop - Selenium WebDriver <div dir="ltr" style="text-align: left;" trbidi="on">
<h3>Drag & Drop - Selenium WebDriver</h3><br />
For drag & drop, we will use the below example. You can see the screenshot of a web page below before running the drag & drop code.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBRvnm0NALJ4hbL0BD5B2pVku8XyF_yobO7m5hOBUSm2f27oYiyi2y6RV_Sjeoz2PUpMKAVC8JbDqvj7M1IDXqucPnXNxtRZbNZiiPAsqqGdBckCqOS65vAyTbmUfQGpcfzicwaLGGKs/s1600/drag-drop-webdriver-0.JPG" /><br /><br />
This is our drag & drop code which we will run in order to drag element "A" into the grey rectangle.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyW6irOJDr_aTt4nX6q10FX99sOBGs1z6awPmwJ7Ik4grRcDBtTP6Z-0-LAU_Pns7B-D7uIjqe2WYya3LU8inqruwne2P8X4TL-QAuz1rLLxv1HghGyNH5AM4AnDEycDsB9XkjXuKSe4s/s1600/drag-drop-webdriver-1.JPG" /><br />
As far as the code is concerned, create a new driver for Firefox (You can try it on Chrome also. I tried and it worked fine.). Navigate to the desired URL. The URL which we are using in this example is <i>http://mikeplate.github.io/jquery-drag-drop-plugin/example/simple.html</i>. Maximize the browser window (its always a good idea). Find an element to the dragged / dropped and also the element on which it needs to be dropped, like source and target in the above code. <br />
Next we are creating an instance of an Actions class. Then a little pause is there with
Thread.Sleep, otherwise the code will run too fast and you will not the able to see the actual drag and drop. Usually Thread.Sleep is not a good idea. At last we are holding the source, moving it to the target and releasing it. <br /><br />
Below is the screenshot after our code run.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7uXzoI7I5FF1Dk5__W1R8yLlxCKOfhu4xUNdwcDDNCWyfSgVjCk84jc_qzVYnfqEZYqA0C3yIsVXoLZMP9ng4nAuj9P2JsnyBLd7o56K5CXZrQoZVgeQqsDAjEklDU2oywn1-Zcyi-8E/s1600/drag-drop-webdriver-2.JPG" /><br /><br />
Another way to write the above code:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpAhMU3e7Qt7pxkx3SRb3qIxzk3Ec3UW0t0aLRjOjMCQoyjUgsL16WYR_ofkKck8JQI3wK0tN6PcPpIj2UnG45VJQXth4quHSE1UzFkSbYZwULJcQC2HvL06skUCZuyunPRMe1ACkNJTo/s1600/drag-drop-webdriver-3.JPG" /><br /><br />
Drag & Drop for HTML5 is not supported at <a href="https://code.google.com/p/selenium/issues/detail?id=3604">present</a>. And there is a workaround to <a href="https://gist.github.com/rcorreia/2362544">this</a>.
<br />
<br /></div>
Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-62017501541481806402016-05-28T23:23:00.003-07:002016-05-28T23:26:36.394-07:00WebDriver - Double-click an Element<h3>WebDriver - Double-click an Element</h3>
<br />We will see an example of <b>double-clicking an element using WebDriver</b>.
Write the below code in a notepad and save it as an ".html" file.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSQIYXlRlK0iZWkXbPoNh668EIEoSsYOEotb7dGv6YSuAkOcWoC0V4lx_Fd6IDIyrcCvD9GZsmyUGF3B_NLjE7VFgj5fjZ6pRSINHANKPYm-4jrR2Pwx6RymDG8aHysQs0bX-hqf3Er8E/s1600/webdriver_double_click_element_1.JPG" />
<br /><br />
On opening the above file in Firefox, it looks as shown below. In our WebDriver code we will double click on text "Double-click here".<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmTaSG5CNMp-oFIOOiBEFEIHciV80o-_4vZb3X5NMQXGJjDxwRI6xMVCNmD0bAdnRAtKgMmZ5J3W9fFZFNQy4vIJCzzG0xf1Zr30ib_D-D7HoiBhmnOFqARV3FY6nc2B_nut23BD6o7TM/s1600/webdriver_double_click_element_2.JPG" />
<br /><br />
When you double click on text "Double-click here", it shows another line of text "Hey! you just double-clicked :-)".<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqPeuPK2QjCTmWTBDACVRAPO7qD5v_mmua6wV8hhLJU5_l2J3VTdmbcIApkjfvQQ9VStN7hzvbZg6JMP_24lOsvNTGqbQIONnp1Y44uOTmhpFbRqR27AVJe2pwL8BnT-AZ79Mfk3NvekQ/s1600/webdriver_double_click_element_3.JPG" />
<br /><br />
Below is the code for double-clicking. Lets go through the code one line at a time. <br /><br />
Firstly we are creating a new instance of the Firefox driver. Second line is navigating to a file saved on our desktop. Then an instance of Actions class using WebDriver instance is being created. In the next line we are finding the line, where we need to double-click, using the SPAN tag. Additional sleep is added, otherwise test will run too fast that we will not be able to watch anything happening. At last, using actions we are moving to the target line and double-clicking it.<br />
<img border="1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2h4RvVXwIgAOUh0-dUtuy4sVzY3TqECd4_dkbLF10C6E3f165_p3EAW8xTZ9ZmkpEUE58QkA0hrvTEhtK4dFqRHm28gBNsGD13PWJDJjYzPRrym5y961s_kikBaJWMI4s4X_KNdfNa7Y/s1600/webdriver_double_click_element_4.JPG" /><br /><br />
In the above code, second last line is commented. We can use either of these (last or second last) to double click.
<br />Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-37834426237168531532016-05-17T09:32:00.000-07:002016-05-17T09:34:00.699-07:00WebDriver Page Object Model<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver Page Object Model</h3><br />
The basic idea of <b>page object model</b> is that you will have a class for each page in the application. This class contains methods which represent the functionality on that page.
So the tests do not directly interact with the page, they use page class which in turn knows how to do the requested functionality on a page using an automation tool like WebDriver.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH87_Xqll0O4Nv9Ib0Vbwi_p0TSmm-1tdWEeUvCNi0z-xdONq9O8nHPa9lQ6ITGmqftFSJeA13xemOuQm564okphHyzMdZ3dhSaPwhXXN5vij8RpX7GIhECXBiY-_t7WlQuotrKtMKxAc/s1600/webdriver_page_object_model_1.png" />
<br />
In the example (below) you can see a login page and it’s represented in our framework (above) using LoginPage class. This class has methods that map to a functionality that the login page would expose to a user. The methods in a LoginPage class should correspond to the actions a user would do on the page.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYAjpMZ1En1oLJkDNg-No106BD11Bv89_bHym3JDhfXOK0NG1jy2zMzP8TlMl2wLgZzwgDZTg0noW6o0lJGsXQnU3LxuMhR_sUgpotQSOmgWkzFVWqKQt_KpqC3BsiDC9eFQs9nGLUxHE/s1600/webdriver_page_object_model_2.png" /><br />
<br />
Take another example, suppose we have this below page of Gmail which we want to model. Here do not try to blindly model every user interface element (e.g. every button, text box, link etc) on this page. Instead, try and think what user, using this page, will try to do on this page like search emails. We can write a method named <i>Search_Emails</i> which takes the search criteria, types the criteria in the search box and clicks the Search button. So it’s better to have it combined rather than having separate methods/properties etc. to type into the box and clicking Search button and so on.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHv5TOzB92UwCzywOeouoOAIFKp6v6j-S4uLin_aKtBfeAn3Pb6ALnKuCAuEpaxgfC-7Uqx7GtQwncp0vvNZcO_EtH4pwUsXeX75wBuxFMNngOyo4dTBnQSxvk6gTY0wovKasapnOiojs/s1600/webdriver_page_object_model_3.png" />
<br /><br />
Also see:<br />
<a href="http://selenium-tutorial.blogspot.com/2012/06/webdriver-page-objects-pattern.html">Selenium WebDriver Page Objects</a>
<br /></div>Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-8908879952581986892016-04-17T09:03:00.001-07:002016-04-17T09:04:28.789-07:00WebDriver MouseHover Actions<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver MouseHover Actions</h3><br />
We will look at two examples of WebDriver mouse hover actions:<br /><br />
<b>Example 1 of WebDriver mouse hover</b><br />
For the first example we will work with the below simple example of mouse hover. When we mouse hover on Dropdown button, the 3 Sub-Menus open.<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNCVfPn9XKZsveuH4rwjOEcSn8YPACGsO5butJa9Ln3KnB3TRx6RityTDoQ1KMqQloiO3GD6NoCJb41wCJdx3tryRqi_MK2SGSDWQm6kpw5DobMETLuPjT70Ru6i2uF38qBBuAsSPZYOQ/s1600/webdriver_mouse_hover_1.png" /><br /><br />
If you do inspect element for Dropdown button it looks as below:<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyDwzi2G8NT8C0c6STyN8tWW4oNwCjkCMiBeeYcH63e54MxY0pOCUtuoCm2d1ZWJ6e-1WTnNano1Ls6FMtVBzBFqT-FMNwoCO4d1GR23WhDHbKlkggOclGwKRSLy4fW2bp2rKnRhXIFBw/s1600/webdriver_mouse_hover_2.png" />
<br /><br />
Lets look at the code which works with the above Dropdown button and hover on the Dropdown button to open three sub-menus.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2XAWu00AU_LedDazSyOIgwR6O5yHzz1c3lNs2Kijt6aUGEl-RPOActlLhRSOG8L6JXMhT4nNYir0CDZlO0Oi2tvzYd5MIGk6O1c7qyXuOVAcnvEEFcf_deAP0mJTh7U3-1rN0cIEcTaw/s1600/code_1.png" />
<br /><br />
Make sure you add "using OpenQA.Selenium.Interactions;" namespace at the top of the code. This namespace needs to be added in order to use the Actions method. We are using Firefox browser. After browser is launched we open the file which contains the code for our Dropdown button. An object named action is being created from Actions class and a driver object is being passed to it. After that we are identifying the Dropdown button. Finally mouse hover action is being done using Perform().
<br /><br />
<b>Example 2 of WebDriver mouse hover</b><br />
In our second example we will look at a little bit more complex example then the first one. Here we are hovering two times. First on the Tutorials then on the Web Design. Below menus are taken from http://line25.com/.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9cvIO15CAFRfo7lb6wSNbvjAfsABh1SdoXKo0dMFctSsSve72SKh3LpGp3hwsuU5Fe5CqtvP6PpznFZ8UreqHoNIHeCS4JIzBYa9RsoX1tR218S8-pidBWixPJYJjgt_L7Mby6HJ37mo/s1600/webdriver_mouse_hover_3.png" />
<br /><br />
and if we do inspect element for Tutorials it looks as shown below:
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOP5jLJsHXct-GrE5V2RGI5sjkKoEK3lpurfcea5vJtBuXTD04cB91gAqLqxFfw8vcEbJGcx8_VVy24YXJNdOj7GS6urEtlYi_-K_YGoJ5DHnTRgIagDXTXnOmU2H3nkWNTgLAYBC1a-4/s1600/webdriver_mouse_hover_4.png" />
<br /><br />
The code to work with the above Tutorials menu is:
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPK_5YfCUILVPY_nFOmnS4XZfSrUvQr1s9ptepgkXvbRBPNARfUrPZ5dvn72ZlmGG7tUkYnnFEo1EDrUtNGw6HkzaVKKZDaUh7j9Td2gERnqxLcE90KXmzok2iPdcJD9tP0WiBHseQfi0/s1600/code_2.JPG" />
<br /></div>Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.comtag:blogger.com,1999:blog-7590876822205797344.post-87239945549020491552016-02-27T05:29:00.003-08:002016-02-27T05:32:12.916-08:00WebDriver - Select Item From Drop-down<div dir="ltr" style="text-align: left;" trbidi="on">
<h3>WebDriver - Select Item From Drop-down</h3><br />
In the below examples we are using drop-down from Amazon.in and are trying to select 3rd item ("Baby") from the drop-down.
<br /><br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipdWae-r0ejgXWa-lonC85Q6UEbYy-hAOBl2QgQoAiizV1TO-tXwSTtgvMui7m8kMNvRSTEXwvKhRVjwNCf9oRXxzefFrVEy4huFtz5vZE7jn4aFRLWVaSaRGVhIm8leeXuu57jnBylAs/s1600/select_item_from_dropdown_1.JPG" />
<br /><br />
<b>Example 1 - Select an Item from Drop-down</b><br />
<pre>
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
namespace Project_1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://amazon.in");
var select = driver.FindElement(By.Id("searchDropdownBox"));
var babyOption = select.FindElements(By.TagName("Option"))[2];
babyOption.Click();
}
}
}
</pre><br />
In above code, we are using "TagName" to find an element or rather array of elements using FindElements.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6LB-bPEPpmC48XKGnmywe9Hjuyg5EGJ8IkP48lvjExDiy0OivuIpF86FVUMiAaKfuy_dOYUP_l41wLKTVnH2NdB3NTv0C2pCttoC1swHs5ryNoMhHdJ4uenjvclqSqfpumO_9JVgTES8/s1600/select_item_from_dropdown_2.JPG" />
<br />
As we have to select "Baby" from the drop-down list and "Baby" being the 3rd item in the list, we have used the index 2 above (as it is 0 indexed). In the last line we are using the click method to select an item.
<br /><br />
<b>Example 2 - Select an Item from Drop-down</b><br />
<pre>
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
namespace Project_1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://amazon.in");
var select = driver.FindElement(By.Id("searchDropdownBox"));
var selectElement = new SelectElement(select);
selectElement.SelectByText("Baby");
}
}
}
</pre>
<br />
In above code, we are wrapping WebElement into Select Object and using method like SelectByText to find an item in the drop-down. For this to work, make sure you have added "WebDriver.Support" DLL along with "WebDriver" DLL.
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWbEuE83a1rbN9LmQlpll3xUE89Raef1YSa8Tjkb-SEDVEfCEqhmeE9yLFo9g_LridmXfaxn1_SGoZW2yGbzT3wUeZWMeG3jl4UszG_iizWSKL_pP3-T_cSUzPH_bYNr5HDC9LxTvjcCc/s1600/select_item_from_dropdown_3.JPG" />
<br /></div>Sachin Dhallhttp://www.blogger.com/profile/10881803310787301970noreply@blogger.com