code
stringlengths
73
34.1k
label
stringclasses
1 value
protected static void setAppURL(Selenified clazz, ITestContext context, String siteURL) { context.setAttribute(clazz.getClass().getName() + APP_URL, siteURL); }
java
private String getVersion(String clazz, ITestContext context) { return (String) context.getAttribute(clazz + "Version"); }
java
protected static void setVersion(Selenified clazz, ITestContext context, String version) { context.setAttribute(clazz.getClass().getName() + "Version", version); }
java
private String getAuthor(String clazz, ITestContext context) { return (String) context.getAttribute(clazz + "Author"); }
java
protected static void setAuthor(Selenified clazz, ITestContext context, String author) { context.setAttribute(clazz.getClass().getName() + "Author", author); }
java
private static Map<String, Object> getExtraHeaders(String clazz, ITestContext context) { return (Map<String, Object>) context.getAttribute(clazz + "Headers"); }
java
protected static void addAdditionalDesiredCapabilities(Selenified clazz, ITestContext context, String capabilityName, Object capabilityValue) { DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); if (context.getAttributeNames().contains(clazz.getClass().getName() + DESIRED_CAPABILITIES)...
java
private static DesiredCapabilities getAdditionalDesiredCapabilities(String clazz, ITestContext context) { return (DesiredCapabilities) context.getAttribute(clazz + DESIRED_CAPABILITIES); }
java
private static String getServiceUserCredential(String clazz, ITestContext context) { if (System.getenv("SERVICES_USER") != null) { return System.getenv("SERVICES_USER"); } if (context.getAttribute(clazz + SERVICES_USER) != null) { return (String) context.getAttribute(claz...
java
private static String getServicePassCredential(String clazz, ITestContext context) { if (System.getenv("SERVICES_PASS") != null) { return System.getenv("SERVICES_PASS"); } if (context.getAttribute(clazz + SERVICES_PASS) != null) { return (String) context.getAttribute(claz...
java
private void loadInitialPage(App app, String url, Reporter reporter) { String startingPage = "The initial url of <i>"; String act = "Opening new browser and loading up initial url"; String expected = startingPage + url + "</i> will successfully load"; if (app != null) { try ...
java
protected void finish() { Reporter reporter = this.reporterThreadLocal.get(); assertEquals("Detailed results found at: " + reporter.getFileName(), "0 errors", Integer.toString(reporter.getFails()) + ERRORS_CHECK); }
java
private void setupScreenSize(App app) { String screensize = app.getBrowser().getScreensize(); if (screensize != null) { if (screensize.matches("(\\d+)x(\\d+)")) { int width = Integer.parseInt(screensize.split("x")[0]); int height = Integer.parseInt(screensize....
java
private void init(WebDriver driver, Reporter reporter) { this.driver = driver; this.reporter = reporter; App app = null; if (reporter != null) { app = reporter.getApp(); } is = new Is(this); get = new Get(app, driver, this); verifyState = new...
java
private String prettyOutputStart(String initialString) { initialString += Reporter.ordinal(match + 1) + " element with <i>" + type.toString() + "</i> of <i>" + locator + "</i>"; if (parent != null) { initialString = parent.prettyOutputStart(initialString + " and parent of "); } ...
java
public By defineByElement() { // consider adding strengthening By byElement = null; switch (type) { // determine which locator type we are interested in case XPATH: byElement = By.xpath(locator); break; case ID: byElement = ...
java
public WebElement getWebElement() { List<WebElement> elements = getWebElements(); if (elements.size() > match) { return elements.get(match); } String reason = this.prettyOutputStart() + " was not located on the page"; if( !elements.isEmpty() ) { reason += ...
java
public List<WebElement> getWebElements() { if (parent != null) { return parent.getWebElement().findElements(defineByElement()); } return driver.findElements(defineByElement()); }
java
public Element findChild(Element child) { return new Element(child.getDriver(), reporter, child.getType(), child.getLocator(), child.getMatch(), this); }
java
private boolean isNotInput(String action, String expected, String extra) { // wait for element to be displayed if (!is.input()) { reporter.fail(action, expected, extra + prettyOutput() + NOT_AN_INPUT); // indicates element not an input return true; } r...
java
private boolean isSelect(String action, String expected) { // wait for element to be displayed if (!is.select()) { reporter.fail(action, expected, Element.CANT_SELECT + prettyOutput() + NOT_A_SELECT); // indicates element not an input return false; } r...
java
private boolean isNotPresentDisplayedEnabled(String action, String expected, String extra) { // wait for element to be present if (isNotPresent(action, expected, extra)) { return true; } // wait for element to be displayed if (isNotDisplayed(action, expected, extra)) ...
java
private boolean isNotPresentEnabledInput(String action, String expected) { // wait for element to be present if (isNotPresent(action, expected, Element.CANT_TYPE)) { return true; } // wait for element to be enabled return isNotEnabled(action, expected, Element.CANT_TY...
java
private boolean isNotPresentDisplayedEnabledInput(String action, String expected, String extra) { // wait for element to be present if (isNotPresent(action, expected, extra)) { return true; } // wait for element to be displayed if (isNotDisplayed(action, expected, ext...
java
private boolean isNotPresentDisplayedEnabledSelect(String action, String expected) { // wait for element to be present if (isNotPresent(action, expected, Element.CANT_SELECT)) { return true; } // wait for element to be displayed if (isNotDisplayed(action, expected, El...
java
public void click() { String cantClick = "Unable to click "; String action = "Clicking " + prettyOutput(); String expected = prettyOutputStart() + " is present, displayed, and enabled to be clicked"; try { if (isNotPresentDisplayedEnabled(action, expected, cantClick)) { ...
java
public void hover() { String cantHover = "Unable to hover over "; String action = "Hovering over " + prettyOutput(); String expected = prettyOutputStart() + " is present, and displayed to be hovered over"; try { // wait for element to be present if (isNotPresent(a...
java
public void focus() { String cantFocus = "Unable to focus on "; String action = "Focusing on " + prettyOutput(); String expected = prettyOutputStart() + " is present, displayed, and enabled to be focused"; try { if (isNotPresentDisplayedEnabledInput(action, expected, cantFocu...
java
public void type(String text) { String action = "Typing text '" + text + IN + prettyOutput(); String expected = prettyOutputStart() + " is present, displayed, and enabled to have text " + text + " typed in"; boolean warning = false; try { if (isNotPresentEnabledInput(action, ...
java
public void clear() { String cantClear = "Unable to clear "; String action = "Clearing text in " + prettyOutput(); String expected = prettyOutputStart() + " is present, displayed, and enabled to have text cleared"; try { if (isNotPresentDisplayedEnabledInput(action, expected,...
java
public void select(int index) { String action = SELECTING + index + " in " + prettyOutput(); String expected = prettyOutputStart() + PRESENT_DISPLAYED_AND_ENABLED + index + SELECTED; try { if (isNotPresentDisplayedEnabledSelect(action, expected)) { return; ...
java
public void selectOption(String option) { String action = SELECTING + option + " in " + prettyOutput(); String expected = prettyOutputStart() + PRESENT_DISPLAYED_AND_ENABLED + option + SELECTED; try { if (isNotPresentDisplayedEnabledSelect(action, expected)) { return;...
java
public void selectValue(String value) { String action = SELECTING + value + " in " + prettyOutput(); String expected = prettyOutputStart() + PRESENT_DISPLAYED_AND_ENABLED + value + SELECTED; try { if (isNotPresentDisplayedEnabledSelect(action, expected)) { return; ...
java
private void isScrolledTo(String action, String expected) { WebElement webElement = getWebElement(); long elementPosition = webElement.getLocation().getY(); JavascriptExecutor js = (JavascriptExecutor) driver; int scrollHeight = ((Number) js.executeScript("return document.documentElement...
java
public void scrollTo() { String action = "Scrolling screen to " + prettyOutput(); String expected = prettyOutputStart() + " is now within the current viewport"; try { // wait for element to be present if (isNotPresent(action, expected, CANT_SCROLL)) { retu...
java
public void scrollTo(long position) { String action = "Scrolling screen to " + position + " pixels above " + prettyOutput(); String expected = prettyOutputStart() + " is now within the current viewport"; try { // wait for element to be present if (isNotPresent(action, exp...
java
public void draw(List<Point<Integer, Integer>> points) { if (points.isEmpty()) { reporter.fail("Drawing object in " + prettyOutput(), "Drew object in " + prettyOutput(), "Unable to draw in " + prettyOutput() + " as no points were supplied"); return; } ...
java
public void selectFrame() { String cantSelect = "Unable to focus on frame "; String action = "Focusing on frame " + prettyOutput(); String expected = "Frame " + prettyOutput() + " is present, displayed, and focused"; try { // wait for element to be present if (isN...
java
private String getScreenshot() { WebElement webElement = getWebElement(); String imageLink = "<b><font class='fail'>No Image Preview</font></b>"; // capture an image of it try { imageLink = reporter.captureEntirePageScreenshot(); File image = new File(reporter.get...
java
public String selectedOption() { if (isNotPresentSelect()) { return null; } WebElement webElement = element.getWebElement(); Select dropdown = new Select(webElement); WebElement option = dropdown.getFirstSelectedOption(); return option.getText(); }
java
public String selectedValue() { if (isNotPresentSelect()) { return null; } WebElement webElement = element.getWebElement(); Select dropdown = new Select(webElement); WebElement option = dropdown.getFirstSelectedOption(); return option.getAttribute(VALUE); ...
java
@SuppressWarnings("squid:S1168") public String[] selectedValues() { if (isNotPresentSelect()) { return null; // returning an empty array could be confused with no values selected } WebElement webElement = element.getWebElement(); Select dropdown = new Select(webElement...
java
public String text() { if (!element.is().present()) { return null; } WebElement webElement = element.getWebElement(); return webElement.getText(); }
java
public String value() { if (!element.is().present() || !element.is().input()) { return null; } WebElement webElement = element.getWebElement(); return webElement.getAttribute(VALUE); }
java
public String css(String attribute) { if (!element.is().present()) { return null; } try { WebElement webElement = element.getWebElement(); return webElement.getCssValue(attribute); } catch (NoSuchMethodError | Exception e) { log.warn(e); ...
java
@SuppressWarnings("unchecked") public Map<String, String> allAttributes() { if (!element.is().present()) { return null; } try { WebElement webElement = element.getWebElement(); JavascriptExecutor js = (JavascriptExecutor) driver; return (Map<St...
java
public Object eval(String javascriptFunction) { if (!element.is().present()) { return null; } try { WebElement webElement = element.getWebElement(); JavascriptExecutor js = (JavascriptExecutor) driver; return js.executeScript(javascriptFunction, we...
java
public int numOfSelectOptions() { if (isNotPresentSelect()) { return -1; } WebElement webElement = element.getWebElement(); Select dropdown = new Select(webElement); List<WebElement> options = dropdown.getOptions(); return options.size(); }
java
@SuppressWarnings("squid:S1168") public String[] selectOptions() { if (isNotPresentSelect()) { return null; // returning an empty array could be confused with no options available } WebElement webElement = element.getWebElement(); Select dropdown = new Select(webElemen...
java
@SuppressWarnings("squid:S1168") public Element tableRows() { if (!element.is().present()) { return null; // returning an empty array could be confused with no rows } if (!element.is().table()) { return null; // returning an empty array could be confused with no...
java
public int numOfTableColumns() { Element rows = tableRows(); if (rows == null) { return -1; } Element thCells = rows.findChild(app.newElement(Locator.TAGNAME, "th")); Element tdCells = rows.findChild(app.newElement(Locator.TAGNAME, "td")); return thCells.get()...
java
@SuppressWarnings("squid:S1168") public Element tableRow(int rowNum) { Element rows = tableRows(); if (rows == null) { return null; } if (numOfTableRows() < rowNum) { return null; } return rows.get(rowNum); }
java
public Element tableCell(int rowNum, int colNum) { Element row = tableRow(rowNum); if (row == null || numOfTableColumns() < colNum) { return null; } Element thCells = row.findChild(app.newElement(Locator.TAGNAME, "th")); Element tdCells = row.findChild(app.newElement(...
java
public Map<String, Object> getHeaders() { Map<String, Object> map = new HashMap<>(); map.put("Content-length", "0"); map.put(CONTENT_TYPE, contentType); map.put("Accept", "application/json"); for (Map.Entry<String, Object> entry : extraHeaders.entrySet()) { map.put(en...
java
public Response get(String service, Request request) throws IOException { return call(Method.GET, service, request, null); }
java
public Response post(String service, Request request, File file) throws IOException { if (file != null) { this.contentType = MULTIPART + BOUNDARY; } return call(Method.POST, service, request, file); }
java
public Response put(String service, Request request, File file) throws IOException { if (file != null) { this.contentType = MULTIPART + BOUNDARY; } return call(Method.PUT, service, request, file); }
java
public Response delete(String service, Request request, File file) throws IOException { if (file != null) { this.contentType = MULTIPART + BOUNDARY; } return call(Method.DELETE, service, request, file); }
java
public String getRequestParams(Request request) { StringBuilder params = new StringBuilder(); if (request != null && request.getUrlParams() != null) { params.append("?"); for (String key : request.getUrlParams().keySet()) { params.append(key); para...
java
private void setupHeaders(HttpURLConnection connection) { for (Map.Entry<String, Object> entry : getHeaders().entrySet()) { connection.setRequestProperty(entry.getKey(), String.valueOf(entry.getValue())); } connection.setDoOutput(true); connection.setDoInput(true); co...
java
private Response call(Method method, String service, Request request, File file) throws IOException { URL url = new URL(this.serviceBaseUrl + service + getRequestParams(request)); HttpURLConnection connection = getConnection(url); connection.setRequestMethod(method.toString()); setupHead...
java
private HttpURLConnection getConnection(URL url) throws IOException { Proxy proxy = Proxy.NO_PROXY; if (Property.isProxySet()) { SocketAddress addr = new InetSocketAddress(Property.getProxyHost(), Property.getProxyPort()); proxy = new Proxy(Proxy.Type.HTTP, addr); } ...
java
@SuppressWarnings({"squid:S3776", "squid:S2093"}) private Response getResponse(HttpURLConnection connection) { int status; Map headers; try { status = connection.getResponseCode(); headers = connection.getHeaderFields(); } catch (IOException e) { l...
java
@SuppressWarnings("rawtypes") @Override public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { annotation.setInvocationCount(StringUtils.countMatches(getBrowser(), ",") + 1); }
java
private boolean isRealBrowser() { Browser browser = capabilities.getBrowser(); return browser.getName() != BrowserName.NONE && browser.getName() != BrowserName.HTMLUNIT; }
java
private String generateFilename() { String counter = ""; if (capabilities.getInstance() > 0) { counter = "_" + capabilities.getInstance(); } return test + counter; }
java
private void setupFile() { if (!new File(directory).exists() && !new File(directory).mkdirs()) { try { throw new IOException("Unable to create output directory"); } catch (IOException e) { log.info(e); } } if (!file.exists()) { ...
java
private void replaceInFile(String oldText, String newText) { StringBuilder oldContent = new StringBuilder(); try (FileReader fr = new FileReader(file); BufferedReader reader = new BufferedReader(fr)) { String line; while ((line = reader.readLine()) != null) { old...
java
private String getHtmlForPDFConversion() throws IOException { StringBuilder oldContent = new StringBuilder(); FileReader fr = new FileReader(file); try (BufferedReader reader = new BufferedReader(fr)) { String line; while ((line = reader.readLine()) != null) { ...
java
public String captureEntirePageScreenshot() { String imageName = generateImageName(); String imageLink = generateImageLink(imageName); try { app.takeScreenshot(imageName); screenshots.add(imageName); } catch (Exception e) { log.error(e); im...
java
private String getAction(String check, double waitFor) { String action = ""; if (waitFor > 0) { action = "Waiting up to " + waitFor + " seconds " + check; } return action; }
java
private String getActual(String actual, double timeTook) { if (timeTook > 0) { String lowercase = actual.substring(0, 1).toLowerCase(); actual = "After waiting for " + timeTook + " seconds, " + lowercase + actual.substring(1); } return actual; }
java
public void pass(String action, String expectedResult, String actualResult) { passes++; recordStep(action, expectedResult, actualResult, false, Success.PASS); }
java
public void finalizeReporter(int testStatus) { // reopen the file try (FileWriter fw = new FileWriter(file, true); BufferedWriter out = new BufferedWriter(fw)) { out.write(" </table>\n"); out.write(" </body>\n"); out.write("</html>\n"); } catch (IOException e...
java
private void addTimeToReport() { // record the time SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss"); String timeNow = stf.format(new Date()); long totalTime = (new Date()).getTime() - startTime; long time = totalTime / 1000; StringBuilder seconds = new StringBuild...
java
private void generatePdf() { File pdfFile = new File(directory, filename + ".pdf"); try (OutputStream os = new FileOutputStream(pdfFile)) { PdfRendererBuilder builder = new PdfRendererBuilder(); builder.withHtmlContent(getHtmlForPDFConversion(), "file://" + pdfFile.getAbsolutePat...
java
private String generateImageLink(String imageName) { StringBuilder imageLink = new StringBuilder("<br/>"); if (imageName.length() >= directory.length() + 1) { imageLink.append(ONCLICK_TOGGLE). append(imageName.substring(directory.length() + 1)). append...
java
public static String formatResponse(Response response) { if (response == null) { return ""; } StringBuilder output = new StringBuilder(); if (response.isData()) { output.append(DIV_I); Gson gson = new GsonBuilder().setPrettyPrinting().create(); ...
java
public static String formatKeyPair(Map<String, Object> keyPairs) { if (keyPairs == null) { return ""; } StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry<String, Object> entry : keyPairs.entrySet()) { stringBuilder.append(DIV); stringBu...
java
public static String getRequestPayloadOutput(Request params, File file) { StringBuilder payload = new StringBuilder(); String uuid = getUUID(); payload.append(ONCLICK_TOGGLE).append(uuid).append("\")'>Toggle Payload</a> "); payload.append(SPAN_ID).append(uuid).append(DISPLAY_NONE); ...
java
public static String getCredentialStringOutput(HTTP http) { if (http == null || !http.useCredentials()) { return ""; } StringBuilder credentials = new StringBuilder(); String uuid = getUUID(); credentials.append(ONCLICK_TOGGLE).append(uuid).append("\")'>Toggle Credent...
java
public static String getRequestHeadersOutput(HTTP http) { if (http == null) { return ""; } StringBuilder requestHeaders = new StringBuilder(); String uuid = getUUID(); requestHeaders.append(ONCLICK_TOGGLE).append(uuid).append("\")'>Toggle Headers</a> "); reque...
java
public static String getResponseHeadersOutput(Response response) { if (response == null) { return ""; } StringBuilder responseHeaders = new StringBuilder(); String uuid = getUUID(); responseHeaders.append(ONCLICK_TOGGLE).append(uuid).append("\")'>Toggle Headers</a> ")...
java
public static String getResponseCodeOutput(Response response) { if (response == null) { return ""; } StringBuilder responseOutput = new StringBuilder(); String uuid = getUUID(); responseOutput.append(ONCLICK_TOGGLE).append(uuid).append("\")'>Toggle Response Status Cod...
java
public static String getResponseOutput(Response response) { if (response == null || response.getMessage() == null || "".equals(response.getMessage())) { return ""; } StringBuilder responseOutput = new StringBuilder(); String uuid = getUUID(); responseOutput.append(ONC...
java
public static String getUUID() { long timeInSeconds = new Date().getTime(); String randomChars = TestCase.getRandomString(10); return timeInSeconds + "_" + randomChars; }
java
public String url() { try { return driver.getCurrentUrl(); } catch (Exception e) { log.warn(e); return null; } }
java
public String title() { try { return driver.getTitle(); } catch (Exception e) { log.warn(e); return null; } }
java
public String htmlSource() { try { return driver.getPageSource(); } catch (Exception e) { log.warn(e); return null; } }
java
public Object eval(String javascriptFunction) { try { JavascriptExecutor js = (JavascriptExecutor) driver; return js.executeScript(javascriptFunction); } catch (NoSuchMethodError | Exception e) { log.warn(e); return null; } }
java
public String confirmation() { if (!is.confirmationPresent()) { return null; } try { Alert alert = driver.switchTo().alert(); return alert.getText(); } catch (Exception e) { log.warn(e); return null; } }
java
public Cookie cookie(String expectedCookieName) { try { return driver.manage().getCookieNamed(expectedCookieName); } catch (Exception e) { log.warn(e); return null; } }
java
public String cookieValue(String expectedCookieName) { Cookie cookie = cookie(expectedCookieName); if (cookie != null) { return cookie.getValue(); } return null; }
java
public String cookiePath(String expectedCookieName) { Cookie cookie = cookie(expectedCookieName); if (cookie != null) { return cookie.getPath(); } return null; }
java
public String cookieDomain(String expectedCookieName) { Cookie cookie = cookie(expectedCookieName); if (cookie != null) { return cookie.getDomain(); } return null; }
java
public Date cookieExpiration(String expectedCookieName) { Cookie cookie = cookie(expectedCookieName); if (cookie != null) { return cookie.getExpiry(); } return null; }
java
public void urlEquals(double seconds, String expectedURL) { double end = System.currentTimeMillis() + (seconds * 1000); try { WebDriverWait wait = new WebDriverWait(app.getDriver(), (long) seconds, DEFAULT_POLLING_INTERVAL); wait.until(ExpectedConditions.urlToBe(expectedURL)); ...
java
private double popup(double seconds) { // wait for up to XX seconds for the error message double end = System.currentTimeMillis() + (seconds * 1000); WebDriverWait wait = new WebDriverWait(app.getDriver(), (long) seconds, DEFAULT_POLLING_INTERVAL); wait.until(ExpectedConditions.alertIsPr...
java
public void alertPresent(double seconds) { try { double timeTook = popup(seconds); checkAlertPresent(seconds, timeTook); } catch (TimeoutException e) { checkAlertPresent(seconds, seconds); } }
java
public void alertNotPresent(double seconds) { try { double timeTook = noPopup(seconds); checkAlertNotPresent(seconds, timeTook); } catch (TimeoutException e) { checkAlertNotPresent(seconds, seconds); } }
java