/** * 关闭浏览器引擎 */ public void close() { if(driver != null) { Options manage = driver.manage(); boolean cookieSave = Boolean.parseBoolean(enginePro.getProperty("cookie.save", "false")); File root = PathUtil.getRootDir(); File cookieFile = new File(root, enginePro.getProperty("cookie.save.path", "phoenix.autotest.cookie")); if(cookieSave) { Set<Cookie> cookies = manage.getCookies(); try(ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(cookieFile))) { output.writeObject(cookies); } catch (IOException e) { logger.error("", e); } } driver.quit(); } }
@SuppressWarnings("unchecked") @Override protected void prepareDriver() throws Exception { System.clearProperty("test.selenium.remote"); localDriverClass = WebDriverMock.class.getName(); remoteDriverClass = WebDriverMock.class.getName(); scenario = "sc"; super.prepareDriver(); mockDriver = Mockito.mock(WebDriverMock.class); Mockito.when(mockDriver.getScreenshotAs(ArgumentMatchers.any(OutputType.class))) .thenReturn(new File(Thread.currentThread().getContextClassLoader().getResource("log4j2.json").toURI())); final Options options = Mockito.mock(Options.class); Mockito.when(options.window()).thenReturn(Mockito.mock(Window.class)); Mockito.when(mockDriver.manage()).thenReturn(options); final WebElement webElement = Mockito.mock(WebElement.class); Mockito.when(mockDriver.findElement(ArgumentMatchers.any(By.class))).thenReturn(webElement); Mockito.when(webElement.isDisplayed()).thenReturn(true); this.driver = mockDriver; }
@SuppressWarnings("unchecked") @Override protected void prepareDriver() throws Exception { testName = Mockito.mock(TestName.class); Mockito.when(testName.getMethodName()).thenReturn("mockTest"); System.clearProperty("test.selenium.remote"); localDriverClass = WebDriverMock.class.getName(); remoteDriverClass = WebDriverMock.class.getName(); scenario = "sc"; super.prepareDriver(); mockDriver = Mockito.mock(WebDriverMock.class); Mockito.when(((WebDriverMock) mockDriver).getScreenshotAs(ArgumentMatchers.any(OutputType.class))).thenReturn( new File(Thread.currentThread().getContextClassLoader().getResource("log4j2.json").toURI())); final Options options = Mockito.mock(Options.class); Mockito.when(options.window()).thenReturn(Mockito.mock(Window.class)); Mockito.when(mockDriver.manage()).thenReturn(options); final WebElement webElement = Mockito.mock(WebElement.class); Mockito.when(mockDriver.findElement(ArgumentMatchers.any(By.class))).thenReturn(webElement); Mockito.when(webElement.isDisplayed()).thenReturn(true); this.driver = mockDriver; }
@SuppressWarnings("unchecked") @Override protected void prepareDriver() throws Exception { System.clearProperty("test.selenium.remote"); localDriverClass = WebDriverMock.class.getName(); remoteDriverClass = WebDriverMock.class.getName(); scenario = "sc"; super.prepareDriver(); mockDriver = Mockito.mock(WebDriverMock.class); Mockito.when(mockDriver.getScreenshotAs(ArgumentMatchers.any(OutputType.class))).thenReturn( new File(Thread.currentThread().getContextClassLoader().getResource("log4j2.json").toURI())); final Options options = Mockito.mock(Options.class); Mockito.when(options.window()).thenReturn(Mockito.mock(Window.class)); Mockito.when(mockDriver.manage()).thenReturn(options); final WebElement webElement = Mockito.mock(WebElement.class); Mockito.when(mockDriver.findElement(ArgumentMatchers.any(By.class))).thenReturn(webElement); Mockito.when(webElement.isDisplayed()).thenReturn(true); this.driver = mockDriver; }
/** * 获取验证码 * @param engine 引擎 * @param param 例如:data,http://localhost:8080/G2/captcha!getLastCode.do * @return 验证码 */ public static String execute(SeleniumEngine engine, String param) { WebDriver driver = engine.getDriver(); Options manage = driver.manage(); String[] paramArray = param.split(",", 2); if(paramArray.length != 2) { throw new RuntimeException("Param format is error, should be 'data,url'"); } String key = paramArray[0]; String url = paramArray[1]; Set<Cookie> cookies = manage.getCookies(); List<AtCookie> atCookieList = new ArrayList<AtCookie>(); for(Cookie cookie : cookies) { String name = cookie.getName(); String value = cookie.getValue(); AtCookie atCookie = new AtCookie(); atCookie.setName(name); atCookie.setValue(value); atCookie.setPath(cookie.getPath()); atCookie.setDomain(cookie.getDomain()); atCookieList.add(atCookie); } String code = HttpApiUtil.getJsonValue(url, atCookieList, key); return code; }
private Window mockWindow() { Options options = mock(Options.class); doReturn(options).when(webDriver).manage(); Window window = mock(Window.class); doReturn(window).when(options).window(); return window; }
/** * Instantiates a new base ate test. */ public BaseATETest() { super(); mockedDriver = ateMock(WebDriver.class); myMockedDriver = ateMock(IMyWebDriver.class); options = ateMock(Options.class); TestProjectRunner.registerXsdNameSpaceParsers(); TestProjectRunner.registerProblemHandlers(); }
@Before public void initData() { dataInitService.clear(); dataInitService.init(); webDriver = new PhantomJSDriver(); final Options options = webDriver.manage(); final Window window = options.window(); window.setSize(new Dimension(1280, 800)); }
@Test public void shouldFindExistingCookie() { WebDriver driver = mock(WebDriver.class); Options options = mock(Options.class); Cookie cookie = new Cookie(cookieName, "oki-doki"); when(driver.manage()).thenReturn(options); when(options.getCookieNamed(eq(cookieName))).thenReturn(cookie); assertThat("Cookie not found!", driver, hasCookie(cookieName)); }
@Test public void shouldNotFindUnexistingCookie() { final WebDriver driver = mock(WebDriver.class); Options options = mock(Options.class); when(driver.manage()).thenReturn(options); when(options.getCookieNamed(eq(cookieName))).thenReturn(null); assertThat("Cook should not be found", driver, not(hasCookie(cookieName))); }
@Test public void testWaitFixedTime() { final Options options = mockDriver.manage(); Mockito.when(options.timeouts()).thenReturn(Mockito.mock(Timeouts.class)); waitFixedTime(); }
/** * Override this method to change {@link WebDriver} options before the test is run. */ @Override public void manage( Options options ) { options.window().setSize( new Dimension( 640, 480 ) ); }
/** * @return the options */ public Options getOptions() { return options; }
public static WebDriver implicitWait(WebDriver webDriver) { Options webDriverOptions = webDriver.manage(); Timeouts timeouts = webDriverOptions.timeouts(); timeouts.implicitlyWait(30, TimeUnit.SECONDS); return webDriver; }
/** * This life-cycle callback gives you a chance to modify {@link WebDriver} {@link Options} before * the test starts. * * @param options {@link WebDriver} options. */ default void manage( Options options ) {}
/** * Gets the Option interface * * @return An option interface * @see Options */ public Options manage() { return seleniumWebDriver().manage(); }
/** * Gets the web driver manage. * * @return the web driver manage */ public Options getWebDriverManage() { return getOptions(); }