Skip to content

Instantly share code, notes, and snippets.

@rahulrathore44
Last active July 25, 2021 18:33
Show Gist options
  • Save rahulrathore44/56ee542bf9d107c890085f49e1cff0f9 to your computer and use it in GitHub Desktop.
Save rahulrathore44/56ee542bf9d107c890085f49e1cff0f9 to your computer and use it in GitHub Desktop.
Webdriver Containers - Testcontainers can be used to automatically instantiate and manage containers that include web browsers
package com.api.automation;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.utility.DockerImageName;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class BaseTest {
// 1. Before Suite -> Code to start the test-container --> which start the selenium container
// 2. After Suite -> Code to stop the container
// 3. Base Test will be the parent class for all the test classes
private static String imageName = "selenium/standalone-chrome-debug";
private static String imageTag = "3.141.59";
protected static BrowserWebDriverContainer<?> chrome = null;
@BeforeSuite
public static void setUp() {
Testcontainers.exposeHostPorts(9091);
DockerImageName image = DockerImageName.parse(imageName).withTag(imageTag);
chrome = new BrowserWebDriverContainer<>(image).withCapabilities(new ChromeOptions());
chrome.start();
}
@AfterSuite
public static void tearDown() {
if(chrome.isRunning()) {
chrome.close();
chrome.stop();
}
}
}
package com.api.automation;
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.testcontainers.shaded.org.apache.commons.io.FileUtils;
import org.testng.annotations.Test;
public class TestBugzillaUsingContainer extends BaseTest {
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']");
@Test
public void testBugzilla() throws IOException, InterruptedException {
String appUrl = getUrl("host.testcontainers.internal", 9091);
WebDriver driver = chrome.getWebDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.get(appUrl);
driver.findElement(enter_bug).click();
driver.findElement(bugzilla_login).sendKeys("[email protected]");
driver.findElement(bugzilla_password).sendKeys("password");
driver.findElement(log_in).click();
takeScreenShot(driver);
TimeUnit.SECONDS.sleep(2);
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
driver.findElement(log_out).click();
}
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