无法将WebElement强制转换为org.openqa.selenium.internal.Locatable

时间:2015-06-22 15:35:21

标签: java selenium selenium-webdriver automation

我正在尝试通过使用Actions类来自动调整表中列的大小,这是我的代码:

public boolean resizeColumn(String columnToResize, String toColumn) {
    try {
        WebElement column = findElement(columnResize, columnToResize);
        WebElement moveToPosition = findElement(columnByName, toColumn);
        waitUntilClickable(column);
        //click on resize
        getSupport().clickAndHold(column);
        ..
        ...

在PageSupport类中:

public void clickAndHold(WebElement onElement) {
    Actions actions = new Actions(driver);
    actions.clickAndHold(onElement);
}

在Actions类中:

 public Actions clickAndHold(WebElement onElement) {
    action.addAction(new ClickAndHoldAction(mouse, (Locatable) onElement));
    return this;
  }

但是我收到以下错误:

  

java.lang.ClassCastException:eu.ohim.tmdsview.selenium.util.WebElementProxy无法强制转换为org.openqa.selenium.internal.Locatable

显然它不喜欢演员对Locatable,任何想法?提前谢谢!

UPDATE- WebElementProxy类

package eu.ohim.tmdsview.selenium.util;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.ohim.tmdsview.test.TestEnv;

/**
 * <class>WebElementProxy</class>. Defines all the interaction manners that the framework 
 * may perform with the application. 
 * @implements WebElement, WrapsElement
 *
 */
public class WebElementProxy implements WebElement, WrapsElement {

private final Logger logger = LoggerFactory.getLogger(getClass());

private By locator;

private WebDriver searchContext;

private WebElement element;

/**
 * WebElementProxy. Overloaded constructor.
 * @param searchContext. Webdriver search content
 * @param locator. By element
 */
public WebElementProxy(WebDriver searchContext, By locator) {
    this.locator = locator;
    this.searchContext = searchContext;
}

/**
 * WebElementProxy. Overloaded constructor.
 * @param searchContext. Webdriver search content
 * @param element. Webelement object
 */
public WebElementProxy(WebDriver searchContext, WebElement element) {
    this.element = element;
    this.searchContext = searchContext;
}   

/**
 * <method>getElement</method> method: recovers to the framework the WebElement selected to use it 
 * @return the WebElement selected
 */
private WebElement getElement() {
    //If the element is not null, the framework returns it
    if(element != null) {
        return element;
    } else {
        //If the elements has not found, the framework prints the problematic locator 
        logger.trace("findElement() [{}]", locator);
        return searchContext.findElement(locator);
    }
}

/**
 * <method>click</method> method: search the element and does click on it
 */
public void click() {
    infoWithScrenshot("click() [{}]", locator);
    getElement().click();
}

/**
 * <method>submit</method> method: search the element and submits it
 */
public void submit() {
    infoWithScrenshot("submit() [{}]", locator);
    getElement().submit();
}

/**
 * <method>sendKeys</method> method: search the element and writes the text passed as a parameter
 * @param CharSequence... keysToSend. Text to writes on the application
 */
public void sendKeys(CharSequence... keysToSend) {
    infoWithScrenshot("sendKeys() [{}] to element [{}]", keysToSend, locator);
    getElement().sendKeys(keysToSend);
}

/**
 * <method>clear</method> method: search the element and clears it
 */
public void clear() {
    infoWithScrenshot("clear() [{}]", locator);
    getElement().clear();
}

/**
 * <method>getTagName</method> method: search the element and catch the name of it
 * @return the name of the tag as a string
 */
public String getTagName() {
    logger.info("getTagName() [{}]", locator);
    return getElement().getTagName();
}

/**
 * <method>getAttribute</method> method: search the element and catch the attribute of it
 * @return the text inside the attribute element
 */
public String getAttribute(String name) {
    logger.info("getAttribute() [{}] from [{}]", name, locator);
    return getElement().getAttribute(name);
}

/**
 * <method>isSelected</method> method: search the element and selects it
 * @return a boolean value to know if is selected or not an element
 */
public boolean isSelected() {
    logger.info("isSelected() [{}]", locator);
    return getElement().isSelected();
}

/**
 * <method>isEnabled</method> method: search the element and checks if is enabled it or not
 * @return a boolean value to know if is enabled or not an element
 */
public boolean isEnabled() {
    logger.info("isEnabled() [{}]", locator);
    return getElement().isEnabled();
}

/**
 * <method>isEnabled</method> method: search the element and recovers the inside it
 * @return a string value with the text inside the element
 */
public String getText() {
    logger.info("getText() [{}]", locator);
    return getElement().getText();
}

/**
 * <method>findElements</method> method: search elements in the web code and returns them
 * @param by. By
 * @return The list of elements that have found, to use it
 */
public List<WebElement> findElements(By by) {
    logger.trace("findElements() [{}] on element [{}]", by, locator);       
    return getElement().findElements(by);
}

/**
 * <method>findElement</method> method: search an element in the web code and returns it
 * @param by. By
 * @return The element found to use it
 */
public WebElement findElement(By by) {
    logger.trace("findElement() [{}] on element [{}]", by, locator);        
    return getElement().findElement(by);
}

/**
 * <method>isDisplayed</method> method: search an element in the web code and check if is displayed
 * currently on the "ghost" window browser
 * @return Boolean value to know if the element is displayed currently or not 
 */
public boolean isDisplayed() {
    logger.trace("isDisplayed() [{}]", locator);
    return getElement().isDisplayed();
}

/**
 * <method>getLocation</method> method: search an element in the web code and gets his location
 * @return the Point where the element is
 */
public Point getLocation() {
    logger.info("getLocation() [{}]", locator);     
    return getElement().getLocation();
}

/**
 * <method>getSize</method> method: search an element in the web code and gets his size
 * @return the element Size of the element
 */
public Dimension getSize() {
    logger.info("getSize() [{}]", locator);     
    return getElement().getSize();
}

/**
 * <method>getCssValue</method> method: search an element in the web code and gets his Css value
 * @return a string with the property name
 */
public String getCssValue(String propertyName) {
    logger.info("getCssValue() [{}] on element [{}]", propertyName, locator);           
    return getElement().getCssValue(propertyName);
}

/**
 * <method>getLocator</method> method: search an element in the web code and gets his localization
 * @return the element localization
 */
public By getLocator() {
    return locator;
}

/**
 * <method>toString</method> method: makes as a string properly formated the WebElementProxy 
 * @return String properly formated
 */
@Override
public String toString() {
    return ConstantsFramework.WEB_ELEMENT_PROXY_LOCATOR + locator + ConstantsFramework.ELEMENT + element + ConstantsFramework.CLOSE_BRACKET;
}

/**
 * <method>getWrappedElement</method> method: to obtain the wrappered element 
 * @return the WebElement to wrapper
 */
@Override
public WebElement getWrappedElement() {
    return getElement();
}

/**
 * <method>infoWithScrenshot</method> method: adds information to the screenshot taken
 * @param msg. String with a comment
 * @param Object... args. The screenshot
 */
private void infoWithScrenshot(String msg, Object... args) {
    if(TestEnv.instance().isAutomaticScreenshot()) {
        String imgPath = ScreenshotUtil.takeScreenshot();
        //If the image path exists
        if(imgPath != null) {
            msg += " [<a class=\"screenshot\" href=\"" + imgPath + "\" target=\"_blank\">before</a>]";
        }
    }
    logger.info(msg, args);     
}   

}

更新 这就是我想要调整大小的内容,点击:style =&#34; cursor:col-resize;

<thead>
<th id="grid_ipvalue" class="ui-state-default ui-th-column ui-th-ltr" title="Indication of the product" role="columnheader" style="width: 568px;">
<th id="grid_ApplicantName" class="ui-state-default ui-th-column ui-th-ltr" title="Owner name" role="columnheader" style="width: 426px;" aria-selected="true">
<span class="ui-jqgrid-resize ui-jqgrid-resize-ltr" style="cursor: col-resize;"/>
<div id="jqgh_grid_ApplicantName" class="ui-jqgrid-sortable">
</th>
<th id="grid_did" class="ui-state-default ui-th-column ui-th-ltr" title="Design number" role="columnheader" style="width: 311px;">

在这里,您可以看到table

1 个答案:

答案 0 :(得分:1)

从你发布的html代码看起来你正在使用jqGrid。 所以我试着在jqGrid demo site中做同样的事情。没有任何错误,它对我来说很好。

因此,尝试使用简单代码而不是工厂模型,然后检查

ChromeDriver版本2.15

Selenium 2.46

示例代码

   System.setProperty("webdriver.chrome.driver", "D:\\Madhan\\Drivers\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.get("http://www.trirand.com/blog/jqgrid/jqgrid.html");
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"1\"]/td[2]/span")));
    //Click on the Loadingdata menu
    WebElement load = driver.findElement(By.xpath("//*[@id=\"1\"]/td[2]/span"));
    load.click();

    //Click on the JSON Data sub menu
    WebElement json = driver.findElement(By.xpath("//*[@id=\"3\"]/td[2]/span"));
    json.click();
    wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#list2_amount > span")));

    //Amount column dragger span
    WebElement amountcolumn = driver.findElement(By.cssSelector("#list2_amount > span"));
    //total column dragger span
    WebElement totalcolumn = driver.findElement(By.cssSelector("#list2_total > span"));

    Actions action = new Actions(driver);
    //click and hold the amount dragger to total dragger to reize the column
    action.clickAndHold(amountcolumn).moveToElement(totalcolumn).release().build().perform();
    driver.quit();