Example usage for org.openqa.selenium.interactions Actions Actions

List of usage examples for org.openqa.selenium.interactions Actions Actions

Introduction

In this page you can find the example usage for org.openqa.selenium.interactions Actions Actions.

Prototype

public Actions(WebDriver driver) 

Source Link

Usage

From source file:com.springer.omelet.driver.DriverUtility.java

License:Apache License

/***
 * Double click on WebElement using JavaScript or Actions Class
 * /*from   ww w. j  a  v a2 s.  c  o  m*/
 * @param element
 *            :Element on which Double click needs to be performed
 * @param clickStrategy
 *            : double click using javascript or using action class
 * @param driver
 * @author kapilA
 */
public static void doubleClick(WebElement element, WebDriver driver, CLICK_STRATEGY clickStrategy) {

    switch (clickStrategy) {

    case USING_ACTION:
        Actions action = new Actions(driver);
        action.doubleClick(element).perform();
        break;
    case USING_JS:
        ((JavascriptExecutor) driver).executeScript("var evt = document.createEvent('MouseEvents');"
                + "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);"
                + "arguments[0].dispatchEvent(evt);", element);
    }

}

From source file:com.springer.omelet.driver.DriverUtility.java

License:Apache License

/***
 * Perform drag and drop/*from   w  w w .  j a  va2s . c o m*/
 * 
 * @param sourceElement
 *            :element which need to be dragged
 * @param targetElement
 *            :element on which dragged Element needs to be dropped
 * @param driver
 *            :WebDriver
 * @author kapilA
 */
public static void dragAndDrop(WebElement sourceElement, WebElement targetElement, WebDriver driver) {
    Actions a = new Actions(driver);
    a.dragAndDrop(sourceElement, targetElement).perform();
}

From source file:com.stratio.qa.specs.CommonG.java

License:Apache License

/**
 * Capture a snapshot or an evidence in the driver
 *
 * @param driver driver used for testing
 * @param type type/* w  w w  . jav a  2 s  .  co m*/
 * @param suffix suffix
 * @return String
 */
public String captureEvidence(WebDriver driver, String type, String suffix) {
    String testSuffix = System.getProperty("TESTSUFFIX");
    String dir = "./target/executions/";
    if (testSuffix != null) {
        dir = dir + testSuffix + "/";
    }

    String clazz = ThreadProperty.get("class");
    String currentBrowser = ThreadProperty.get("browser");
    String currentData = ThreadProperty.get("dataSet");

    if (!currentData.equals("")) {
        currentData = currentData.replaceAll("[\\\\|\\/|\\|\\s|:|\\*]", "_");
    }

    if (!"".equals(currentData)) {
        currentData = "-" + HashUtils.doHash(currentData);
    }

    Timestamp ts = new Timestamp(new java.util.Date().getTime());
    String outputFile = dir + clazz + "/" + ThreadProperty.get("feature") + "." + ThreadProperty.get("scenario")
            + "/" + currentBrowser + currentData + ts.toString() + suffix;

    outputFile = outputFile.replaceAll(" ", "_");

    if (type.endsWith("htmlSource")) {
        if (type.equals("framehtmlSource")) {
            boolean isFrame = (Boolean) ((JavascriptExecutor) driver)
                    .executeScript("return window.top != window.self");

            if (isFrame) {
                outputFile = outputFile + "frame.html";
            } else {
                outputFile = "";
            }
        } else if (type.equals("htmlSource")) {
            driver.switchTo().defaultContent();
            outputFile = outputFile + ".html";
        }

        if (!outputFile.equals("")) {
            String source = ((RemoteWebDriver) driver).getPageSource();

            File fout = new File(outputFile);
            boolean dirs = fout.getParentFile().mkdirs();

            FileOutputStream fos;
            try {
                fos = new FileOutputStream(fout, true);
                Writer out = new OutputStreamWriter(fos, "UTF8");
                PrintWriter writer = new PrintWriter(out, false);
                writer.append(source);
                writer.close();
                out.close();
            } catch (IOException e) {
                logger.error("Exception on evidence capture", e);
            }
        }

    } else if ("screenCapture".equals(type)) {
        outputFile = outputFile + ".png";
        File file = null;
        driver.switchTo().defaultContent();
        ((Locatable) driver.findElement(By.tagName("body"))).getCoordinates().inViewPort();

        if (currentBrowser.startsWith("chrome") || currentBrowser.startsWith("droidemu")) {
            Actions actions = new Actions(driver);
            actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform();
            actions.keyUp(Keys.CONTROL).perform();

            file = chromeFullScreenCapture(driver);
        } else {
            file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        }
        try {
            FileUtils.copyFile(file, new File(outputFile));
        } catch (IOException e) {
            logger.error("Exception on copying browser screen capture", e);
        }
    }

    return outputFile;

}

From source file:com.stratio.qa.specs.WhenGSpec.java

License:Apache License

/**
 * Searchs for two webelements dragging the first one to the second
 *
 * @param source/* w w w . j av  a 2  s .  c  o  m*/
 * @param destination
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws ClassNotFoundException
 */
@When("^I drag '([^:]*?):([^:]*?)' and drop it to '([^:]*?):([^:]*?)'$")
public void seleniumDrag(String smethod, String source, String dmethod, String destination)
        throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException {
    Actions builder = new Actions(commonspec.getDriver());

    List<WebElement> sourceElement = commonspec.locateElement(smethod, source, 1);
    List<WebElement> destinationElement = commonspec.locateElement(dmethod, destination, 1);

    builder.dragAndDrop(sourceElement.get(0), destinationElement.get(0)).perform();
}

From source file:com.stratio.qa.specs.WhenGSpec.java

License:Apache License

/**
 * Send a {@code strokes} list on an numbered {@code url} previously found element or to the driver. strokes examples are "HOME, END"
 * or "END, SHIFT + HOME, DELETE". Each element in the stroke list has to be an element from
 * {@link org.openqa.selenium.Keys} (NULL, CANCEL, HELP, BACK_SPACE, TAB, CLEAR, RETURN, ENTER, SHIFT, LEFT_SHIFT,
 * CONTROL, LEFT_CONTROL, ALT, LEFT_ALT, PAUSE, ESCAPE, SPACE, PAGE_UP, PAGE_DOWN, END, HOME, LEFT, ARROW_LEFT, UP,
 * ARROW_UP, RIGHT, ARROW_RIGHT, DOWN, ARROW_DOWN, INSERT, DELETE, SEMICOLON, EQUALS, NUMPAD0, NUMPAD1, NUMPAD2,
 * NUMPAD3, NUMPAD4, NUMPAD5, NUMPAD6, NUMPAD7, NUMPAD8, NUMPAD9, MULTIPLY, ADD, SEPARATOR, SUBTRACT, DECIMAL,
 * DIVIDE, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, META, COMMAND, ZENKAKU_HANKAKU) , a plus sign (+), a
 * comma (,) or spaces ( )/*w  w  w . j a v  a  2s. c o m*/
 *
 * @param strokes
 * @param foo
 * @param index
 */
@When("^I send '(.+?)'( on the element on index '(\\d+?)')?$")
public void seleniumKeys(@Transform(ArrayListConverter.class) List<String> strokes, String foo, Integer index) {
    if (index != null) {
        assertThat(this.commonspec, commonspec.getPreviousWebElements())
                .as("There are less found elements than required").hasAtLeast(index);
    }
    assertThat(strokes).isNotEmpty();

    for (String stroke : strokes) {
        if (stroke.contains("+")) {
            List<Keys> csl = new ArrayList<Keys>();
            for (String strokeInChord : stroke.split("\\+")) {
                csl.add(Keys.valueOf(strokeInChord.trim()));
            }
            Keys[] csa = csl.toArray(new Keys[csl.size()]);
            if (index == null) {
                new Actions(commonspec.getDriver())
                        .sendKeys(commonspec.getDriver().findElement(By.tagName("body")), csa).perform();
            } else {
                commonspec.getPreviousWebElements().getPreviousWebElements().get(index).sendKeys(csa);
            }
        } else {
            if (index == null) {
                new Actions(commonspec.getDriver())
                        .sendKeys(commonspec.getDriver().findElement(By.tagName("body")), Keys.valueOf(stroke))
                        .perform();
            } else {
                commonspec.getPreviousWebElements().getPreviousWebElements().get(index)
                        .sendKeys(Keys.valueOf(stroke));
            }
        }
    }
}

From source file:com.sugarcrm.candybean.automation.control.VControl.java

License:Open Source License

/**
 * Double-click the element./* w  ww  . j  a  va2s. co m*/
 *
 * @throws Exception    if the element cannot be found
 */
public void doubleClick() throws Exception {
    voodoo.log.info("Selenium: double-clicking on control: " + this.toString());
    Actions action = new Actions(this.iface.wd);
    action.doubleClick(we).perform();
}

From source file:com.sugarcrm.candybean.automation.control.VControl.java

License:Open Source License

/**
 * Drag this control and drop onto another control.
 *
 * @param dropControl  target of the drag and drop
 * @throws Exception    if either element cannot be found
 *///from  w w w  . j  a va  2s . co  m
public void dragNDrop(VControl dropControl) throws Exception {
    voodoo.log
            .info("Selenium: dragging control: " + this.toString() + " to control: " + dropControl.toString());
    Actions action = new Actions(this.iface.wd);
    action.dragAndDrop(this.we, dropControl.we).build().perform();
}

From source file:com.sugarcrm.candybean.automation.control.VControl.java

License:Open Source License

/**
 * Hover over this control.//from w w w.  ja v  a2s .  c  o m
 *
 * @throws Exception    if the element cannot be found
 */
public void hover() throws Exception {
    voodoo.log.info("Selenium: hovering over control: " + this.toString());
    Actions action = new Actions(this.iface.wd);
    action.moveToElement(this.we).perform();
}

From source file:com.sugarcrm.candybean.automation.control.VControl.java

License:Open Source License

/**
 * Right-click this control./*from   ww w  . j  a  v a 2s.  c  o m*/
 *
 * @throws Exception    if the element cannot be found
 */
public void rightClick() throws Exception {
    voodoo.log.info("Selenium: right-clicking control: " + this.toString());
    Actions action = new Actions(this.iface.wd);
    action.contextClick(this.we).perform();
}

From source file:com.sugarcrm.candybean.automation.webdriver.WebDriverElement.java

License:Open Source License

/**
 * Double-click the element./*w w  w  .j a  v  a  2 s  .c  o m*/
 */
public void doubleClick() throws CandybeanException {
    logger.info("Double-clicking on element: " + this.toString());
    Actions action = new Actions(this.wd);
    action.doubleClick(we).perform();
}