Package com.thoughtworks.selenium

Examples of com.thoughtworks.selenium.Selenium

  • link=textPattern: Select the link (anchor) element which contains text matching the specified pattern.
  • css=cssSelectorSyntax: Select the element using css selectors. Please refer to CSS2 selectors, CSS3 selectors for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.

    Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).

  • Without an explicit locator prefix, Selenium uses the following default strategies:

    Element Filters

    Element filters can be used with a locator to refine a list of candidate elements. They are currently used only in the 'name' element-locator.

    Filters look much like locators, ie.

    filterType=argument

    Supported element-filters are:

    value=valuePattern

    Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.

    index=index

    Selects a single element based on its position in the list (offset from zero).

    String-match Patterns

    Various Pattern syntaxes are available for matching string values:

    If no pattern prefix is specified, Selenium assumes that it's a "glob" pattern.

    For commands that return multiple values (such as verifySelectOptions), the string being matched is a comma-separated list of the return values, where both commas and backslashes in the values are backslash-escaped. When providing a pattern, the optional matching syntax (i.e. glob, regexp, etc.) is specified once, as usual, at the beginning of the pattern.


            ErrorReporter errorReporter = new ErrorReporterImpl(httpCommandProcessor, testContext);

            ErrorReportingCommandProcessor commandProcessor = new ErrorReportingCommandProcessor(httpCommandProcessor, errorReporter);

            final Selenium selenium = new DefaultSelenium(commandProcessor);

            selenium.start();

            testContext.setAttribute(TapestryTestConstants.BASE_URL_ATTRIBUTE, baseURL);
            testContext.setAttribute(TapestryTestConstants.SELENIUM_ATTRIBUTE, selenium);
            testContext.setAttribute(TapestryTestConstants.ERROR_REPORTER_ATTRIBUTE, errorReporter);
            testContext.setAttribute(TapestryTestConstants.COMMAND_PROCESSOR_ATTRIBUTE, commandProcessor);

            testContext.setAttribute(TapestryTestConstants.SHUTDOWN_ATTRIBUTE, new Runnable()
            {
                public void run()
                {
                    try
                    {
                        selenium.stop();
                        seleniumServer.stop();
                        stopWebServer.run();
                    }
                    finally
                    {
    View Full Code Here


            String methodName = stackTrace[index].getMethodName();
            int lNumber = stackTrace[index].getLineNumber();
            String lineNumber = Integer.toString( lNumber );
            String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
            targetPath.mkdirs();
            Selenium selenium = AbstractSeleniumTest.getSelenium();
            String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
            try
            {
                selenium.windowMaximize();
                File fileName = new File( targetPath, fileBaseName + ".png" );
                selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "background=#FFFFFF" );
            }
            catch ( RuntimeException e )
            {
                System.out.println( "Error when take screenshot for test " + tr.getName() + ": " + e.getMessage() );
                try
                {
                    File fileName = new File( targetPath, fileBaseName + ".html" );
                    FileUtils.writeStringToFile( fileName, selenium.getHtmlSource() );
                }
                catch ( IOException ioe )
                {
                    System.out.println( ioe.getMessage() );
                }
    View Full Code Here

            captureScreenshotAndSource( tr.getTestClass().getName(), tr.getThrowable() );
        }

        public static void captureScreenshotAndSource( String cName, Throwable throwable )
        {
            Selenium selenium = AbstractSeleniumTest.getSelenium();
            if ( selenium == null )
            {
                // avoid swallowing exception
                System.err.println( "Not capturing screenshot as Selenium is not initialised" );
                return;
            }

            String locator = "link=Show/hide Stack Trace";
            if ( selenium.isElementPresent( locator ) )
            {
                selenium.click( locator );
            }

            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
            String time = sdf.format( new Date() );
            File targetPath = new File( "target", "screenshots" );
            StackTraceElement trace = getStackTraceOfCallingClass( cName, throwable.getStackTrace() );
            String methodName;
            int lNumber;
            if ( trace == null )
            {
                System.err.println( "Unable to determine the calling method from class " + cName );
                throwable.printStackTrace();
                methodName = "unknown";
                lNumber = 0;
            }
            else
            {
                methodName = trace.getMethodName();
                lNumber = trace.getLineNumber();
            }
            String lineNumber = Integer.toString( lNumber );
            String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
            if ( !targetPath.exists() && !targetPath.mkdirs() )
            {
                System.out.println( "Unable to create screenshots directory" );
                return;
            }
            String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
            try
            {
                selenium.windowMaximize();
                File fileName = getFileName( targetPath, fileBaseName, ".png" );
                selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "" );
            }
            catch ( RuntimeException e )
            {
                System.out.println( "Error when take screenshot of error: " + e.getMessage() );
            }
            try
            {
                File fileName = getFileName( targetPath, fileBaseName, ".html" );
                FileUtils.writeStringToFile( fileName, selenium.getHtmlSource() );
            }
            catch ( IOException ioe )
            {
                System.out.println( "Error writing HTML of error: " + ioe.getMessage() );
            }
    View Full Code Here

            final ErrorReporterImpl errorReporter = new ErrorReporterImpl(httpCommandProcessor, testContext);

            ErrorReportingCommandProcessor commandProcessor = new ErrorReportingCommandProcessor(httpCommandProcessor,
                    errorReporter);

            final Selenium selenium = new DefaultSelenium(commandProcessor);

            selenium.start();

            testContext.setAttribute(TapestryTestConstants.BASE_URL_ATTRIBUTE, baseURL);
            testContext.setAttribute(TapestryTestConstants.SELENIUM_ATTRIBUTE, selenium);
            testContext.setAttribute(TapestryTestConstants.ERROR_REPORTER_ATTRIBUTE, errorReporter);
            testContext.setAttribute(TapestryTestConstants.COMMAND_PROCESSOR_ATTRIBUTE, commandProcessor);

            testContext.setAttribute(TapestryTestConstants.SHUTDOWN_ATTRIBUTE, new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        LOGGER.info("Shutting down selenium client ...");

                        try
                        {
                            selenium.stop();
                        } catch (RuntimeException e)
                        {
                            LOGGER.error("Selenium client shutdown failure.", e);
                        }
    View Full Code Here

            String methodName = stackTrace[index].getMethodName();
            int lNumber = stackTrace[index].getLineNumber();
            String lineNumber = Integer.toString( lNumber );
            String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
            targetPath.mkdirs();
            Selenium selenium = AbstractSeleniumTest.getSelenium();
            String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
            try
            {
                selenium.windowMaximize();
                File fileName = new File( targetPath, fileBaseName + ".png" );
                selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "" );
            }
            catch ( RuntimeException e )
            {
                System.out.println( "Error when take screenshot for test " + tr.getName() + ": " + e.getMessage() );
                try
                {
                    File fileName = new File( targetPath, fileBaseName + ".html" );
                    FileUtils.writeStringToFile( fileName, selenium.getHtmlSource() );
                }
                catch ( IOException ioe )
                {
                    System.out.println( ioe.getMessage() );
                }
    View Full Code Here

          throw new MojoExecutionException("Cannot resolve host " + jooUnitSeleniumRCHost +
                  ". Please specify a host running the selenium remote control or skip tests" +
                  " by -DskipTests", e);
        }
        getLog().info("JooTest report directory: " + testResultOutputDirectory.getAbsolutePath());
        Selenium selenium = new DefaultSelenium(jooUnitSeleniumRCHost, jooUnitSeleniumRCPort, jooUnitSeleniumBrowserStartCommand, testsHtmlUrl);
        try {
          selenium.start();
          getLog().debug("Opening " + testsHtmlUrl);
          selenium.open(testsHtmlUrl);
          getLog().debug("Waiting for test results for " + jooUnitTestExecutionTimeout + "ms ...");
          selenium.waitForCondition("selenium.browserbot.getCurrentWindow().result != null || selenium.browserbot.getCurrentWindow().classLoadingError != null", "" + jooUnitTestExecutionTimeout);
          String classLoadingError = selenium.getEval("selenium.browserbot.getCurrentWindow().classLoadingError");
          if (classLoadingError != null && !classLoadingError.equals("null")) {
            throw new MojoExecutionException(classLoadingError);
          }

          String testResultXml = selenium.getEval("selenium.browserbot.getCurrentWindow().result");
          writeResultToFile(testResultXml);
          evalTestOutput(new StringReader(testResultXml));
        } catch (IOException e) {
          throw new MojoExecutionException("Cannot write test results to file", e);
        } catch (ParserConfigurationException e) {
          throw new MojoExecutionException("Cannot create a simple XML Builder", e);
        } catch (SAXException e) {
          throw new MojoExecutionException("Cannot parse test result", e);
        } catch (SeleniumException e) {
          throw new MojoExecutionException("Selenium setup exception", e);
        } finally {
          selenium.stop();
        }
      }
    View Full Code Here

            final ErrorReporterImpl errorReporter = new ErrorReporterImpl(httpCommandProcessor, testContext);

            ErrorReportingCommandProcessor commandProcessor = new ErrorReportingCommandProcessor(httpCommandProcessor,
                    errorReporter);

            final Selenium selenium = new DefaultSelenium(commandProcessor);

            selenium.start();

            testContext.setAttribute(TapestryTestConstants.BASE_URL_ATTRIBUTE, baseURL);
            testContext.setAttribute(TapestryTestConstants.SELENIUM_ATTRIBUTE, selenium);
            testContext.setAttribute(TapestryTestConstants.ERROR_REPORTER_ATTRIBUTE, errorReporter);
            testContext.setAttribute(TapestryTestConstants.COMMAND_PROCESSOR_ATTRIBUTE, commandProcessor);

            testContext.setAttribute(TapestryTestConstants.SHUTDOWN_ATTRIBUTE, new Runnable()
            {
                public void run()
                {
                    try
                    {
                        LOGGER.info("Shutting down selenium client ...");

                        try
                        {
                            selenium.stop();
                        } catch (RuntimeException e)
                        {
                            LOGGER.error("Selenium client shutdown failure.", e);
                        }
    View Full Code Here

    import com.thoughtworks.selenium.Selenium;
    import com.thoughtworks.selenium.SeleniumLogLevels;

    public class Scrap {
      public static void main(String[] args) {
        Selenium selenium = new DefaultSelenium("http://www.google.com", SeleniumServer.getDefaultPort(), "*firefox",
            "http://www.google.com");
        selenium.start();
        selenium.setContext("A real test, using the real Selenium on the browser side served by Jetty, driven from Java");
        selenium.setBrowserLogLevel(SeleniumLogLevels.WARN);

        Browser browser = new DefaultBrowser(selenium);
        browser.open("/");
        selenium.stop();

        Page currentPage = browser.currentPage();
        Form form = currentPage.form("id");
        Field field = form.field("fieldName");
      }
    View Full Code Here

        public String toString() {
            return getElementReferenceExpression();
        }

        protected String evalSeleniumInspectorExpression(String expression) {
            Selenium selenium = getSelenium();
            String fullExpression = "this." + expression;
            try {
                return selenium.getEval(fullExpression);
            } catch (RuntimeException e) {
                throw new RuntimeException("Error evaluating selenium expression: " + fullExpression, e);
            }
        }
    View Full Code Here

        /**
         * @return true if the element referred to by this element inspector really exists in browser's DOM
         */
        public boolean elementExists() {
            Selenium selenium = getSelenium();
            String elementExistsStr = selenium.getEval("!!(" + getElementReferenceExpression() + ")");
            return Boolean.valueOf(elementExistsStr);
        }
    View Full Code Here

    TOP

    Related Classes of com.thoughtworks.selenium.Selenium

    Copyright © 2018 www.massapicom. All rights reserved.
    All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.