Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remote Grid Selenium TestBase
package framework;
import static framework.Constants.BROWSER_CAPABILIITY;
import static framework.Constants.ENABLE_VIDEO;
import static framework.Constants.ENABLE_VNC;
import static framework.Constants.NAME;
import static framework.Constants.THIRTY;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SeleniumTestBase {
@Rule
public TestName testInfo = new TestName();
private String currentTestName = "";
protected RemoteWebDriver browser;
protected List<Page> pagesToInitialize;
private URL seleniumGrid;
@After
public void tearDown() {
this.browser.quit();
System.out.println("Recording at http://localhost:4444/video/" + currentTestName + ".mp4");
}
@Before
public void setup() throws MalformedURLException {
System.out.println("Running Selenium Test: " + testInfo.getMethodName());
this.seleniumGrid = new URL("http://localhost:4444/wd/hub");
this.currentTestName = this.getClass().getSimpleName() + "-" + testInfo.getMethodName();
this.browser = getBrowserSession("chrome");
this.pagesToInitialize.forEach(p -> p.configure(this.browser));
}
private RemoteWebDriver getBrowserSession(String browser) {
DesiredCapabilities abilities = new DesiredCapabilities();
abilities.setCapability("name", testInfo.getMethodName());
abilities.setCapability("enableVNC", true);
abilities.setCapability("enableVideo", true);
abilities.setCapability("videoName", this.currentTestName + ".mp4");
abilities.setCapability("browser", browser);
RemoteWebDriver driver = new RemoteWebDriver(seleniumGrid, abilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment