Example usage for org.openqa.selenium By toString

List of usage examples for org.openqa.selenium By toString

Introduction

In this page you can find the example usage for org.openqa.selenium By toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.citrix.g2w.webdriver.pages.BasePage.java

License:Open Source License

/**
 * Method to find visible element.//from w ww.j  a  v a 2s  . c  o  m
 *
 * @param by
 *            (by element)
 * @return visibleElement (visible element)
 */
public WebElement findVisibleElement(final By by) {
    WebElement visibleElement;
    try {
        visibleElement = (new WebDriverWait(this.driver, this.DEFAULT_TIMEOUT))
                .until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (Throwable cause) {
        String errorMessage = "Could not find visible element: " + by.toString();
        this.logger.logWithScreenShot("findVisibleElement failed.", this.driver);
        throw new RuntimeException(errorMessage);
    }
    return visibleElement;
}

From source file:com.citrix.g2w.webdriver.pages.BasePage.java

License:Open Source License

/**
 * Method to find visible element in the page.
 * //w ww  .j  a v a  2 s  .c  om
 * @param by
 *            (by element to be visible in page)
 * @param timeoutInSeconds
 *            (timeout value in seconds)
 * @return visibleElement (Web element object of the visible element)
 */
public WebElement findVisibleElement(final By by, final int timeoutInSeconds) {
    WebElement visibleElement;
    try {
        visibleElement = (new WebDriverWait(this.driver, timeoutInSeconds))
                .until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (Throwable clause) {
        String errorMessage = "Could not find visible element: " + by.toString();
        this.logger.logWithScreenShot("Could not find visible element: ", this.driver);
        throw new RuntimeException(errorMessage);
    }
    return visibleElement;
}

From source file:com.cognifide.aet.job.common.modifiers.hide.HideModifier.java

License:Apache License

private CollectorStepResult hideElement(By locator, boolean leaveBlankSpace) throws ProcessingException {
    CollectorStepResult result;//from  w  w  w .java 2s  .c om
    try {
        String script = retrieveHidingScript(leaveBlankSpace);

        SeleniumWaitHelper.waitForElementToBePresent(webDriver, locator, getTimeoutInSeconds());

        List<WebElement> webElements = webDriver.findElements(locator);
        for (WebElement element : webElements) {
            ((JavascriptExecutor) webDriver).executeScript(script, element);
        }
        result = CollectorStepResult.newModifierResult();
    } catch (TimeoutException e) {
        final String message = String.format("Element not found before timeout (%s seconds): '%s'",
                getTimeoutInSeconds(), locator.toString());
        result = CollectorStepResult.newProcessingErrorResult(message);
        LOG.info(message);
    } catch (WebDriverException e) {
        final String message = String.format("Error while hiding element %s. Error: %s", locator.toString(),
                e.getMessage());
        result = CollectorStepResult.newProcessingErrorResult(message);
        LOG.warn(message, e);
    } catch (Exception e) {
        throw new ProcessingException("Can't hide element by " + locator.toString(), e);
    }
    return result;
}

From source file:com.cognifide.qa.bb.logging.WebDriverLogger.java

License:Apache License

private String fetchParameter(By by, WebElement element) {
    if (element == null) {
        return by.toString();
    } else {//from  w ww  .  jav a2s.  c o m
        beforeEvent("findBy", by.toString());
        return String.format("%s -> %s", element.toString(), by.toString());
    }
}

From source file:com.consol.citrus.cucumber.step.runner.selenium.SeleniumStepsTest.java

License:Apache License

@Test
public void testClick() {
    SeleniumBrowserConfiguration endpointConfiguration = new SeleniumBrowserConfiguration();
    when(seleniumBrowser.getName()).thenReturn("seleniumBrowser");
    when(seleniumBrowser.getWebDriver()).thenReturn(webDriver);
    when(seleniumBrowser.getEndpointConfiguration()).thenReturn(endpointConfiguration);

    WebElement element = Mockito.mock(WebElement.class);
    when(element.isDisplayed()).thenReturn(true);
    when(element.isEnabled()).thenReturn(true);
    when(element.getTagName()).thenReturn("button");

    when(webDriver.findElement(any(By.class))).thenAnswer(invocation -> {
        By select = (By) invocation.getArguments()[0];

        Assert.assertEquals(select.getClass(), By.ById.class);
        Assert.assertEquals(select.toString(), "By.id: foo");
        return element;
    });//  www  .  j a  va  2 s  .c o  m

    steps.setBrowser("seleniumBrowser");
    steps.click("id", "foo");

    Assert.assertEquals(runner.getTestCase().getActionCount(), 1L);
    Assert.assertTrue(((DelegatingTestAction) runner.getTestCase().getTestAction(0))
            .getDelegate() instanceof SeleniumAction);
    SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) runner.getTestCase().getTestAction(0))
            .getDelegate();

    Assert.assertEquals(action.getBrowser(), seleniumBrowser);
    Assert.assertTrue(action instanceof ClickAction);
    Assert.assertEquals(((ClickAction) action).getProperty(), "id");
    Assert.assertEquals(((ClickAction) action).getPropertyValue(), "foo");

    verify(element).click();
}

From source file:com.consol.citrus.cucumber.step.runner.selenium.SeleniumStepsTest.java

License:Apache License

@Test
public void testShouldDisplay() {
    SeleniumBrowserConfiguration endpointConfiguration = new SeleniumBrowserConfiguration();
    when(seleniumBrowser.getName()).thenReturn("seleniumBrowser");
    when(seleniumBrowser.getWebDriver()).thenReturn(webDriver);
    when(seleniumBrowser.getEndpointConfiguration()).thenReturn(endpointConfiguration);

    WebElement element = Mockito.mock(WebElement.class);
    when(element.isDisplayed()).thenReturn(true);
    when(element.isEnabled()).thenReturn(true);
    when(element.getTagName()).thenReturn("button");

    when(webDriver.findElement(any(By.class))).thenAnswer(invocation -> {
        By select = (By) invocation.getArguments()[0];

        Assert.assertEquals(select.getClass(), By.ByName.class);
        Assert.assertEquals(select.toString(), "By.name: foo");
        return element;
    });/* ww w.j  ava 2 s  .co m*/

    steps.setBrowser("seleniumBrowser");
    steps.should_display("name", "foo");

    Assert.assertEquals(runner.getTestCase().getActionCount(), 1L);
    Assert.assertTrue(((DelegatingTestAction) runner.getTestCase().getTestAction(0))
            .getDelegate() instanceof SeleniumAction);
    SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) runner.getTestCase().getTestAction(0))
            .getDelegate();

    Assert.assertEquals(action.getBrowser(), seleniumBrowser);
    Assert.assertTrue(action instanceof FindElementAction);
    Assert.assertEquals(((FindElementAction) action).getProperty(), "name");
    Assert.assertEquals(((FindElementAction) action).getPropertyValue(), "foo");
}

From source file:com.consol.citrus.selenium.action.ExtractAction.java

License:Apache License

@Override
public void doExecute(TestContext context) {
    super.doExecute(context);

    if (elements != null) {
        for (By by : elements.keySet()) {
            String variable = elements.get(by).getVariable();
            String attribute = elements.get(by).getAttribute();
            String value = null;//w w w . j a v a  2  s .  c  om
            logger.info("extracting the element by <{}>", by);

            WebElement element = webClient.findElement(by);

            // check if the by has attribute selection
            if (by instanceof By.ByXPath) {
                // TODO: refactor the following
                String xpathExpression = by.toString().replaceAll("By.xpath:\\s*", "");
                if (xpathExpression.contains("/@")) {
                    // we found attribute selection.
                    String[] parts = xpathExpression.split("/@");
                    attribute = parts[1];
                    String newXpathExpresseion = parts[0];
                    element = webClient.findElement(By.xpath(newXpathExpresseion));
                }
            }

            if (element != null) {
                if (StringUtils.hasText(attribute)) {
                    value = element.getAttribute(attribute);
                } else {
                    value = element.getAttribute("value");
                    if (StringUtils.isEmpty(value)) {
                        value = element.getText();
                    }
                }
            }
            context.setVariable(variable, value);
        }
    }

    if (StringUtils.hasText(pageName)) {
        WebPage pageObj;
        try {
            logger.debug("Initializing the page object {}", pageName);
            Class pageClass = Class.forName(pageName);
            pageObj = webClient.createPage(pageClass);
            logger.debug("page object {} is succesfully created.", pageName);
            webClient.verifyPage(pageObj);
            for (String pageAction : pageActions.keySet()) {
                String variable = pageActions.get(pageAction);
                logger.debug("running the action {} on the page object {}", pageAction, pageName);
                logger.info("Invoking method '" + pageAction + "' on instance '" + pageClass + "'");
                Method actionMethod = pageObj.getClass().getMethod("get" + pageAction);
                String value = (String) actionMethod.invoke(pageObj);
                context.setVariable(variable, value);
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            throw new CitrusRuntimeException(ex);
        }
    }
}

From source file:com.consol.citrus.selenium.action.ValidateAction.java

License:Apache License

@Override
public void doExecute(TestContext context) {
    super.doExecute(context);
    if (validations != null) {
        for (By by : validations.keySet()) {
            String controlValue = validations.get(by);
            if (ValidationMatcherUtils.isValidationMatcherExpression(controlValue)) {
                String actualValue = null;
                WebElement element = webClient.findElement(by);

                String attribute = null;
                // check if the by has attribute selection
                if (by instanceof By.ByXPath) {
                    // TODO: refactor the following
                    String xpathExpression = by.toString().replaceAll("By.xpath:\\s*", "");
                    if (xpathExpression.contains("/@")) {
                        // we found attribute selection.
                        String[] parts = xpathExpression.split("/@");
                        attribute = parts[1];
                        String newXpathExpresseion = parts[0];
                        element = webClient.findElement(By.xpath(newXpathExpresseion));
                    }/*w w w  .j  a  v a2s.  c o m*/
                }

                if (element != null) {
                    if (attribute != null) {
                        actualValue = element.getAttribute(attribute);
                    } else {
                        actualValue = element.getAttribute("value");
                        if (actualValue == null || actualValue.isEmpty()) {
                            actualValue = element.getText();
                        }
                    }
                }
                ValidationMatcherUtils.resolveValidationMatcher(by.toString(), actualValue, controlValue,
                        context);
            } else {
                webClient.validate(by, controlValue, null);
            }
        }
    }

    if (StringUtils.hasText(pageName)) {
        WebPage pageObj;
        try {
            logger.debug("Initializing the page object {}", pageName);
            Class pageClass = Class.forName(pageName);
            pageObj = webClient.createPage(pageClass);
            logger.debug("page object {} is succesfully created.", pageName);
            webClient.verifyPage(pageObj);
            for (String pageAction : pageValidations.keySet()) {
                String controlValue = pageValidations.get(pageAction);
                logger.debug("running the action {} on the page object {}", pageAction, pageName);
                logger.info("Invoking method '" + pageAction + "' on instance '" + pageClass + "'");
                Method actionMethod = pageObj.getClass().getMethod("get" + pageAction);
                String actualValue = (String) actionMethod.invoke(pageObj);
                if (ValidationMatcherUtils.isValidationMatcherExpression(controlValue)) {
                    ValidationMatcherUtils.resolveValidationMatcher(pageAction, actualValue, controlValue,
                            context);
                } else {
                    webClient.validate(actualValue, controlValue, null);
                }
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            throw new CitrusRuntimeException(ex);
        }
    }
}

From source file:com.consol.citrus.selenium.actions.ClickActionTest.java

License:Apache License

@Test
public void testExecute() throws Exception {
    when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() {
        @Override//  ww w .j av  a 2s. co  m
        public WebElement answer(InvocationOnMock invocation) throws Throwable {
            By select = (By) invocation.getArguments()[0];

            Assert.assertEquals(select.getClass(), By.ById.class);
            Assert.assertEquals(select.toString(), "By.id: myButton");
            return element;
        }
    });

    action.execute(context);

    verify(element).click();
}

From source file:com.consol.citrus.selenium.actions.FindElementActionTest.java

License:Apache License

@Test(dataProvider = "findByProvider")
public void testExecuteFindBy(String property, String value, final By by) throws Exception {
    when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() {
        @Override//from w  w  w .j a v a  2s. co m
        public WebElement answer(InvocationOnMock invocation) throws Throwable {
            By select = (By) invocation.getArguments()[0];

            Assert.assertEquals(select.getClass(), by.getClass());
            Assert.assertEquals(select.toString(), by.toString());
            return element;
        }
    });

    action.setProperty(property);
    action.setPropertyValue(value);

    action.execute(context);

    Assert.assertEquals(context.getVariableObject("button"), element);
}