Search in sources :

Example 61 with NoSuchElementException

use of org.openqa.selenium.NoSuchElementException in project cerberus-source by cerberustesting.

the class ControlService method verifyTextNotInElement.

private MessageEvent verifyTextNotInElement(TestCaseExecution tCExecution, String path, String expected) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Control: verifyTextNotInElement on " + path + " element against value: " + expected);
    }
    // Get value from the path element according to the application type
    String actual = null;
    try {
        Identifier identifier = identifierService.convertStringToIdentifier(path);
        String applicationType = tCExecution.getApplicationObj().getType();
        if (Application.TYPE_GUI.equalsIgnoreCase(applicationType) || Application.TYPE_APK.equalsIgnoreCase(applicationType) || Application.TYPE_IPA.equalsIgnoreCase(applicationType)) {
            actual = webdriverService.getValueFromHTML(tCExecution.getSession(), identifier);
            // In case of null actual value then we alert user
            if (actual == null) {
                MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT_NULL);
                mes.setDescription(mes.getDescription().replace("%STRING1%", path));
                return mes;
            }
            // Construct the message from the actual response
            MessageEvent mes = actual.equalsIgnoreCase(expected) ? new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT) : new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_TEXTNOTINELEMENT);
            mes.setDescription(mes.getDescription().replace("%STRING1%", path));
            mes.setDescription(mes.getDescription().replace("%STRING2%", actual));
            mes.setDescription(mes.getDescription().replace("%STRING3%", expected));
            return mes;
        } else if (Application.TYPE_SRV.equalsIgnoreCase(applicationType)) {
            if (tCExecution.getLastServiceCalled() != null) {
                String responseBody = tCExecution.getLastServiceCalled().getResponseHTTPBody();
                MessageEvent mes;
                switch(tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType()) {
                    case AppService.RESPONSEHTTPBODYCONTENTTYPE_XML:
                        if (!xmlUnitService.isElementPresent(responseBody, path)) {
                            throw new NoSuchElementException("Unable to find element " + path);
                        }
                        String newPath = StringUtil.addSuffixIfNotAlready(path, "/text()");
                        actual = xmlUnitService.getFromXml(responseBody, newPath);
                        // In case of null actual value then we alert user
                        if (actual == null) {
                            mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT_NULL);
                            mes.setDescription(mes.getDescription().replace("%STRING1%", path));
                            return mes;
                        }
                        // Construct the message from the actual response
                        mes = actual.equalsIgnoreCase(expected) ? new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT) : new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_TEXTNOTINELEMENT);
                        mes.setDescription(mes.getDescription().replace("%STRING1%", path));
                        mes.setDescription(mes.getDescription().replace("%STRING2%", actual));
                        mes.setDescription(mes.getDescription().replace("%STRING3%", expected));
                        return mes;
                    case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON:
                        {
                            try {
                                actual = jsonService.getFromJson(responseBody, null, path);
                            } catch (Exception ex) {
                                mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);
                                mes.setDescription(mes.getDescription().replace("%ERROR%", ex.toString()));
                                return mes;
                            }
                        }
                        // In case of null actual value then we alert user
                        if (actual == null) {
                            mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTINELEMENT_NO_SUCH_ELEMENT);
                            mes.setDescription(mes.getDescription().replace("%ELEMENT%", path));
                            return mes;
                        }
                        // Construct the message from the actual response
                        mes = actual.equalsIgnoreCase(expected) ? new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT) : new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_TEXTNOTINELEMENT);
                        mes.setDescription(mes.getDescription().replace("%STRING1%", path));
                        mes.setDescription(mes.getDescription().replace("%STRING2%", actual));
                        mes.setDescription(mes.getDescription().replace("%STRING3%", expected));
                        return mes;
                    default:
                        mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE);
                        mes.setDescription(mes.getDescription().replace("%TYPE%", tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType()));
                        mes.setDescription(mes.getDescription().replace("%CONTROL%", "verifyTextNotInElement"));
                        return mes;
                }
            } else {
                MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOOBJECTINMEMORY);
                return mes;
            }
        } else {
            MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION);
            mes.setDescription(mes.getDescription().replace("%CONTROL%", "verifyTextNotInElement"));
            mes.setDescription(mes.getDescription().replace("%APPLICATIONTYPE%", tCExecution.getApplicationObj().getType()));
            return mes;
        }
    } catch (NoSuchElementException exception) {
        MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_TEXTNOTINELEMENT_NO_SUCH_ELEMENT);
        mes.setDescription(mes.getDescription().replace("%ELEMENT%", path));
        return mes;
    } catch (WebDriverException exception) {
        return parseWebDriverException(exception);
    }
}
Also used : Identifier(org.cerberus.engine.entity.Identifier) MessageEvent(org.cerberus.engine.entity.MessageEvent) NoSuchElementException(org.openqa.selenium.NoSuchElementException) WebDriverException(org.openqa.selenium.WebDriverException) PatternSyntaxException(java.util.regex.PatternSyntaxException) CerberusEventException(org.cerberus.exception.CerberusEventException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 62 with NoSuchElementException

use of org.openqa.selenium.NoSuchElementException in project selenium_java by sergueik.

the class YandexTest method doLogin.

private String doLogin() {
    // And I enter the username
    WebElement element = driver.findElement(By.xpath("//form/div[1]/label/span/input"));
    highlight(element);
    element.clear();
    element.sendKeys(username);
    // Assert that input gets added to the background form
    element = driver.findElement(By.cssSelector("form.new-auth-form input[name='login']"));
    System.err.println("Username: " + element.getAttribute("value"));
    // And I enter the password
    element = driver.findElement(By.xpath("//form/div[2]/label/span/input"));
    highlight(element);
    element.clear();
    element.sendKeys(password);
    // Assert that input gets added to the background form
    element = driver.findElement(By.cssSelector("form.new-auth-form input[name='passwd']"));
    System.err.println("Password: " + element.getAttribute("value"));
    // Evaluate the landing page URL
    element = driver.findElement(By.cssSelector("form.new-auth-form span.new-auth-submit a.nb-button"));
    String login_href = element.getAttribute("href");
    System.err.println("Login href: " + login_href);
    Pattern pattern = Pattern.compile(String.format("%s/auth/\\?mode=qr&retpath=(.+)$", loginURL));
    Matcher matcher = pattern.matcher(login_href);
    String retpath = null;
    if (matcher.find()) {
        try {
            retpath = java.net.URLDecoder.decode(matcher.group(1).toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
        // ignore
        }
    }
    System.err.println("Login retpath: " + retpath);
    // And I click the login button
    element = driver.findElement(By.cssSelector("form.new-auth-form span.new-auth-submit button"));
    highlight(element);
    // String currentUrl = driver.getCurrentUrl();
    element.click();
    // wait until browser is away from the login page
    System.err.println("Waiting to get away from " + loginURL);
    try {
        wait.until(ExpectedConditions.not(ExpectedConditions.urlContains(loginURL)));
    } catch (TimeoutException tex) {
        // verify if the page warns about the invalid credentials
        try {
            element = driver.findElement(By.cssSelector("div.layout-inner div.js-messages div.error-msg"));
            verificationErrors.append("Getting error message " + element.getText());
            System.err.println("Getting error message " + element.getText());
        } catch (NoSuchElementException elex) {
        // ignore
        }
        verificationErrors.append(tex.toString());
    }
    System.err.println("Waiting for " + retpath);
    // wait until browser is on the landing page
    wait.until(ExpectedConditions.urlContains(retpath));
    // System.out.println("Page url: " + driver.getCurrentUrl());
    try {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mail-App-Header div.mail-User")));
    } catch (TimeoutException | UnreachableBrowserException e) {
        verificationErrors.append(e.toString());
    }
    return retpath;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException)

Example 63 with NoSuchElementException

use of org.openqa.selenium.NoSuchElementException in project selenium_java by sergueik.

the class CodeMirrorTest method test2.

// only loads first 35 lines of code
@Test(enabled = true)
public void test2() {
    // Arrange
    driver.get("https://codemirror.net/demo/simplemode.html");
    WebElement codeElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id = 'code']//div[@class='CodeMirror-code']"))));
    assertThat(codeElement, notNullValue());
    // Act
    // brute force ?
    int cnt = 0;
    while (true) {
        cnt++;
        try {
            WebElement codeLine = codeElement.findElement(By.cssSelector(String.format("pre[role='presentation']:nth-of-type(%d)", cnt)));
            assertThat(codeLine, notNullValue());
            executeScript("arguments[0].scrollIntoView(true);", codeLine);
            highlight(codeLine);
            System.err.println(codeLine.getText());
        } catch (NoSuchElementException e) {
            // e.printStackTrace();
            break;
        }
    }
// Assert
}
Also used : WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException) Test(org.testng.annotations.Test)

Example 64 with NoSuchElementException

use of org.openqa.selenium.NoSuchElementException in project irida by phac-nml.

the class ProjectSamplesPage method shareMoveSamples.

private void shareMoveSamples(String project, boolean owner) {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOf(copySamplesModal));
    enterSelect2Value(project);
    if (owner) {
        try {
            giveOwnerBtn.click();
        } catch (NoSuchElementException e) {
            throw new GiveOwnerNotDisplayedException();
        }
    }
    wait.until(ExpectedConditions.elementToBeClickable(copyModalConfirmBtn));
    copyModalConfirmBtn.click();
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("t-copy-samples-modal")));
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) NoSuchElementException(org.openqa.selenium.NoSuchElementException)

Example 65 with NoSuchElementException

use of org.openqa.selenium.NoSuchElementException in project irida by phac-nml.

the class AbstractPage method get.

protected static void get(WebDriver driver, String relativeUrl) {
    String url = BASE_URL + relativeUrl;
    driver.get(url);
    // Check to make sure that there is no server error
    try {
        WebElement main = driver.findElement(By.tagName("main"));
        String error = main.getAttribute("data-error");
        if (!Strings.isNullOrEmpty(error)) {
            determineError(error);
        }
    } catch (NoSuchElementException e) {
        logger.trace("Did not find `main` element on page when checking for errors, everything is *probably* OK.");
    }
}
Also used : WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException)

Aggregations

NoSuchElementException (org.openqa.selenium.NoSuchElementException)362 WebElement (org.openqa.selenium.WebElement)200 Test (org.junit.Test)61 TimeoutException (org.openqa.selenium.TimeoutException)50 Test (org.testng.annotations.Test)50 WebDriverException (org.openqa.selenium.WebDriverException)38 MessageEvent (org.cerberus.engine.entity.MessageEvent)34 WebDriver (org.openqa.selenium.WebDriver)28 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)27 MockitoTest (com.seleniumtests.MockitoTest)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23 Select (org.openqa.selenium.support.ui.Select)21 Test (org.junit.jupiter.api.Test)20 By (org.openqa.selenium.By)20 AnswerItem (org.cerberus.util.answer.AnswerItem)18 ArrayList (java.util.ArrayList)13 StaleElementReferenceException (org.openqa.selenium.StaleElementReferenceException)13 CerberusEventException (org.cerberus.exception.CerberusEventException)11 Actions (org.openqa.selenium.interactions.Actions)11 AndroidElement (io.appium.java_client.android.AndroidElement)10