Search in sources :

Example 6 with RuntimeException

use of java.lang.RuntimeException in project selenium_java by sergueik.

the class AppTest method findElements.

private List<WebElement> findElements(String selectorKind, String selectorValue, WebElement parent) {
    SearchContext finder;
    String parent_css_selector = null;
    String parent_xpath = null;
    List<WebElement> elements = null;
    Hashtable<String, Boolean> supportedSelectorStrategies = new Hashtable<String, Boolean>();
    supportedSelectorStrategies.put("css_selector", true);
    supportedSelectorStrategies.put("xpath", true);
    if (selectorKind == null || !supportedSelectorStrategies.containsKey(selectorKind) || !supportedSelectorStrategies.get(selectorKind)) {
        return null;
    }
    if (parent != null) {
        parent_css_selector = cssSelectorOfElement(parent);
        parent_xpath = xpathOfElement(parent);
        finder = parent;
    } else {
        finder = driver;
    }
    if (selectorKind == "css_selector") {
        String extended_css_selector = String.format("%s  %s", parent_css_selector, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.cssSelector(selectorValue));
    }
    if (selectorKind == "xpath") {
        String extended_xpath = String.format("%s/%s", parent_xpath, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.xpath(selectorValue));
    }
    return elements;
}
Also used : RuntimeException(java.lang.RuntimeException) Hashtable(java.util.Hashtable) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement) Boolean(java.lang.Boolean)

Example 7 with RuntimeException

use of java.lang.RuntimeException in project selenium_java by sergueik.

the class App method find_elements.

private static List<WebElement> find_elements(String selector_type, String selector_value, WebElement parent) {
    int flexible_wait_interval = 5;
    SearchContext finder;
    long wait_polling_interval = 500;
    String parent_css_selector = null;
    String parent_xpath = null;
    WebDriverWait wait = new WebDriverWait(driver, flexible_wait_interval);
    wait.pollingEvery(wait_polling_interval, TimeUnit.MILLISECONDS);
    List<WebElement> elements = null;
    Hashtable<String, Boolean> supported_selectors = new Hashtable<String, Boolean>();
    supported_selectors.put("css_selector", true);
    supported_selectors.put("xpath", true);
    if (selector_type == null || !supported_selectors.containsKey(selector_type) || !supported_selectors.get(selector_type)) {
        return null;
    }
    if (parent != null) {
        parent_css_selector = css_selector_of(parent);
        parent_xpath = xpath_of(parent);
        finder = parent;
    } else {
        finder = driver;
    }
    if (selector_type == "css_selector") {
        String extended_css_selector = String.format("%s  %s", parent_css_selector, selector_value);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.cssSelector(selector_value));
    }
    if (selector_type == "xpath") {
        String extended_xpath = String.format("%s/%s", parent_xpath, selector_value);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.xpath(selector_value));
    }
    return elements;
}
Also used : RuntimeException(java.lang.RuntimeException) Hashtable(java.util.Hashtable) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement)

Example 8 with RuntimeException

use of java.lang.RuntimeException in project selenium_java by sergueik.

the class App method testUpload.

@Test
public static void testUpload() throws Exception {
    // Wait for the page to load ( check that logo / title is updated )
    WebElement element = driver.findElement(By.cssSelector("a.brand"));
    assertThat(element.getAttribute("title"), containsString("Translate text, documents and websites for free"));
    // optional:
    // driver.manage().window().maximize();
    WebElement uploadButton = driver.findElement(By.cssSelector("div[id = 'upload-button']"));
    highlight(uploadButton, 1500);
    WebElement uploadElement = driver.findElement(By.cssSelector("input[class='ajaxupload-input']"));
    assertThat(uploadElement, notNullValue());
    assertEquals("file", uploadElement.getAttribute("type"));
    assertEquals("file", uploadElement.getAttribute("name"));
    // the uploadElement is not visible
    LocalFileDetector detector = new LocalFileDetector();
    File remoteFile = detector.getLocalFile(localPath);
    // https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/LocalFileDetector.html
    // driver.setFileDetector(new LocalFileDetector());
    ((RemoteWebElement) uploadElement).setFileDetector(detector);
    System.err.format("Uploading the file '%s'.\n", remoteFile.getAbsolutePath());
    // Populate upload input
    uploadElement.sendKeys(remoteFile.getAbsolutePath());
    // TODO : locate the progressbar
    // hard wait
    Thread.sleep(2000);
    try {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("gw-download-link")));
    } catch (RuntimeException timeoutException) {
    // continue - assertion follows
    }
    WebElement downloadLink = driver.findElement(By.className("gw-download-link"));
    assertEquals("Download", downloadLink.getText());
    WebElement downloaIndicator = driver.findElement(By.cssSelector("div [class='status-text'] img[class *= 'gw-icon']"));
    assertThat(downloaIndicator, notNullValue());
    // System.err.println(downloaIndicator.getAttribute("outerHTML"));
    highlight(downloaIndicator);
    String downloadHref = downloadLink.getAttribute("href");
    System.err.println("Reading '" + downloadHref + "'");
    sendGet(downloadHref);
// assertEquals("file", downloadElement.getAttribute("name"));
}
Also used : RuntimeException(java.lang.RuntimeException) WebElement(org.openqa.selenium.WebElement) File(java.io.File) Test(org.junit.Test)

Example 9 with RuntimeException

use of java.lang.RuntimeException in project selenium_java by sergueik.

the class AppTest method getPageContent.

private String getPageContent(String pagename) {
    try {
        URI uri = AppTest.class.getClassLoader().getResource(pagename).toURI();
        System.err.println("Testing: " + uri.toString());
        return uri.toString();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : RuntimeException(java.lang.RuntimeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 10 with RuntimeException

use of java.lang.RuntimeException in project selenium_java by sergueik.

the class AppTest method testUpload.

@Ignore
@Test
public // https://www.youtube.com/watch?v=8OfnQEfzfmw
void testUpload() {
    String openButtonImage = fullPath("open_1920x1080.png");
    Pattern filenameTextBox = new Pattern(fullPath("filename_1920x1080.png"));
    driver.navigate().to(getPageContent("upload.html"));
    try {
        File tmpFile = File.createTempFile("foo", ".png");
        WebElement element = driver.findElement(By.tagName("input"));
        element.click();
        try {
            screen.exists(openButtonImage, sikuliTimeout);
            screen.type(filenameTextBox, tmpFile.getCanonicalPath());
            System.out.println("Uploading: " + tmpFile.getCanonicalPath());
            screen.click(openButtonImage, 0);
        } catch (FindFailed e) {
            verificationErrors.append(e.toString());
        }
        try {
            element = driver.findElement(By.tagName("input"));
            highlight(element);
            assertThat(element.getAttribute("value"), containsString(tmpFile.getName()));
        } catch (Error e) {
            verificationErrors.append(e.toString());
        }
    } catch (IOException e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : Pattern(org.sikuli.script.Pattern) RuntimeException(java.lang.RuntimeException) FindFailed(org.sikuli.script.FindFailed) IOException(java.io.IOException) WebElement(org.openqa.selenium.WebElement) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

RuntimeException (java.lang.RuntimeException)23 IOException (java.io.IOException)11 CipherInputStream (javax.crypto.CipherInputStream)9 WebElement (org.openqa.selenium.WebElement)7 Hashtable (java.util.Hashtable)4 Cipher (javax.crypto.Cipher)4 File (java.io.File)3 Exception (java.lang.Exception)3 Throwable (java.lang.Throwable)3 AEADBadTagException (javax.crypto.AEADBadTagException)3 Test (org.junit.Test)3 FileNotFoundException (java.io.FileNotFoundException)2 Boolean (java.lang.Boolean)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 SearchContext (org.openqa.selenium.SearchContext)2 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)2 SuppressLint (android.annotation.SuppressLint)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 TransactionTooLargeException (android.os.TransactionTooLargeException)1 BufferedReader (java.io.BufferedReader)1