Run Selenium Test in Google Chrome Browser
Run Selenium Test in Google Chrome Browser
In this article, we will run a sample selenium test in the Google Chrome browser. To run the test in Chrome we need to set the system property to the path of the Chrome driver as shown below:
Environment:
- Google Chrome,
- Windows 10 Operating System
Download Chrome Browser
Steps to Download Chrome are listed here:
(https://www.testingdocs.com/questions/how-to-download-chrome-browser-on-windows/)
System.setProperty(“webdriver.chrome.driver”, “chromedriver.exe”);
Sample Code
public class ChromeBrowserExample { private WebDriver driver; @Test public void sampleTest() { driver.get("https://www.google.com"); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); Assert.assertEquals(driver.getTitle(), "Google", "I'm verifying window title here in this test"); } @BeforeClass public void beforeClass() { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); } @AfterClass public void afterClass() { if(driver != null) { driver.quit(); } } }
Explanation
The above sample TestNG test shown, opens the Chrome browser with Google search page. It waits for 30 sec implicitly and verifies the title of the window. The before class annotated method initializes the chrome driver
The test annotated method navigates to the Google search home page. The after class annotated method kills the driver session at the end.