{"id":802,"date":"2019-12-05T19:37:58","date_gmt":"2019-12-05T14:07:58","guid":{"rendered":"http:\/\/http:\/\/artoftesting.com\/\/?p=802"},"modified":"2021-05-13T13:54:44","modified_gmt":"2021-05-13T08:24:44","slug":"selenium-with-java-example","status":"publish","type":"post","link":"https:\/\/artoftesting.com\/selenium-with-java-example","title":{"rendered":"Selenium with Java Example"},"content":{"rendered":"\n<p><a href=\"http:\/\/artoftesting.com\/automation-testing\">Test automation<\/a> requires a tool that can automate the application, a testing tool or library that provides various capabilities like &#8211; test result reports, a different type of validations, etc, and a programming language that both these tools or libraries support.<br><br>In this Selenium with Java Example, we will use the following to test the Google Calculator feature-<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Selenium WebDriver<\/strong> &#8211; For UI automation<\/li><li><strong>TestNG <\/strong>&#8211; As a testing framework<\/li><li><strong>Java <\/strong>&#8211; As a programming language<br><\/li><\/ul>\n\n\n\n<p>Using all these, we will automate the Google Calculator feature on the Chrome browser. For running the test on the Chrome browser, we need to set the <strong>webdriver.chrome.driver<\/strong> system property and point to a chrome driver executable file.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Download the latest ChromeDriver binary from\u00a0the <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\" target=\"_blank\" rel=\"noreferrer noopener\">Chromium.org download page<\/a>\u00a0and place the executable on your local machine.<br><br><\/li><li>Set the webdriver.chrome.driver property to the chromeDriver.exe\u2019s location as-<br><em><strong>System.setProperty(\u201cwebdriver.chrome.driver\u201d, \u201cchromeDriver.exe path\u201d);<\/strong><\/em><br><br><\/li><\/ol>\n\n\n\n<p>Now, let&#8217;s check the code snippet for this test. You can also download this java file here &#8211; \u00a0<a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/calculatorTest.java_.zip\" target=\"_blank\">calculatorTest.java<\/a>.<br><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class calculatorTest {\n\t\n\t@Test\n\t\/\/Tests google calculator\n\tpublic void googleCalculator(){\n\t\t\n\t\t\/\/Creating a driver object referencing WebDriver \n\t\tWebDriver driver;\n         \n\t\t\/\/Setting the webdriver.chrome.driver property to its executable's location\n\t\tSystem.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;\/lib\/chromedriver.exe&quot;);\n\t\t\n\t\t\/\/Instantiating driver \n\t\tdriver = new ChromeDriver();\n\n\t\t\/\/Set implicit wait of 10 seconds\n\t\tdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\n\t\t\n\t\t\/\/Launch google\n\t\tdriver.get(&quot;http:\/\/www.google.co.in&quot;);\n\t\t\n\t\t\/\/Write 2+2 in google textbox\n\t\tWebElement googleTextBox = driver.findElement(By.id(&quot;gbqfq&quot;));\n\t\tgoogleTextBox.sendKeys(&quot;2+2&quot;);\n\t\t\n\t\t\/\/Click on searchButton\n\t\tWebElement searchButton = driver.findElement(By.id(&quot;gbqfb&quot;));\n\t\tsearchButton.click();\n\t\t\n\t\t\/\/Get result from calculator\n\t\tWebElement calculatorTextBox = driver.findElement(By.id(&quot;cwos&quot;));\n\t\tString result = calculatorTextBox.getText();\n\t\t\n\t\t\/\/Verify that result of 2+2 is 4\n\t\tAssert.assertEquals(result, &quot;4&quot;);\n\t}\n\t\n}\n<\/pre><\/div>\n\n\n<p><br>This completes our tutorial on &#8211; Selenium with Java example. For a complete step-by-step Selenium tutorial series, please check &#8211; <a href=\"http:\/\/artoftesting.com\/selenium-tutorial\"><strong>Selenium WebDriver with Java Tutorial<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Test automation requires a tool that can automate the application, a testing tool or library that provides various capabilities like &ndash; test result reports, a different type of validations, etc, and a programming language that both these tools or libraries support. In this Selenium with Java Example, we will use the following to test the &#8230; <a title=\"Selenium with Java Example\" class=\"read-more\" href=\"https:\/\/artoftesting.com\/selenium-with-java-example\" aria-label=\"Read more about Selenium with Java Example\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1640,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,12],"tags":[],"class_list":["post-802","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium","category-testng"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Selenium with Java Example [Selenium-TestNG Sample Script]<\/title>\n<meta name=\"description\" content=\"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/artoftesting.com\/selenium-with-java-example\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selenium with Java Example [Selenium-TestNG Sample Script]\" \/>\n<meta property=\"og:description\" content=\"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/artoftesting.com\/selenium-with-java-example\" \/>\n<meta property=\"og:site_name\" content=\"ArtOfTesting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/artoftesting\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-05T14:07:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-13T08:24:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kuldeep Rana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:site\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kuldeep Rana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#article\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example\"},\"author\":{\"name\":\"Kuldeep Rana\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\"},\"headline\":\"Selenium with Java Example\",\"datePublished\":\"2019-12-05T14:07:58+00:00\",\"dateModified\":\"2021-05-13T08:24:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example\"},\"wordCount\":205,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg\",\"articleSection\":[\"Selenium\",\"TestNG\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/artoftesting.com\/selenium-with-java-example#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example\",\"url\":\"https:\/\/artoftesting.com\/selenium-with-java-example\",\"name\":\"Selenium with Java Example [Selenium-TestNG Sample Script]\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg\",\"datePublished\":\"2019-12-05T14:07:58+00:00\",\"dateModified\":\"2021-05-13T08:24:44+00:00\",\"description\":\"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.\",\"breadcrumb\":{\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/artoftesting.com\/selenium-with-java-example\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage\",\"url\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg\",\"contentUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg\",\"width\":700,\"height\":400,\"caption\":\"Selenium with Java Example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/artoftesting.com\/selenium-with-java-example#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/artoftesting.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automation Testing\",\"item\":\"https:\/\/artoftesting.com\/category\/automation-testing\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Selenium\",\"item\":\"https:\/\/artoftesting.com\/category\/automation-testing\/selenium\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Selenium with Java Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/artoftesting.com\/#website\",\"url\":\"https:\/\/artoftesting.com\/\",\"name\":\"ArtOfTesting\",\"description\":\"A Beginners Guide to Testing\",\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/artoftesting.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/artoftesting.com\/#organization\",\"name\":\"ArtOfTesting\",\"url\":\"https:\/\/artoftesting.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"contentUrl\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"width\":400,\"height\":60,\"caption\":\"ArtOfTesting\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/artoftesting\",\"https:\/\/x.com\/theartoftesting\",\"https:\/\/www.linkedin.com\/groups\/4797819\/\",\"https:\/\/in.pinterest.com\/artoftesting\/\",\"https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\",\"name\":\"Kuldeep Rana\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"caption\":\"Kuldeep Rana\"},\"description\":\"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Selenium with Java Example [Selenium-TestNG Sample Script]","description":"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/artoftesting.com\/selenium-with-java-example","og_locale":"en_US","og_type":"article","og_title":"Selenium with Java Example [Selenium-TestNG Sample Script]","og_description":"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.","og_url":"https:\/\/artoftesting.com\/selenium-with-java-example","og_site_name":"ArtOfTesting","article_publisher":"https:\/\/facebook.com\/artoftesting","article_published_time":"2019-12-05T14:07:58+00:00","article_modified_time":"2021-05-13T08:24:44+00:00","og_image":[{"width":700,"height":400,"url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg","type":"image\/jpeg"}],"author":"Kuldeep Rana","twitter_card":"summary_large_image","twitter_creator":"@theartoftesting","twitter_site":"@theartoftesting","twitter_misc":{"Written by":"Kuldeep Rana","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/artoftesting.com\/selenium-with-java-example#article","isPartOf":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example"},"author":{"name":"Kuldeep Rana","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5"},"headline":"Selenium with Java Example","datePublished":"2019-12-05T14:07:58+00:00","dateModified":"2021-05-13T08:24:44+00:00","mainEntityOfPage":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example"},"wordCount":205,"commentCount":1,"publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"image":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg","articleSection":["Selenium","TestNG"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/artoftesting.com\/selenium-with-java-example#respond"]}]},{"@type":"WebPage","@id":"https:\/\/artoftesting.com\/selenium-with-java-example","url":"https:\/\/artoftesting.com\/selenium-with-java-example","name":"Selenium with Java Example [Selenium-TestNG Sample Script]","isPartOf":{"@id":"https:\/\/artoftesting.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage"},"image":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg","datePublished":"2019-12-05T14:07:58+00:00","dateModified":"2021-05-13T08:24:44+00:00","description":"With this Selenium with Java example, we will automate the Google Calculator feature on the Chrome browser. You can also download the sample script.","breadcrumb":{"@id":"https:\/\/artoftesting.com\/selenium-with-java-example#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/artoftesting.com\/selenium-with-java-example"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/selenium-with-java-example#primaryimage","url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg","contentUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Selenium-WebDriver-with-TestNG-Sample-Script.jpg","width":700,"height":400,"caption":"Selenium with Java Example"},{"@type":"BreadcrumbList","@id":"https:\/\/artoftesting.com\/selenium-with-java-example#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/artoftesting.com\/"},{"@type":"ListItem","position":2,"name":"Automation Testing","item":"https:\/\/artoftesting.com\/category\/automation-testing"},{"@type":"ListItem","position":3,"name":"Selenium","item":"https:\/\/artoftesting.com\/category\/automation-testing\/selenium"},{"@type":"ListItem","position":4,"name":"Selenium with Java Example"}]},{"@type":"WebSite","@id":"https:\/\/artoftesting.com\/#website","url":"https:\/\/artoftesting.com\/","name":"ArtOfTesting","description":"A Beginners Guide to Testing","publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/artoftesting.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/artoftesting.com\/#organization","name":"ArtOfTesting","url":"https:\/\/artoftesting.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/","url":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","contentUrl":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","width":400,"height":60,"caption":"ArtOfTesting"},"image":{"@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/artoftesting","https:\/\/x.com\/theartoftesting","https:\/\/www.linkedin.com\/groups\/4797819\/","https:\/\/in.pinterest.com\/artoftesting\/","https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q"]},{"@type":"Person","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5","name":"Kuldeep Rana","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","caption":"Kuldeep Rana"},"description":"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals."}]}},"_links":{"self":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":1,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"predecessor-version":[{"id":5009,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/802\/revisions\/5009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media\/1640"}],"wp:attachment":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}