private Config config() { return driver().config(); }
public File download(Driver driver, String relativeOrAbsoluteUrl, long timeout) throws IOException { String url = makeAbsoluteUrl(driver.config(), relativeOrAbsoluteUrl); HttpResponse response = executeHttpRequest(driver, url, timeout); if (response.getStatusLine().getStatusCode() >= 500) { throw new RuntimeException("Failed to download file " + url + ": " + response.getStatusLine()); } if (response.getStatusLine().getStatusCode() >= 400) { throw new FileNotFoundException("Failed to download file " + url + ": " + response.getStatusLine()); } String fileName = getFileName(url, response); File downloadedFile = downloader.prepareTargetFile(driver.config(), fileName); return saveFileContent(response, downloadedFile); }
protected void click(Driver driver, WebElement element) { if (driver.config().clickViaJs()) { driver.executeJavaScript("arguments[0].click()", element); } else { element.click(); } }
public WebElement findElement(Driver driver, SearchContext context, By selector) { if (driver.config().selectorMode() == CSS || !(selector instanceof ByCssSelector)) { return context.findElement(selector); } List<WebElement> webElements = evaluateSizzleSelector(driver, context, (ByCssSelector) selector); return webElements.isEmpty() ? null : webElements.get(0); }
private File writeToFile(Driver driver, BufferedImage dest) throws IOException { if (dest == null) { return null; } File screenshotOfElement = new File(driver.config().reportsFolder(), generateScreenshotFileName() + ".png"); ensureFolderExists(screenshotOfElement); ImageIO.write(dest, "png", screenshotOfElement); return screenshotOfElement; }
default SelenideTargetLocator switchTo() { return new SelenideTargetLocator(config(), getWebDriver()); }
/** * Takes screenshot of current browser window. * Stores 2 files: html of page (if "savePageSource" option is enabled), and (if possible) image in PNG format. * * @param fileName name of file (without extension) to store screenshot to. * @return the name of last saved screenshot or null if failed to create screenshot */ public String takeScreenShot(Driver driver, String fileName) { return ifWebDriverStarted(driver, webDriver -> ifReportsFolderNotNull(driver.config(), config -> takeScreenShot(config, webDriver, fileName))); }
/** * For example: * {@code $$(".error").shouldHave(size(3))} * {@code $$(".error").shouldHave(texts("Error1", "Error2"))} */ public ElementsCollection shouldHave(CollectionCondition... conditions) { return should("have", driver().config().timeout(), conditions); }
public BufferedImage takeScreenshotAsImage(Driver driver, WebElement element) { return ifWebDriverStarted(driver, webdriver -> ifReportsFolderNotNull(driver.config(), config -> takeScreenshotAsImage(webdriver, element))); }
public List<WebElement> findElements(Driver driver, SearchContext context, By selector) { if (driver.config().selectorMode() == CSS || !(selector instanceof ByCssSelector)) { return context.findElements(selector); } return evaluateSizzleSelector(driver, context, (ByCssSelector) selector); }
/** * For example: {@code $$(".error").shouldBe(empty)} */ public ElementsCollection shouldBe(CollectionCondition... conditions) { return should("be", driver().config().timeout(), conditions); }
public static String screenshot(Driver driver) { return screenshot(driver.config(), ScreenShotLaboratory.getInstance().formatScreenShotPath(driver)); }
private void setValueForTextInput(Driver driver, WebElement element, String text) { if (text == null || text.isEmpty()) { element.clear(); } else if (driver.config().fastSetValue()) { String error = setValueByJs(driver, element, text); if (error != null) throw new InvalidStateException(driver, error); else { events.fireEvent(driver, element, "keydown", "keypress", "input", "keyup", "change"); } } else { element.clear(); element.sendKeys(text); } }
@Override public WebElement execute(SelenideElement proxy, WebElementSource locator, Object[] args) { String text = (String) args[0]; WebElement element = locator.findAndAssertElementIsInteractable(); if (locator.driver().config().versatileSetValue() && "select".equalsIgnoreCase(element.getTagName())) { selectOptionByValue.execute(proxy, locator, args); return proxy; } if (locator.driver().config().versatileSetValue() && "input".equalsIgnoreCase(element.getTagName()) && "radio".equals(element.getAttribute("type"))) { selectRadio.execute(proxy, locator, args); return proxy; } setValueForTextInput(locator.driver(), element, text); return proxy; }
protected String uiDetails() { return screenshot(driver.config(), screenshot) + timeout(timeoutMs) + causedBy(getCause()); }
@Override public File execute(SelenideElement proxy, WebElementSource linkWithHref, Object[] args) throws IOException { WebElement link = linkWithHref.findAndAssertElementIsInteractable(); Config config = linkWithHref.driver().config(); long timeout = getTimeout(config, args); if (config.fileDownload() == HTTPGET) { LOG.config("selenide.fileDownload = " + System.getProperty("selenide.fileDownload") + " download file via http get"); return downloadFileWithHttpRequest.download(linkWithHref.driver(), link, timeout); } if (!config.proxyEnabled()) { throw new IllegalStateException("Cannot download file: proxy server is not enabled. Setup proxyEnabled"); } if (linkWithHref.driver().getProxy() == null) { throw new IllegalStateException("Cannot download file: proxy server is not started"); } return downloadFileWithProxyServer.download(linkWithHref, link, linkWithHref.driver().getProxy(), timeout); }
@Override public File execute(SelenideElement proxy, WebElementSource locator, Object[] args) throws IOException { File[] file; if (args instanceof File[]) { file = (File[]) args; } else { file = (File[]) args[0]; } if (file.length == 0) { throw new IllegalArgumentException("No files to upload"); } WebElement inputField = locator.getWebElement(); File uploadedFile = uploadFile(locator.driver(), inputField, file[0]); if (file.length > 1) { SelenideElement form = proxy.closest("form"); List<WebElement> newInputs = cloneInputField(locator.driver(), form, inputField, file.length - 1); Config config = locator.driver().config(); Stopwatch stopwatch = new Stopwatch(config.timeout()); for (int i = 1; i < file.length; i++) { WebElement newInput = newInputs.get(i - 1); uploadSingleFile(config, file[i], stopwatch, newInput); } } return uploadedFile; }
protected ElementsCollection should(String prefix, long timeoutMs, CollectionCondition... conditions) { validateAssertionMode(driver().config()); SelenideLog log = SelenideLogger.beginStep(collection.description(), "should " + prefix, (Object[]) conditions); try { for (CollectionCondition condition : conditions) { waitUntil(condition, timeoutMs); } SelenideLogger.commitStep(log, PASS); return this; } catch (Error error) { Error wrappedError = UIAssertionError.wrap(driver(), error, timeoutMs); SelenideLogger.commitStep(log, wrappedError); switch (driver().config().assertionMode()) { case SOFT: return this; default: throw wrappedError; } } catch (RuntimeException e) { SelenideLogger.commitStep(log, e); throw e; } }
protected void click(Driver driver, WebElement element, int offsetX, int offsetY) { if (driver.config().clickViaJs()) { driver.executeJavaScript("arguments[0].dispatchEvent(new MouseEvent('click', {" + "'view': window," + "'bubbles': true," + "'cancelable': true," + "'clientX': arguments[0].getClientRects()[0].left + arguments[1]," + "'clientY': arguments[0].getClientRects()[0].top + arguments[2]" + "}))", element, offsetX, offsetY); } else { driver.actions() .moveToElement(element, offsetX, offsetY) .click() .build() .perform(); } } }
private File clickAndInterceptFileByProxyServer(WebElementSource anyClickableElement, WebElement clickable, SelenideProxyServer proxyServer, long timeout) throws FileNotFoundException { Config config = anyClickableElement.driver().config(); WebDriver webDriver = anyClickableElement.driver().getWebDriver(); String currentWindowHandle = webDriver.getWindowHandle(); Set<String> currentWindows = webDriver.getWindowHandles(); FileDownloadFilter filter = proxyServer.responseFilter("download"); filter.activate(); try { clickable.click(); waiter.wait(filter, new HasDownloads(), timeout, config.pollingInterval()); return firstDownloadedFile(anyClickableElement, filter, timeout); } finally { filter.deactivate(); closeNewWindows(webDriver, currentWindowHandle, currentWindows); } }