Skip to content

Instantly share code, notes, and snippets.

@rahulrathore44
Created April 23, 2022 21:40
Show Gist options
  • Save rahulrathore44/5e1677e379fad6922aaaaf92b1414e2d to your computer and use it in GitHub Desktop.
Save rahulrathore44/5e1677e379fad6922aaaaf92b1414e2d to your computer and use it in GitHub Desktop.
Selenium Webdriver integration with Testcontainers
package com.api.automation;
import org.openqa.selenium.Capabilities;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.utility.DockerImageName;
public abstract class BaseContainerConfiguration {
protected BrowserWebDriverContainer<?> container = null;
/**
* Start the container
*/
@SuppressWarnings("resource")
protected void setUpContainer() {
Testcontainers.exposeHostPorts(9091);
DockerImageName image = DockerImageName.parse(getImageName()).withTag(getImageTag());
container = new BrowserWebDriverContainer<>(image).withCapabilities(getBrowserOptions());
container.start();
}
/**
* Stop the container
*/
protected void shutDownContainer() {
if (container != null && container.isRunning()) {
container.close();
container.stop();
}
}
abstract String getImageTag();
abstract String getImageName();
abstract Capabilities getBrowserOptions();
}
package com.api.automation;
import org.junit.AfterClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeClass;
public abstract class BaseContainerTest {
private static BaseContainerConfiguration configuration;
public BaseContainerTest(BaseContainerConfiguration configuration) {
BaseContainerTest.configuration = configuration;
}
@BeforeClass
public static void setUp() {
configuration.setUpContainer();
}
@AfterClass
public static void tearDown() {
configuration.shutDownContainer();
}
/**
* BrowserWebDriverContainer.getWebDriver() return the instance of RemoteWebdriver class.
* Calling the getWebDriver() multiple times will not return a new instance.
* It will return the same instance every time created by BrowserWebDriverContainer.containerIsStarted().
* @return Webdriver
*/
protected WebDriver getWebDriver() {
return configuration.container.getWebDriver();
}
}
package com.api.automation;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ChromeContainer extends BaseContainerConfiguration {
@Override
String getImageTag() {
return "3.141.59";
}
@Override
String getImageName() {
return "selenium/standalone-chrome-debug";
}
@Override
Capabilities getBrowserOptions() {
DesiredCapabilities chromeCap = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
chromeCap.asMap().forEach((key, value) -> {
options.setCapability(key, value);
});
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS);
return options;
}
}
package com.api.automation;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testcontainers.shaded.org.apache.commons.io.FileUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestBugzilla extends BaseContainerTest {
private WebDriver chromeDriver = null;
private By enter_bug = By.id("enter_bug");
private By bugzilla_login = By.id("Bugzilla_login");
private By bugzilla_password = By.id("Bugzilla_password");
private By log_in = By.id("log_in");
private By log_out = By.xpath("//div[@id='header']//a[@href='index.cgi?logout=1']");
public TestBugzilla() {
super(new ChromeContainer());
}
@BeforeMethod
public void openBrowser() {
chromeDriver = getWebDriver();
}
/**
* Do not call chromeDriver.close(). Calling the close method will make the
* current session invalid. That will result in "NoSuchSessionException: invalid
* session id"
*/
@AfterMethod
public void closeBrowser() {
if (chromeDriver != null)
chromeDriver = null;
}
@Test
public void testBugzilla() throws IOException, InterruptedException {
String appUrl = getUrl("host.testcontainers.internal", 9091);
chromeDriver.manage().window().maximize();
chromeDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
chromeDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
chromeDriver.get(appUrl);
chromeDriver.findElement(enter_bug).click();
chromeDriver.findElement(bugzilla_login).sendKeys("[email protected]");
chromeDriver.findElement(bugzilla_password).sendKeys("password");
chromeDriver.findElement(log_in).click();
takeScreenShot(chromeDriver);
TimeUnit.SECONDS.sleep(2);
System.out.println(chromeDriver.getTitle());
System.out.println(chromeDriver.getCurrentUrl());
chromeDriver.findElement(log_out).click();
}
@Test
public void testBugzillaAgain() throws IOException, InterruptedException {
String appUrl = getUrl("host.testcontainers.internal", 9091);
chromeDriver.manage().window().maximize();
chromeDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
chromeDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
chromeDriver.get(appUrl);
chromeDriver.findElement(enter_bug).click();
chromeDriver.findElement(bugzilla_login).sendKeys("[email protected]");
chromeDriver.findElement(bugzilla_password).sendKeys("password");
takeScreenShot(chromeDriver);
TimeUnit.SECONDS.sleep(2);
System.out.println(chromeDriver.getTitle());
System.out.println(chromeDriver.getCurrentUrl());
}
@Test
public void testGooglePage() throws IOException, InterruptedException {
chromeDriver.manage().window().maximize();
chromeDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
chromeDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.name("q"));
searchBox.sendKeys("webdriver");
takeScreenShot(chromeDriver);
TimeUnit.SECONDS.sleep(2);
System.out.println(chromeDriver.getTitle());
System.out.println(chromeDriver.getCurrentUrl());
assertEquals(chromeDriver.getTitle(), "Google");
}
private String getUrl(String host, int port) {
return String.format("http://%s:%d/bugzilla/", host, port);
}
private void takeScreenShot(WebDriver driver) throws IOException {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("/home/vagrant/Selenium/test"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment