{"id":20252,"date":"2015-05-28T14:37:26","date_gmt":"2015-05-28T09:07:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20252"},"modified":"2015-12-17T16:34:17","modified_gmt":"2015-12-17T11:04:17","slug":"useful-functions-for-automation-framework-using-selenium-web-driver","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/useful-functions-for-automation-framework-using-selenium-web-driver\/","title":{"rendered":"Useful Functions for Automation Framework using Selenium Web Driver"},"content":{"rendered":"<p>While doing automation using selenium, there are certain repetetive tasks that we need to perform in order to handle page elements. So, the aim of this blog is to highlight some common operations that we need to handle every now and then.<\/p>\n<p><strong>Some operations are:<\/strong><br \/>\n1. Open URL in different browsers (Chrome, Firefox, IE)<br \/>\n2. Do mouse-hovers<br \/>\n3. Taking snapshots for analysing \u00a0the failures<br \/>\n4. Getting current time-stamp<br \/>\n5. Handling a drop-down<\/p>\n<p><strong>1. Open URL in different browsers (Chrome, Firefox, IE)<\/strong><\/p>\n<p>Do the following steps first, to use this method:<br \/>\n1. Download &#8216;chromedriver.exe&#8217; and &#8216;IEDriverServer.exe&#8217; from<br \/>\n&#8220;http:\/\/www.seleniumhq.org\/download\/&#8221;<br \/>\n2. Then save the downloaded file in &#8216;Drivers&#8217; folder at path where &#8216;src&#8217; folder exists<\/p>\n<p>call &#8216;OpenApp&#8217; function with passing two parameter: &#8216;BrowserName(ie. CH\/FF\/IE) &amp; URL&#8217;<br \/>\ne.g. OpenApp(&#8220;CH&#8221;,&#8221;http:\/\/www.seleniumhq.org\/&#8221;) it will open this url in ChromeBrowser<\/p>\n<p>[java]<br \/>\npublic static WebDriver OpenApp(String BrowserName, String url){<br \/>\nfn_LaunchBrowser(BrowserName);<br \/>\nfn_OpenURL(url);<br \/>\nreturn driver;<br \/>\n}<br \/>\npublic static void fn_OpenURL(String url){<br \/>\ndriver.get(url);<br \/>\ndriver.manage().window().maximize();<br \/>\n}<\/p>\n<p>public static WebDriver fn_LaunchBrowser(String browsername){<br \/>\nif(browsername==&quot;CH&quot;){<br \/>\nSystem.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;Drivers\\\\chromedriver.exe&quot;);<br \/>\ndriver= new ChromeDriver();<br \/>\n}else if(browsername==&quot;FF&quot;){<br \/>\ndriver= new FirefoxDriver();<br \/>\n}else if(browsername==&quot;IE&quot;){<br \/>\nSystem.setProperty(&quot;webdriver.ie.driver&quot;, &quot;Drivers\\\\IEDriverServer.exe&quot;);<br \/>\ndriver= new InternetExplorerDriver();<br \/>\n}<br \/>\ndriver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);<br \/>\nreturn driver;<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2. Do mouse-hovers<\/strong><\/p>\n<p>Incase, we need to click on a sub-menu that is visible only when users\u00a0do mouse-hover on the main-menu, then we can do it using this\u00a0function. Just pass web element position to this function.<br \/>\ne.g MouseOver(driver.findElement(By.name(&#8220;Main-Menu&#8221;)))<\/p>\n<p>[java]<br \/>\npublic static void MouseOver(WebElement we){<br \/>\nActions actObj=new Actions(driver);<br \/>\nactObj.moveToElement(we).build().perform();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3. Take Snapshot for analysing the failures<\/strong><\/p>\n<p>Scenario &#8211; As an automation tester, we must need to take snapshots of error &amp; exceptions while running the script as a proof, so we can use this method.<br \/>\ne.g. fn_TakeSnapshot(driver, &#8220;where you want to save the SnapShot&#8221;)<\/p>\n<p>[java]<br \/>\npublic static String fn_TakeSnapshot(WebDriver driver, String DestFilePath) throws IOException{<br \/>\nString TS=fn_GetTimeStamp();<br \/>\nTakesScreenshot tss=(TakesScreenshot) driver;<br \/>\nFile srcfileObj= tss.getScreenshotAs(OutputType.FILE);<br \/>\nDestFilePath=DestFilePath+TS+&quot;.png&quot;;<br \/>\nFile DestFileObj=new File(DestFilePath);<br \/>\nFileUtils.copyFile(srcfileObj, DestFileObj);<br \/>\nreturn DestFilePath;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4. Get current Time-stamp<\/strong><\/p>\n<p>Scenario &#8211; While reporting your script status, you need to pass time &amp; date that when your test-script has finished. Another scenario: If you need unique username every time, you can append the timestamp with any constant string (e.g SumitMay28201513_15_10 PM), so here you are:<\/p>\n<p>[java]<br \/>\npublic static String fn_GetTimeStamp(){<br \/>\nDateFormat DF=DateFormat.getDateTimeInstance();<br \/>\nDate dte=new Date();<br \/>\nString DateValue=DF.format(dte);<br \/>\nDateValue=DateValue.replaceAll(&quot;:&quot;, &quot;_&quot;);<br \/>\nDateValue=DateValue.replaceAll(&quot;,&quot;, &quot;&quot;);<br \/>\nreturn DateValue;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5. Handle drop-down (select an option from list)<\/strong><\/p>\n<p>Scenario: If you need to select an option from any drop-down, you can use following methods<br \/>\nNOTE: &#8216;Select&#8217; class in selenium works only those drop-downs, which lies in\u00a0&lt;select &gt; tag, like in the following html.<\/p>\n<p>[html]<br \/>\n&lt;div&gt;&lt;select id=&quot;SelectID_Three&quot;&gt;&lt;option value=&quot;selectValue&quot;&gt;Select&lt;\/option&gt;<br \/>\n&lt;option value=&quot;firstcolour&quot;&gt;Yellow&lt;\/option&gt;<br \/>\n&lt;option value=&quot;secondcolour&quot;&gt;Lime&lt;\/option&gt;<br \/>\n&lt;option value=&quot;thirdcolour&quot;&gt;Red&lt;\/option&gt;<br \/>\n&lt;\/select&gt;&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>The following code will help you solve the problem:<\/p>\n<p>[java]<br \/>\n\/\/select the dropdown using &quot;select by visible text&quot;, so pass VisibleText as &#8216;Yellow&#8217; to funtion<br \/>\npublic static void fn_Select(WebElement WE, String VisibleText){<br \/>\nSelect selObj=new Select(WE);<br \/>\nselObj.selectByVisibleText(VisibleText);<br \/>\n}<\/p>\n<p>\/\/select the dropdown using &quot;select by index&quot;, so pass IndexValue as &#8216;2&#8217;<br \/>\npublic static void fn_Select(WebElement WE, int IndexValue){<br \/>\nSelect selObj=new Select(WE);<br \/>\nselObj.selectByIndex(IndexValue);<br \/>\n}<\/p>\n<p>\/\/select the dropdown using &quot;select by value&quot;, so pass Value as &#8216;thirdcolor&#8217;<br \/>\npublic static void fn_Select(WebElement WE, String Value){<br \/>\nSelect selObj=new Select(WE);<br \/>\nselObj.selectByValue(Value);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Hope\u00a0it will ease your tasks, Happy Learning !! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While doing automation using selenium, there are certain repetetive tasks that we need to perform in order to handle page elements. So, the aim of this blog is to highlight some common operations that we need to handle every now and then. Some operations are: 1. Open URL in different browsers (Chrome, Firefox, IE) 2. [&hellip;]<\/p>\n","protected":false},"author":201,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":24},"categories":[1818,1816],"tags":[1561,14,4844,25,1562],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20252"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/201"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20252"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20252\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}