Site icon TestingDocs.com

Run Selenium Test in Google Chrome Browser

Introduction

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 chrome driver as shown below:

Environment:

Download Chrome Browser

Steps to Download  Chrome are listed out 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

Test annotated method navigates to google search home page. The after class annotated method kills the driver session at the end.

 

Selenium WebDriver Tutorials on this website can be found at:

https://www.testingdocs.com/selenium-webdriver-tutorial

For more details on the Selenium, visit the official website at:

https://www.selenium.dev/

Exit mobile version