Test Automation Using Selenium ChromeDriver

What is Selenium ChromeDriver?

WebDriver driver = new ChromeDriver();

ChromeDriver Installation

Setting Up Maven Project To Run Our First Selenium-Based Chrome Test

  1. Java — JDK/JRE
  2. Eclipse or Intellij IDE
  3. Maven
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Blog { public static WebDriver driver; @BeforeTest
public void setUp() {
//setting path of chromedriver.exe
System.setProperty("webdriver.chrome.driver", "C://selenium-java-project//ChromeDriver.exe");
//to launch the chrome browser window
driver = new ChromeDriver();
//maximize the browser window
driver.manage().window().maximize();
}
@Test
public void loginPage() {
driver.get("https://www.google.com/");
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys("pcloudy");
searchField.sendKeys(Keys.ENTER);
WebElement pcloudyLink = driver.findElement(By.xpath("//a[@class='B1uW2d ellip PZPZlf']"));
pcloudyLink.click();
WebElement loginPage = driver.findElement(By.xpath("//*[text()='Login']"));
loginPage.click();
}
@AfterTest
public void tearDown() {
driver.quit();
}
}

Use Of ChromeOptions Class

  • Start-maximized: Opens chrome browser in maximized mode
  • Headless: Launch chrome in headless mode i.e. without GUI
  • Incognito: Launch chrome browser in incognito mode
  • Version: Displays/prints chrome browser version
  • Disable -extensions: Disable existing chrome extensions
  • Disable-popup-blocking: Disable pop-ups being displayed on chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
ChromeDriver driver = new ChromeDriver(options);
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Blog { public static WebDriver driver; @BeforeTest
public void setUp() {
System.setProperty("webdriver.chrome.driver", "C://Selenium-java//chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
options.addArguments("start-maximized");
// to launch the chrome browser window
driver = new ChromeDriver(options);
} @Test
public void loginPage() {
driver.get("https://www.google.com/");
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys("pcloudy");
searchField.sendKeys(Keys.ENTER);
WebElement pcloudyLink = driver.findElement(By.xpath("//a[@class='B1uW2d ellip PZPZlf']"));
pcloudyLink.click();
WebElement loginPage = driver.findElement(By.xpath("//*[text()='Login']"));
loginPage.click();
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
options.addExtensions(New File( < path of crx > ));
options.setBinary(new File( < path of binary file > ));
options.setAcceptInsecureCerts(true);

Manage Chrome Binary with WebDriverManager

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
WebDriverManager.chromedriver().setup();
  • It checks the browser version installed on your machine (e.g. Chrome, Firefox).
  • It matches the version of the driver (e.g. chrome driver, geckodriver).
  • If an unknown version is found, it uses the latest version of the driver. It downloads the driver if it is not present on the WebDriverManager cache (~/.cache/selenium by default).
  • It exports the Selenium required WebDriver Java environment variables.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;public class Blog { public static WebDriver driver; @BeforeTest
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
@Test
public void loginPage() {
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
@AfterTest
public void tearDown() {
driver.quit();
}
}

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
pCloudy

pCloudy

pCloudy is the most powerful cloud-based App Testing Platform. Brand Marketing @ pCloudy (www.pcloudy.com)