package org.testcontainers.junit;

import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
 *
 */
public class BaseWebDriverContainerTest {

    protected void doSimpleWebdriverTest(BrowserWebDriverContainer rule) {
        RemoteWebDriver driver = setupDriverFromRule(rule);
        System.out.println("Selenium remote URL is: " + rule.getSeleniumAddress());
        System.out.println("VNC URL is: " + rule.getVncAddress());

        //Runtime.getRuntime().exec("open " + rule.getVncUrl(driver)); // For debugging, on a Mac

        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("testcontainers");
        driver.findElement(By.name("q")).submit();
        assertEquals("the word 'testcontainers' appears in the search box", "testcontainers", driver.findElement(By.name("q")).getAttribute("value"));
    }

    @NotNull
    private RemoteWebDriver setupDriverFromRule(BrowserWebDriverContainer rule) {
        RemoteWebDriver driver = rule.getWebDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        return driver;
    }

    protected void doSimpleExplore(BrowserWebDriverContainer rule) {
        RemoteWebDriver driver = setupDriverFromRule(rule);
        driver.get("http://en.wikipedia.org/wiki/Randomness");

        loop:
        for (int i = 0; i < 5; i++) {
            Random random = new Random();
            List<WebElement> webElements = driver.findElements(By.tagName("a"));
            for (WebElement webElement : webElements) {
                if (random.nextInt(10) == 0 && webElement.isDisplayed() && webElement.isEnabled()) {
                    webElement.click();
                    break loop;
                }
            }
        }
    }

}

@@ -105,47 +105,8 @@
 By;%0A
-import org.openqa.selenium.WebElement;%0A
 impo
@@ -221,56 +221,8 @@
 r;%0A%0A
-import java.util.List;%0Aimport java.util.Random;%0A
 impo
@@ -1542,455 +1542,8 @@
 s%22);
-%0A%0A        loop:%0A        for (int i = 0; i %3C 5; i++) %7B%0A            Random random = new Random();%0A            List%3CWebElement%3E webElements = driver.findElements(By.tagName(%22a%22));%0A            for (WebElement webElement : webElements) %7B%0A                if (random.nextInt(10) == 0 && webElement.isDisplayed() && webElement.isEnabled()) %7B%0A                    webElement.click();%0A                    break loop;%0A                %7D%0A            %7D%0A        %7D
 %0A   

Simplify selenium tests for reliability