StartBrowserAction extends AbstractSeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { if (!allowAlreadyStarted && browser.isStarted()) { log.warn("There are some open web browsers. They will be stopped."); browser.stop(); } else if (browser.isStarted()) { log.info("Browser already started - skip start action"); context.setVariable(SeleniumHeaders.SELENIUM_BROWSER, browser.getName()); return; } log.info("Opening browser of type {}", browser.getEndpointConfiguration().getBrowserType()); browser.start(); if (StringUtils.hasText(getBrowser().getEndpointConfiguration().getStartPageUrl())) { NavigateAction openStartPage = new NavigateAction.Builder() .page(getBrowser().getEndpointConfiguration().getStartPageUrl()) .build(); openStartPage.execute(browser, context); } context.setVariable(SeleniumHeaders.SELENIUM_BROWSER, browser.getName()); } StartBrowserAction(Builder builder); boolean isAllowAlreadyStarted();  }
@Test public void testStart() throws Exception { when(seleniumBrowser.isStarted()).thenReturn(false); StartBrowserAction action = new StartBrowserAction.Builder() .browser(seleniumBrowser) .build(); action.execute(seleniumBrowser, context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_BROWSER), "ChromeBrowser"); verify(seleniumBrowser).start(); }
@Test public void testStartWithStartPage() throws Exception { when(seleniumBrowser.isStarted()).thenReturn(false); when(seleniumBrowserConfiguration.getStartPageUrl()).thenReturn("http: doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Assert.assertEquals(invocation.getArguments()[0].toString(), "http: return null; } }).when(navigation).to(any(URL.class)); StartBrowserAction action = new StartBrowserAction.Builder() .browser(seleniumBrowser) .build(); action.execute(seleniumBrowser, context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_BROWSER), "ChromeBrowser"); verify(seleniumBrowser).start(); verify(navigation).to(any(URL.class)); }
@Test public void testStartAlreadyStarted() throws Exception { when(seleniumBrowser.isStarted()).thenReturn(true); StartBrowserAction action = new StartBrowserAction.Builder() .browser(seleniumBrowser) .build(); action.execute(seleniumBrowser, context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_BROWSER), "ChromeBrowser"); verify(seleniumBrowser, times(0)).stop(); verify(seleniumBrowser, times(0)).start(); }
@Test public void testStartAlreadyStartedNotAllowed() throws Exception { when(seleniumBrowser.isStarted()).thenReturn(true); StartBrowserAction action = new StartBrowserAction.Builder() .browser(seleniumBrowser) .allowAlreadyStarted(false) .build(); action.execute(seleniumBrowser, context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_BROWSER), "ChromeBrowser"); verify(seleniumBrowser).stop(); verify(seleniumBrowser).start(); }